lundi 7 mars 2016

Invoking getters of a generic enum

I need to invoke the field accessor methods, i.e the getter of a generic enum, but cannot figure out how to invoke the methods, or more specifically how to pass a generic enum as a parameter for the invoke-method.

Thanks in advance, any help is appreciated.

this is what I'd like to do more or less.

public void(Class<? extends Enum<?>> enumType) {
    Enum<?>[] enumConstants = enumType.getEnumConstants();
    String[] text = new String[enumConstants.length];
    String[] names =  new String[enumConstants.length];
    for (int i = 0; i < enumConstants.length; i++ ) {
        Method[] methods = enumConstants[i].getClass().getDeclaredMethods();
        for (Method m: enumConstants[i].getClass().getDeclaredMethods()) {
            System.out.println(enumConstants[i].name() + ": " + m.getName() + "()");
            try {
                if (GET_KEY_METHOD_NAME.equalsIgnoreCase(m.getName())) {
                    Object value = m.invoke(I HAVE NO IDEA WHAT TO PUT HERE, "");
                    System.out.println(value.toString());
                }
                if (GET_VALUE_METHOD_NAME.equalsIgnoreCase(m.getName())) {
                    Object value = m.invoke(I HAVE NO IDEA WHAT TO PUT HERE, "");
                    System.out.println(value.toString());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        }
    }

}





Aucun commentaire:

Enregistrer un commentaire