Could not find which method <init> () to call from this list on newInstance in groovy closure

I am learning groovy and I am trying to initialize my class dynamically with default values ​​for all fields. So, as I continue, I take a list of all properties and get the type of this object and create an object of type, but I get an error while executing 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) 

Below is the code

 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" } 
+5
source share
1 answer

Check out the java docs for java.lang.Boolean . As you can see in the Constructor Summary section, there is no no-arg constructor for this class (and this is what the exception message is). You must:

  • call it (constructor) with argument boolean or String
  • use the default value for boolean - false
  • initialize a value using Boolean.FALSE or Boolean.TRUE
+4
source

All Articles