1024programmer Java How to execute enumeration in java through reflection, please help me!

How to execute enumeration in java through reflection, please help me!

[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


Quoting the reply from TMZ794600991 on the 2nd floor:

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!

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:

ORDERTYPESTATE[] orders = ORDERTYPESTATE.values();
for(ORDERTYPESTATE ord : orders)
System.out.println(ord.getName() + ": " + ord.getValue());

#8


Quote from 7th floor sky_walker85’s reply:

Add the following statement to the code to get the values ​​of all enumeration objects, and then splice them into what you want:

ORDERTYPESTATE[] orders = ORDERTYPESTATE.values();
for(ORDERTYPESTATE ord : orders)
System.out.println(ord.getName() + ": " + ord.getValue());

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


Quote from 3rd floor shixitong’s reply:
Class class2 = field.getClass();

should be

Class class2 = field2.getClass();

Bar

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


Quoting the reply from TMZ794600991 on the 8th floor:
Quote: Quoting the reply from sky_walker85 on the 7th floor:


Add the following statement to the code to get the values ​​of all enumeration objects, and then splice them into what you want:

ORDERTYPESTATE[] orders = ORDERTYPESTATE.values();
for(ORDERTYPESTATE ord : orders)
System.out.println(ord.getName() + ": " + ord.getValue());

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));
}
}
}
}

}

#11


Quoting the reply from sky_walker85 on the 10th floor:
Quote: Quoting the reply from TMZ794600991 on the 8th floor:

Quote: Quoting the reply from sky_walker85 on the 7th floor:


Add the following statement to the code to get the values ​​of all enumeration objects, and then splice them into what you want:

ORDERTYPESTATE[] orders = ORDERTYPESTATE.values();
for(ORDERTYPESTATE ord : orders)
System.out.println(ord.getName() + ": " + ord.getValue());

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.

Quoting the reply from sky_walker85 on the 10th floor:
Quote: Quoting the reply from TMZ794600991 on the 8th floor:

Quote: Quoting the reply from sky_walker85 on the 7th floor:


Add the following statement to the code to get the values ​​of all enumeration objects, and then splice them into what you want:

ORDERTYPESTATE[] orders = ORDERTYPESTATE.values();
for(ORDERTYPESTATE ord : orders)
System.out.println(ord.getName() + ": " + ord.getValue());

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.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-execute-enumeration-in-java-through-reflection-please-help-me-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索