mardi 1 décembre 2015

Reflection - instantiate an interface (newProxyInstance returns null)

I'm trying to create an instance of an interface via reflection.

final Context friendContext = MainApp.get().createPackageContext(BaseDef.PACKAGE_INTERNET_PLUGIN, Context.CONTEXT_INCLUDE_CODE);
final Class classOnLoggedInListener = friendContext.getClassLoader().loadClass(BaseDef.PACKAGE_INTERNET_PLUGIN + ".interfaces.OnLoggedInListener");
InvocationHandler handler = new InvocationHandler() {
    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        Log.d("TEST", method.getName());
        return null;
    }
};
Object listener = Proxy.newProxyInstance(
                    classOnLoggedInListener.getClassLoader(),
                    new Class[]{ classOnLoggedInListener },
                    handler);

// Problem: listener is null!!!

classOnLoggedInListener can be created without problems and the corresponding interface looks like following:

public interface OnLoggedInListener {
    void onLoggedIn();
}

Can someone explain me what's the problem with that?





Aucun commentaire:

Enregistrer un commentaire