title: Could not find which method <init>() to invoke from this list on newInstance in groovy closure
body: <p>I am learning groovy and I am trying to initialize my class dynamically with default values for all fields. So how I am proceeding is, I am taking the list of all the properties and getting the type of that object and create an object of the type, but I am getting error when executing <code>newInstance</code>:</p>
  
  <pre><code>Exception in thread "main" org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method &lt;init&gt;() to invoke from this list:
    public java.lang.Boolean#&lt;init&gt;(boolean)
    public java.lang.Boolean#&lt;init&gt;(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)
  </code></pre>
  
  <p>Below is the code</p>
  
  <pre><code>public static void init() {
          Position position1 = new Position();
  
          JXPathContext context = JXPathContext.newContext(position1)
          context.createPathAndSetValue('id', '2')
  
          position1.properties.each { Map.Entry entry -&gt;
              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"
      }
  </code></pre>