0

The problem I am facing is that when I try to pass an item with the value java.util.ArrayList,pass args[2] with value "add" and Method Parameter with the value Integer it throws a NoSuchMethodException? Any help in this regard is much appreciated. Stack Trace:

java.lang.NoSuchMethodException: java.util.ArrayList.add(java.lang.Integer)
at java.lang.Class.getDeclaredMethod(Unknown Source)
at com.acp.assignments.jcp.JCP.CallMethod(JCP.java:65)
at com.acp.assignments.jcp.JCP.InteractiveMode(JCP.java:129)
at com.acp.assignments.jcp.JCP.BatchOrInterActive(JCP.java:47)
at com.acp.assignments.jcp.Main.main(Main.java:29)

public void CallMethod(String[] args) throws Exception {
    HashMap<String, Object> temp=VariableslistHashMap;
    if(args[0].contains("call")) {
        for(Map.Entry<String, Object> item:temp.entrySet()) {
            for(Map.Entry<String, Object> MethodParameter:temp.entrySet()) {
                if(item.getKey().contains(args[1]) && MethodParameter.getKey().contains(args[3])) {
                    Method method=item.getValue().getClass().getDeclaredMethod(args[2],MethodParameter.getValue().getClass());
                    Object result=(Object) method.invoke(item.getClass(), MethodParameter.getValue());
                    System.out.println(result);
                }
            }
        }
    }
}
WonderWorld
  • 962
  • 1
  • 8
  • 16
afnan1992
  • 57
  • 9

1 Answers1

1

Try using Object as the key. Probably Integer is the type parameter, which is erased..

public static void main(String[] args) throws NoSuchMethodException, SecurityException {

    ArrayList<Integer> i = new ArrayList<>();
    i.getClass().getDeclaredMethod("add", new Class[] { Object.class } );  // Works
    i.getClass().getDeclaredMethod("add", new Class[] { Integer.class } ); // Fails
}
Niels Bech Nielsen
  • 4,459
  • 1
  • 18
  • 42
  • for both item and MethodParameter? I tried to replace the line of Method method declaration with Method method=ArrayList.class.getDeclaredMethod("add", Integer.class); and I still get an exception? – afnan1992 Mar 05 '14 at 13:51
  • GetDeclaredMethod needs a class[] for the parameters not a single class – Niels Bech Nielsen Mar 05 '14 at 13:57
  • In your answer why does the Interger.class one fails and the other one works fine? I want to add an Integer to my ArrayList using reflection. – afnan1992 Mar 05 '14 at 14:02
  • Because type parameters are erased from the class file. There is only one shared class file java.util.ArrayList, and it needs to cater for all types. The type check from the generics only happen in the compiler. – Niels Bech Nielsen Mar 05 '14 at 14:08
  • http://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens – Niels Bech Nielsen Mar 05 '14 at 14:09
  • And you can add an Integer to your arraylist, the method type is just java.lang.Object. You can pass the integer value to the invoke – Niels Bech Nielsen Mar 05 '14 at 14:11
  • Thanks to people like you my passion for programming will never die. – afnan1992 Mar 05 '14 at 14:12