If i have a class, that has a private interface inside, which has the @FunctionalInterface
annotation, and a private constructor which takes an instance of a class that implements this interface, how can I construct this object using reflection with my custom implementations of the said interface? For instance:
public class mcve {
private mcve(mcve.wrapper<String> wrp1, mcve.wrapper<String> wrp2) {
// ...
}
private mcve(mcve.wrapper<String> wrp1) {
// ..
}
public static final mcve INSTANCE_1 = new mcve(ClassExtendsString::new, ClassExtendsString::new);
public static final mcve INSTANCE_2 = new mcve(ClassExtendsString2::new, ClassExtendsString2::new);
@FunctionalInterface
interface wrapper<O> {
O wrap(O object) throws Exception;
}
}
and some code trying to accomplish what I want:
Constructor<mcve> constructor;
constructor = mcve.class.getDeclaredConstructor(??.class);
constructor.setAccessible(true);
mcve testInstance = constructor.newInstance(?? MyCustomClassExtendingString::new);
How do I proceed? The compiler complains about Object
not being a functional interface. I'm not sure what to pass to getDeclaredConstructor
and newInstance
.
Aucun commentaire:
Enregistrer un commentaire