我只是想知道如果我知道它的类加载器,是否可以在 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 功能?