jeudi 21 juillet 2016

How to fetch value of an enum present in an inner class through Reflection

Hello all I am trying to access the value of an enum present in an inner class as shown below ,but what i am getting is not the value but the key . The need of my application is I have to access this value through reflection .

public class Test{
 static class TwelveByTwentyFour {
     public static enum BET_TYPE_NAME {
         Direct12(12),AllOdd(12),AllEven(12), First12(12), Last12(12);

    private int value;
    BET_TYPE_NAME(int value){
                this.value = value;
            }

            public  int getValue() {
                return value;
            }
            public void setValue(int value) {
                this.value = value;
            }
    }

 }
 public static String getBetTypeLength(String gameName,String betType) throws ClassNotFoundException, IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException{
        return Class.forName(Test.class.getCanonicalName()+"$"+gameName+"$"+"BET_TYPE_NAME").getDeclaredField(betType).get(null).toString();
    }
 public static void main(String[] args) throws IllegalArgumentException, SecurityException, ClassNotFoundException, IllegalAccessException, NoSuchFieldException {
    System.out.println(getBetTypeLength("TwelveByTwentyFour", "AllEven"));
}

}

On doing this I am getting "AllEven" as output instead of "12" . can anyone please help me by telling me that how can I get the value.Thanks in anticipation.





Aucun commentaire:

Enregistrer un commentaire