Reflection and Collections

Viewed 85

I have difficulty in accessing member of a class with the parameter using a collection. For example List. This is a tiny example of a "grabber" class, which accesses a function with int parameter, but cannot find the function with the collection as its argument. This code prints a list of all members and their arguments as a diagnosis.

import static java.lang.System.out;
import static java.lang.System.err;

import java.util.List;
import java.util.ArrayList;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class grabber
{
   public static void main(String args[])
   {
      Class<?> c=null;
      try {
        c=Class.forName("item");
      } catch (ClassNotFoundException e) {
        out.printf("?W: class item not found\n");
            return;
      }

    try {
            Object an_item=c.getDeclaredConstructor().newInstance();

            out.printf("Listing methods of 'item' and their parameters\n");

            Method[] allm=c.getDeclaredMethods();
            for (Method m: allm)
            {
                    String mnam=m.getName();
                    Type[] mptype=m.getGenericParameterTypes();
                    out.printf("\n\tItem Method m=%s lg of param types:%d\n",mnam, mptype.length);

                    for (Type t:mptype)
                    {
                            out.printf("\t  arg type=%s\n",t.toString());
                    }
            }
            out.println();

            Method callf2=c.getDeclaredMethod("f2", int.class);
            out.printf("\tMethod callf2() is : %s\n", callf2);
            Object ret=callf2.invoke(an_item,234);

            List<Object> arlob=new ArrayList<Object>(); // identical to item.f1 argument
            Method callf1=c.getDeclaredMethod("f1",arlob.getClass()); // this causes exception

    } catch (Exception e) {
            err.println("?E: Exception "+e.getClass().getName());
            err.println("?E: Exception "+e.getLocalizedMessage());
            e.printStackTrace(err);
            return;
    }
   } /* main() */
} /* class */

The called class "item" is simply:

import java.util.List;

class item
{
    List<Object> f1(List<Object> lob)
    {
            System.out.printf("\t\t------item.f1: lob.lg=%d\n",lob.size());
            return lob;
    }

    int f2(int arg)
    {
            System.out.printf("\t\t------item.f2: arg=%d\n",arg);
            return arg;
    }
}

When I run the class grabber, the following happens, and I could not discover a way to access the function f1(List):

Listing methods of 'item' and their parameters

    Item Method m=f2 lg of param types:1
      arg type=int

    Item Method m=f1 lg of param types:1
      arg type=java.util.List<java.lang.Object>

    Method callf2() is : public int item.f2(int)
            ------item.f2: arg=234
?E: Exception java.lang.NoSuchMethodException
?E: Exception item.f1(java.util.ArrayList)
java.lang.NoSuchMethodException: item.f1(java.util.ArrayList)
    at java.lang.Class.getDeclaredMethod(Class.java:2130)
    at grabber.main(grabber.java:51)

I wonder if someone found out how to access functions using abstract classes in argument list.

1 Answers

Simply solution would be to put known type as you already know what method is accepting as you have done in the first method call.

Change

Method callf1=c.getDeclaredMethod("f1",arlob.getClass());

to

 Method callf1=c.getDeclaredMethod("f1",List.class);
Related