Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
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"
–
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
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.