[英]How to invoke method on Enum class using reflection
Need to call the method on the enum class, which i dont have direct build dependency. 需要在枚举类上调用该方法,我没有直接的构建依赖性。 I want to call the method on enum class using the reflection using java. 我想使用Java使用反射在enum类上调用该方法。
I have tried using the Field as well, but no luck 我也尝试过使用Field,但没有运气
class myClass
{
public void validateObjectType(Object obj)
{
Class<?> cls = Class.forName("package1.myEnum");
Class [] parameterTypes = {Object.class};
Method method = cls.getDeclaredMethod("getMyEnum", parameterTypes );
String enumType = (String)method.invoke(null, new Object[]{obj1});
Field enumTypeField = cls.getField(enumType );
// -- invoke method getLocalName() on the object of the enum class.??
Class [] parameters = {String.class};
Method method1= cls.getDeclaredMethod("getLocalName", parameters);
String localizedName = (String) method1.invoke(enumTypeField , new Object[] {enumType});
}
}
However i am getting error at 但是我在错误
method1.invoke(enumTypeField , new Object[] {}) //
Error : 错误:
java.lang.IllegalArgumentException: object is not an instance of declaring class
Package 1: 套餐1:
class enum myEnum
{
A,
B;
public static myEnum getMyEnum(Object a)
{
// business logic.
// -- based on the type of object decide MyEnum
if (null != object) return B;
else return A ;
}
public String getLocalName(String value)
{
if (value.equal(A.toString) return "my A";
else if(value.equal(B.toString) return "my B";
}
}
Package 2: 套餐二:
// -- Here i dont have build dependency on package 1. // --- dont want to add, as it will lead to cyclic dependency //-在这里,我对程序包1没有依赖。//--不想添加,因为它会导致循环依赖
class myClass
{
public void validateObjectType(Object obj)
{
Class<?> cls = Class.forName("package1.myEnum");
Class [] parameterTypes = {Object.class};
Method method = cls.getDeclaredMethod("getMyEnum", parameterTypes );
?? = (??)method.invoke(null, new Object[] {obj1}); // will get the Enum but dont have acces
// -- invoke method getLocalName() on the object of the enum class.??
}
}
Your mistake is trying to convert the result of getMyEnum
into a String
. 您的错误是试图将getMyEnum
的结果转换为String
。 getMyEnum
returns a myEnum
, so you should not convert it to a String
. getMyEnum
返回一个myEnum
,因此您不应将其转换为String
。 Just leave it as an Object
: 只需将其保留为Object
:
Class<?> cls = Class.forName("package1.myEnum");
Class [] parameterTypes = {Object.class};
Method method = cls.getDeclaredMethod("getMyEnum", parameterTypes);
Object enumValue = method.invoke(null, obj);
And since you said getLocalName
doesn't actually accept any parameters, you can just get the method and call it like this: 并且由于您说过getLocalName
实际上并不接受任何参数,因此您只需获取方法并按如下方式调用它即可:
Method method1= cls.getDeclaredMethod("getLocalName");
String localizedName = (String) method1.invoke(enumValue); // <-- using enumValue here!
System.out.println(localizedName);
You don't need the enumTypeField
variable because enumValue
is already the enum value we are going to call getLocalName
on. 您不需要enumTypeField
变量,因为enumValue
已经是我们将调用getLocalName
的枚举值。
The Method.invoke
call requires an object of the type that the method is from as the first parameter. Method.invoke
调用需要该方法来自的对象类型作为第一个参数。 You are passing a Field
instead (in the second call). 您正在传递Field
(在第二个调用中)。
If you want to get a particular enum by its name you can use the valueOf
method and pass that in. 如果要通过名称获取特定的枚举,则可以使用valueOf
方法并将其传入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.