I am trying to create an Object array (Object[]) to be passed to a method (IDescriptor):
Object[] newValues = {
Boolean.TRUE
}
descriptor.setParameters(newValues)
The particular descriptor expects the first value in the array to be a java.lang.Boolean object. However, it seems Groovy is converting to its own boolean class, and when I run the code (the above is not the full code), the descriptor reports that the first Object in the array is not a java.lang.Boolean object.
Not that it is very informative, this is the stacktrace:
org.openscience.cdk.exception.CDKException: The first parameter must be of type Boolean
at org.openscience.cdk.qsar.descriptors.molecular.AromaticAtomsCountDescriptor.setParameters(AromaticAtomsCountDescriptor.java:118)
When I add an assert (repeated in the error), I get this error:
assert newValues[0] instanceof java.lang.Boolean
| | |
| | false
| AromaticAtomCountDescriptorParams$_run_closure3@1cc5069
[AromaticAtomCountDescriptorParams$_run_closure3@1cc5069]
How can I make sure Groovy uses the Java Boolean class in the array, instead of its own richer class?
Use the correct braces:
You used
{}which creates a closure. That also works because closures have a lot of special abilities but they aren’t lists are arrays. So Groovy wraps the single element on the right hand side in a list and then assigns the arraynewValues.