lundi 1 avril 2019

Java Reflection on Android Studio unable to find public static method to invoke

The Java reflection code in my Android application invokes a public static method in a package B, from a package A.

Recently, after some Gradle upgrades, I am unable to invoke the same method. My implementation fails with a NoSuchMethodFound exception. How can I get the implementation to be able to find this method?

I've tried printing all the available methods from the class. The result is all public non-static methods. No static methods are available to be invoked. The method I want to invoke is a static "getInstance" method which initializes the class and returns a singleton instance.

Calling Code:

implementationClass = Class.forName(CLIENT_CLASS_NAME, false, ClientProvider.class.getClassLoader());
final Method method = implementationClass.getMethod("getInstance", (Class[]) null);
result = method.invoke(null, (Object[]) null);

Called Code:

/**
* Public static method to be reflected.
*/
public static ClientImpl getInstance() {
    return ClientImplHolder.INSTANCE;
}

/**
 * Private class used to hold the lazily initialized singleton instance.
 */
private static class ClientImplHolder {
    private static final ClientImpl INSTANCE = new ClientImpl();
}

When attempting to 'getMethod', I receive a NoSuchMethodException.

Not sure what I am doing wrong here, appreciate any help. Thanks!





Aucun commentaire:

Enregistrer un commentaire