I’m using a custom Classloader to create and return an instance of a class, this seems to work ok however when I try to call a method (via the Reflection API) and pass in a custom object as described below I get a NoSuchMethodException:
Supposing that the custom class loader creates and returns an instance like so:
Object obj = customClassLoader.load(String className,Class[] paramTypes,Object[] param)
Then I call a method (via reflection) and pass in a custom object:
NOTE: THIS IS THE LINE CAUSING THE ERROR
Method m = obj.getClass.getDeclaredMethod("mName",new Class[]{aCustomObject.class})
m.invoke(obj,new Object[]{new CustomObject() })
I’m stumped as to what could be causing the exception since a method definitely does exist which takes the specified custom object, I have confirmed this by using reflection to list all methods.
How is your custom loader’s load() method instantiating the object it is to return? Maybe the
NoSuchMethodExceptionarises during trying to find the correct constructor?This example seems to work out OK: