[size=16px]public class CommonConstants { /** *@desc Order type * @author tangkun * */ public enum ORDERTYPESTATE { HEADQUARTERSORDERS(“Headquarters Order”, 0), DEALERORDERS(“To be sold”, 1), DIRECTORDERS( “Direct Sales Order”, 2); private final String name; private final Integer value ; ORDERTYPESTATE(String name, Integer value) { this.name = name; this.value = value; } public String getName() { return name; } public Integer getValue() { return value; } }
public static void main(String[] args) throws Exception {
Class class1 = Class .forName(“com.tangkun.utils.CommonConstants$ORDERTYPESTATE”);
Field[] field = class1.getFields(); for ( Field field2 : field) { field2.setAccessible(true); Class class2 = field.getClass(); Want to dynamically call the value of the variable in the enumeration */ Method[] methods = class2.getDeclaredMethods(); for (Method method : methods) { System.out.println(method.getName()); }
} } } [/size]
11 solutions
#1
public static void main(String[] args) throws Exception {
Class class1 = Class
.forName("learning.CommonConstants$ORDERTYPESTATE");
ORDERTYPESTATE order = ORDERTYPESTATE.DEALERORDERS ;
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
if(method.getName().startsWith("get")){
System.out.println(method.getName() + ": " + method.invoke(order));
}
}
}
Is this what the poster wants?
#2
sky_walker85: In my actual application scenario, I pass an “ORDERTYPESTATE” through a custom label to get the selection list to generate the drop-down box (select), so I cannot use your step: ORDERTYPESTATE order = ORDERTYPESTATE.DEALERORDERS ;, and the enumeration seems to be unusable Object object = class2.newInstance();This method, so its method cannot be executed dynamically, which is very helpless!
#3
Class class2 = field.getClass();
should be
Class class2 = field2.getClass();
Bar
#4
This is no problem. My order variable is just an example. You can generate instances corresponding to different orders based on the parameters passed in. For example, pass in DEALERORDERS to generate ORDERTYPESTATE order = ORDERTYPESTATE.DEALERORDERS, and then execute method.invoke That’s it
#5
Field[] field = class1.getFields();
for(int i=0;i<field.length;i++){
System.out.pfrintln(sp;* methods is empty, I want to dynamically call the value of the variable in the enumeration
*/
for (Method method : methods) {
if (“getName”.equals(method.getName()))
;
}
//Error report
Object object = class2.newInstance();
Method m = class2.getMethod(“getName”, null);
Object valueObject = m.invoke(object, null);
/********Remaining splicing html code*****/
}
return html;
}
}
When jsp uses custom tags, it just passes the name of an enumeration class!
#7
Add the following statement to the code to get the values of all enumeration objects, and then splice them into what you want:
You probably haven’t understood what I mean. Maybe I didn’t make it clear enough. When I was trying to get the value and splice it, I only knew it was the name of the enumeration, which was just a string. I didn’t know what type the enumeration was. , it is only obtained by reflection based on the package name + enumeration name, so it cannot be determined at all? ORDERTYPESTATE or RECORD_STATE? ORDERTYPESTATE type!
#9
This is indeed my own problem. I can get the method, but an object is required to execute the method. Enumeration types cannot create instances through reflection. Can this be solved?
#10
Try this
public static void main(String[] args) throws Exception {
//String enumStr = "ORDERTYPESTATE";
Class class1 = Class
.forName("learning.CommonConstants$ORDERTYPESTATE");
Method[] methods = class1.getDeclaredMethods();
if(class1.isEnum()){
List list = Arrays.asList(class1.getEnumConstants());
for(Object enu : list){
for(Method method : methods){
if(method.getName().startsWith("get")){
System.out.println(method.invoke(enu));
}
}
}
}
}
#11
Add the following statement to the code to get the values of all enumeration objects, and then splice them into what you want:
You probably haven’t understood what I mean. Maybe I didn’t make it clear enough. When I was trying to get the value and splice it, I only knew it was the name of the enumeration, which was just a string. I didn’t know what type the enumeration was. , it is only obtained by reflection based on the package name + enumeration name, so it cannot be determined at all? ORDERTYPESTATE or RECORD_STATE? ORDERTYPESTATE type!
Try this
public static void main(String[] args) throws Exception {
//String enumStr = "ORDERTYPESTATE";
Class class1 = Class
.forName("learning.CommonConstants$ORDERTYPESTATE");
Method[] methods = class1.getDeclaredMethods();
if(class1.isEnum()){
List list = Arrays.asList(class1.getEnumConstants());
for(Object enu : list){
for(Method method : methods){
if(method.getName().startsWith("get")){
System.out.println(method.invoke(enu));
}
}
}
}
}
I would like to thank you, this is okay. I did a list reflection traversal object operation that day, but I didn’t expect this method. It seems that it is not smart enough. It seems that the wait for many days was not in vain. You are very welcome. Niu B, this guy is pretty good.
Add the following statement to the code to get the values of all enumeration objects, and then splice them into what you want:
You probably haven’t understood what I mean. Maybe I didn’t make it clear enough. When I was trying to get the value and splice it, I only knew it was the name of the enumeration, which was just a string. I didn’t know what type the enumeration was. , it is only obtained by reflection based on the package name + enumeration name, so it cannot be determined at all? ORDERTYPESTATE or RECORD_STATE? ORDERTYPESTATE type!
Try this
public static void main(String[] args) throws Exception {
//String enumStr = "ORDERTYPESTATE";
Class class1 = Class
.forName("learning.CommonConstants$ORDERTYPESTATE");
Method[] methods = class1.getDeclaredMethods();
if(class1.isEnum()){
List list = Arrays.asList(class1.getEnumConstants());
for(Object enu : list){
for(Method method : methods){
if(method.getName().startsWith("get")){
System.out.println(method.invoke(enu));
}
}
}
}
}
I would like to thank you, this is okay. I did a list reflection traversal object operation that day, but I didn’t expect this method. It seems that it is not smart enough. It seems that the wait for many days was not in vain. You are very welcome. Niu B, this guy is pretty good.