lundi 16 novembre 2015

Dealing with `Array.clone()` not showing up via reflection

I am writing a library where I want to look for a method with a specific name on a specific class and save that for later. I am doing so via reflection:

public static Method findMethod(Class<?> instance_c, String name, Class<?> ... args_cs) {
    return instance_c.getMethod(name, args_cs);
}

Now the problem is the following:

    Class<?> c = int[].class;
    // int[] i = new int[2].clone();
    System.out.println(findMethod(c, "clone")); // Throws an exception

This is documented behaviour:

Method java.lang.Class.getMethod(String name, Class... parameterTypes):

[snip]

If this Class object represents an array type, then this method does not find the clone() method.


Is there a way that I somehow can still use the functionality of java.lang.reflect.Method with Array#clone? I could imagine a builtin or selfwritten Proxie that lets me keep the functionality of Method in this one special case.

If I should further explain what of Method I'm using especially, please leave a comment. The goal is to keep as much as possible.





Aucun commentaire:

Enregistrer un commentaire