クラスローダーを知っていれば、JVM ランタイム中にクラスのインスタンス (オブジェクトへの参照) を取得できるかどうか疑問に思っています。私の質問を理解するには、以下のコードを参照してください。
クラスA:
package local.run;
public class A {
public static void main(String[] args) {
B b = new B();
b.setCount(5);
C c = new C();
c.dummyMethod();
b.printCount();
}
}
クラス B:
package local.run;
public class B {
public int count = 0;
public void setCount(int aCount) {
count = aCount;
}
public void printCount() {
System.out.println(count);
}
}
クラス C:
package local.run;
public class C {
public void dummyMethod() {
// Can I get the instance of class B created in class A here?
// I don't want to pass the instance of class B as a parameter to this method.
System.out.println("ClassLoader Namespace -> "+B.class.getProtectionDomain().getCodeSource().getLocation());
// I know the ClassLoader Namespace of class B
// How to get instance of class B created in class A???
System.out.println(b.count); // I wan't to print 5
}
}
これが不可能な場合、JSF が @ManagedProperty 機能をどのように実装するのか疑問に思っていますか?