gpt4 book ai didi

java - 通过反射提取和设置枚举值

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:42 26 4
gpt4 key购买 nike

我正在尝试将一些枚举设置为默认值我正在使用以下方法:

private void checkEnum(Field field, String setMethod) {
// TODO Auto-generated method stub
try {
String className = Character.toUpperCase(field.getName().charAt(0)) +
field.getName().substring(1);
Class<?> cls = Class.forName("com.citigroup.get.zcc.intf." + className);
Object[] enumArray = cls.getEnumConstants();

//set to the last Enum which is unknown
invoke(setMethod, enumArray[enumArray.length - 1] );
} catch(Exception e) {
System.out.println(e.toString());
}
}

问题实际上是设置枚举。我已经提取了枚举类型,但随后调用了 MethodInvoker。传入 Enum 对象证明是有问题的。所有枚举都将以下作为枚举数组的最后一个元素。

EnumName.UNKNOWN

然而,这不是通过调用方法设置的,如下所示:

private Object invoke(String methodName, Object newValue) {
Object value = null;
try {
methodInvoker.setTargetMethod(methodName);

if (newValue != null) {
methodInvoker.setArguments(new Object[]{newValue});
} else {
methodInvoker.setArguments(new Object[]{});
}
methodInvoker.prepare();
value = methodInvoker.invoke();
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
} catch (java.lang.reflect.InvocationTargetException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(),e);
}

return value;
}

所以我不知道为什么

invoke(setMethod, enumArray[enumArray.length -1] );

没有设置我的枚举

最佳答案

我试图让您的代码运行。 methodInvoker.prepare() 调用抛出:java.lang.IllegalArgumentException:需要“targetClass”或“targetObject”

因此,如果我了解您的用例,我在类中添加了缺少的参数并且代码有效。您似乎正在设置一个静态字段,其名称必须是 com.citigroup.get.zcc.intf 下的枚举类的名称,字段名称中的第一个字符小写。

这是我修改后的代码:

    public void checkEnum(Field field, String setMethod, Class clazz) {
try {
String className = Character.toUpperCase(field.getName().charAt(0)) +
field.getName().substring(1);
Class<?> cls = Class.forName("com.citigroup.get.zcc.intf." + className);
Object[] enumArray = cls.getEnumConstants();

//set to the last Enum which is unknown
invoke(setMethod, enumArray[enumArray.length - 1], clazz);
} catch (Exception e) {
System.out.println(e.toString());
}
}
private Object invoke(String methodName, Object newValue, Class clazz) {
Object value = null;
try {
MethodInvoker methodInvoker = new MethodInvoker(); // this was missing
methodInvoker.setTargetMethod(methodName);
methodInvoker.setTargetClass(clazz); // This was missing

if (newValue != null) {
methodInvoker.setArguments(new Object[]{newValue});
} else {
methodInvoker.setArguments(new Object[]{});
}
methodInvoker.prepare();
value = methodInvoker.invoke();
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(), e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(), e);
} catch (java.lang.reflect.InvocationTargetException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Method invocation failed. " + e.getMessage(), e);
}

return value;
}
}

我的测试代码类似于(Show 是我的枚举类,MethodNameHelper 之前已发布到 StackExchange):

public class StackExchangeTestCase {
protected static final Logger log = Logger.getLogger(StackExchangeTestCase.class);
public static Show show;
public static void setShow(Show newShow) {
show = newShow;
}

@Test
public void testJunk() throws Exception {

Method me = (new Util.MethodNameHelper(){}).getMethod();
Class<?> aClass = me.getDeclaringClass();
Field att1 = aClass.getField("show");
show = null;

methodNameHelper.checkEnum(att1, "setShow", aClass);

System.out.println(show); // worked
}
}

关于java - 通过反射提取和设置枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7268517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com