Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get static enum from class

Tags:

enums

grails

gsp

i have an java-constraint-interface declaring some enums.

public interface MyClass{static enum BOOP{a,b,c,d,e,f}};

how can i get dynamicly the name of the 6th enum-const from the "BOOP"-enum?

${org.java.some.MyClass."$enumName"[5]}

doenst work.

like image 574
Grim Avatar asked May 03 '26 08:05

Grim


2 Answers

This should work :

public interface MyClass{static enum BOOP{a,b,c,d,e,f}};

${org.java.some.MyClass.BOOP.values()[5]}    // prints f

Additionally, you can't get the element with index 6 due to the array is 0-based and you do not have 7 elements in your enum.

like image 182
aiolos Avatar answered May 05 '26 10:05

aiolos


I got it by myself

<%=org.java.some.MyClass.class.getDeclaredClasses().find{it.simpleName=enumName}.values()[5]%>
like image 45
Grim Avatar answered May 05 '26 09:05

Grim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!