我有一个Class A,它有一个私有的、不可变的成员变量,这个成员变量是另一个Class B的对象。
Class A {
private final classB obj;
}
Class B {
public void methodSet(String some){
}
}
我知道类A是单例模式。我需要使用类B中的“methodSet”方法设置一个值。我尝试访问类A,并在类A中获取ClassB的实例。
我这样做:
Field MapField = Class.forName("com.classA").getDeclaredField("obj");
MapField.setAccessible(true);
Class<?> instance = mapField.getType(); //get teh instance of Class B.
instance.getMethods()
//loop through all till the name matches with "methodSet"
m.invoke(instance, newValue);
在这里我遇到了一个异常。
我不是很擅长反射。如果有人能提供解决方案或指出问题,我会非常感激。