lundi 20 avril 2020

Use reflection to iterate over default methods on an interface and call them

public interface IMyInterface {
    default String get() {
        return "help";
    }
}
 Class settings = IMyInterface.class;
IMyInterface target = (IMyInterface) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{IMyInterface.class}, (o, method, objects) -> method.invoke(o,objects));
Method[] methods =  settings.getMethods()

for(Method method : methods){
  Object value = method.invoke(target);
}

Doesn't work object is not an instance of declaring class ?





Aucun commentaire:

Enregistrer un commentaire