gpt4 book ai didi

java - 我无法创建嵌套类的实例,也无法初始化字段。我想通过单元测试

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:35 25 4
gpt4 key购买 nike

我在 Debug模式下获取了嵌套类的类类型,但无法创建实例来在下一步中初始化嵌套实例的字段。 (请参阅我的评论行)。所以我的单元测试因错误而失败。

public class Reflector {
public static <T> T initialize(Class<? extends T> clazz) {

try {
Object classInstance = clazz.newInstance();
System.out.println("MainclassInstance: "+ classInstance.toString());

for (Field field : clazz.getDeclaredFields()) {

System.out.println(
"\n### 1. For-Schleife\nclazz.getDeclaredFields().size(): " + clazz.getDeclaredFields().length);

System.out.println("clazz.field: " + field.getName());

//Class Object
if (field.getType().isMemberClass()) {

Object memberClassInstance = field.getType(); //Nested$FirstNested

System.out.println("memberClassInstance: " + memberClassInstance.toString()); //Nested$FirstNested

List<Field> memberClassFields = Arrays.asList(((Class<?>) memberClassInstance).getDeclaredFields());

for (int memberFieldIndex = 0; memberFieldIndex < memberClassFields.size() - 1; memberFieldIndex++) {
System.out.println("meberClassField: " + memberClassFields.get(memberFieldIndex).getName());


//memberClassFields.get(memberFieldIndex).set(memberClassInstance, "nestedField");
}
//field.set(memberClassInstance, "MyObject");
//field.set(classInstance, memberClassInstance);
}

}

return (T) classInstance;

} catch (SecurityException | IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException ex) {
Logger.getLogger(Reflector.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Reflector.class.getName()).log(Level.SEVERE, null, ex);
}

return null;
}

这是 JUnit 测试类

public class ReflectorTest {

@Test
public void testInitializeNestedObject() {
Nested actual = Reflector.initialize(Nested.class);

assertNotNull(actual);
assertNotNull(actual.nested1);
assertTrue(actual.nested1 instanceof Nested.FirstNested);
assertNotNull(actual.nested1.nestedField1);
assertNotNull(actual.nested1.nestedField2);

assertNotNull(actual.nested2);
assertTrue(actual.nested2 instanceof Nested.SecondNested);
assertNotNull(actual.nested2.nestedField3);
assertNotNull(actual.nested2.nestedField4);
}
}

这是数据类

public class Nested {

public class FirstNested{
public Object nestedField1;
public Object nestedField2;
}

public class SecondNested{
public Object nestedField3;
public Object nestedField4;
}

public FirstNested nested1;
public SecondNested nested2;

}

最佳答案

问题是,虽然您为容器类 Nested 创建实例,但您没有为封闭类 FirstNestedSecondNested 创建实例。为了制作这些实例,您可以像这样更改您的类

public static class Nested
{
public static class FirstNested
{
public Object nestedField1;
public Object nestedField2;
}

public static class SecondNested
{
public Object nestedField3;
public Object nestedField4;
}

public FirstNested nested1;
public SecondNested nested2;

}

您必须将内部类设为静态,以便可以通过反射轻松访问它们。然后,您必须实现(可能以递归方式)创建新实例并将这些实例注册到您的容器类实例。

类似这样的事情

Object memberClassInstance = field.getType().newInstance();
field.set(classInstance, memberClassInstance);

或者你可以保持类不变并使用类似的东西

Object memberClassInstance = field.getType().getConstructor(Nested.class).newInstance()

正如@CoronA 在评论中提到的。有关如何实例化内部类的更多信息可以在这里找到 How to instantiate inner class with reflection in Java?

关于java - 我无法创建嵌套类的实例,也无法初始化字段。我想通过单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33567250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com