what is or will be the faster way to lookup suitable constructor matching object types (find constructor which fits our init args method) for given example:
class A implements IA3,IA2,IA1 {
}
class B implements IB1,IB2 {
}
public class C implements IC1 {
private static volatile Object _simpleLock = new Object();
private static IC1 _instance;
public static IC1 getInstance() {
if(getState()) {
return _instance;
}
throw new IllegalStateException("Call make me first :)")
}
public static boolean getState() {
return _instance != null && _instance.isInitialized()
}
// this is default C class constructor
// but subclass can have its own multiple version
// (regardless modifiers)
// and we need to find the best one
// which fits to passed arg to init method
protected C(IA2 a, IB) {
}
// smart try initialize :) from what we will give you
public static <IA extends IA2, IB extends IB1, IC extends IC1>
void initialize(Class<IC> forClass, IA a, IB b) {
synchronize(_simpleLock) {
try {
// >>> lookupMatching constructor types ??? <<<
// 1) getDeclaredConstructors
// 2) match each permutation on class
// and its interfaces (from sub to super?)
// handling possible null objects?
Class[] types =
//
IC constructor = forClass.getDeclaredConstructor(types);
constructor.setAccessible(true);
_instance = constructor.newInstance(new Object[]{a,b});
_instance.setInitialized(true);
} catch {Exception e} {
}
}
}
}
usage :
// call 1
C.initialize(C.class, new IA2 {impl}, new IB1 {impl});
// call 2
class IA7 impplements IA2 {
}
C.initialize(C.class, new IA7(bla,bla), new IB1 {impl});
// call 3
class IA7 impplements IA2 {
}
class IA6 impplements IA2 {
}
class C2 implements IC1 {
protected C2(IA7 a, IB1 b) {
}
protected C2(IA6 a, IB1 b) {
}
}
C.initialize(C2.class, new IA7(bla,bla), new IB1 {impl});
// or
C.initialize(C2.class, new IA6(noblabla), new IB1 {impl});
Aucun commentaire:
Enregistrer un commentaire