2011-09-01 74 views
3

我想设定一些枚举的默认值,我使用下面的方法设置的枚举值:提取,并通过反射

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对象证明是一个问题。所有枚举都具有以下内容作为枚举数组的最后一个元素。

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

是不是我的设置枚举

EnumName.UNKNOWN 

然而,这不被通过,它看起来像invoke方法设置

+0

你看到一个异常? 'invoke'中'newValue'的值是'null',还是你的枚举之一? –

+0

你的代码有点复杂。你的'invoke'方法没有一个可以设置枚举的对象实例。你只是传递一个方法名和一个枚举值...你真的没有机会在构造函数中做这件事吗? – Kai

回答

3

我试图让你的代码运行。该methodInvoker.prepare()调用被扔: java.lang.IllegalArgumentException异常:无论是“targetClass”或“targetObject”需要

所以我在课堂上缺少的参数,并将该代码的工作,如果我理解你的用例。 您似乎设置了一个静态字段,其名称必须是com.citigroup.get.zcc.intf中Enum类的名称,并且字段名称中的第一个字符已降低。

这是我修改后的代码:

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

我的测试代码相似(显示为一个枚举类矿井,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 
    } 
} 
+0

getEnumConstants()是我正在寻找的。谢谢。 – David