Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

groovy

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 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"
    }
like image 346
Ambuj Jauhari Avatar asked Jul 13 '16 08:07

Ambuj Jauhari


People also ask

What is -> in Groovy?

It is used to separate where you declare bindings for your closure from the actual code, eg: def myClosure = { x, y -> x + y } the part before -> declares that the closure has two arguments named x and y while the second part is the code of the closure.

How do you call a function in Groovy?

In Groovy we can add a method named call to a class and then invoke the method without using the name call . We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator: () . This can be especially useful in for example a DSL written with Groovy.

What is this in Groovy?

" this " in a block mean in Groovy always (be it a normal Java-like block or a Closure) the surrounding class (instance). " owner " is a property of the Closure and points to the embedding object, which is either a class (instance), and then then same as " this ", or another Closure.


1 Answers

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

  • invoke it (constructor) with boolean or String argument
  • use default value for boolean - which is false
  • initialize the value with Boolean.FALSE or Boolean.TRUE
like image 160
Opal Avatar answered Oct 23 '22 18:10

Opal