我正在学习groovy,我正在尝试使用所有字段的默认值动态初始化我的类.所以我正在进行的是,我正在获取所有属性的列表并获取该对象的类型并创建该类型的对象,但是在执行newInstance时遇到错误:
Exception in thread "main" org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method () to invoke from this list:
public java.lang.Boolean#(boolean)
public java.lang.Boolean#(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"
}