问题描述
查看Java文档的java.lang.Boolean
。如您在本节中所看到的,Constructor
Summary
该类没有no-arg构造函数(这就是异常消息所说的)。您必须:
解决方法
我正在学习groovy,并且尝试使用所有字段的默认值动态初始化我的类。因此,我要进行的工作是,获取所有属性的列表并获取该对象的类型并创建该类型的对象,但是执行时出现错误newInstance
:
Exception in thread "main" org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method <init>() to invoke from this list:
public java.lang.Boolean#<init>(boolean)
public java.lang.Boolean#<init>(java.lang.String)
at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3160)
at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3097)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1707)
at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1526)
下面是代码
public static void init() {
Position position1 = new Position();
JXPathContext context = JXPathContext.newContext(position1)
context.createPathAndSetValue('id','2')
position1.properties.each { Map.Entry entry ->
String propertyName = entry.key;
if (!propertyName.equalsIgnoreCase('class')) {
Class clazz = position1.class.getDeclaredField(propertyName)?.type
println "$clazz"
Object ob = clazz.newInstance()
}
}
Identifier sourceSystemPositionId = new Identifier()
context.setValue('sourceSystemPositionId/content','default-content')
context.setValue('sourceSystemPositionId/domain','default-domain')
println "$position1"
}