Groovy: unable to pass object 'true' with class 'java.lang.Boolean' to class 'java.io.File'

I do not understand why I am getting the following error. Any thoughts?

I get an error:

Cannot cast object 'true' with class 'java.lang.Boolean' to class 'java.io.File'

This is the code that creates the error in the line 'if (envProp.exists () ...':

 static private File envProp = new File('env.properties') static private File envPropBak = new File('env.properties.bak') @BeforeClass static void beforeAll() { if (envProp.exists()) { envPropBak.write( envProp.text ) } } 

I do not understand why envProp.exists() trying to do something different. The .exists() method should just return boolean .

thanks

+4
source share
3 answers

I had the same problem today, but in my case it was:

 org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'true' with class 'java.lang.Boolean' to class 'java.util.List' 

The thing is, if you have something like this:

 public List<Foo> method(){ methodThatReturnsTrue() } 

Since Groovy uses the last return clause as the return value of the method, it tries to pass true to <some_not_boolean_type> , and so we are getting an error.

+5
source

For completeness, I have to say that I received a similar message:

Unable to pass object 'true' with class 'java.lang.Boolean' to class 'Java.util.List'

When I call:

 final List<String> reportedChangedFiles = linesOfChangedFiles.removeAll([null]) 

I expected removeAll () to return a new collection, but I forgot that it modifies the current collection and returns a boolean value instead. It was so easy:

 linesOfChangedFiles.removeAll([null]) 
+1
source

This was a bug and fixed in newer versions (2.3.2): http://jira.codehaus.org/browse/GROOVY-6810

0
source

Source: https://habr.com/ru/post/1485827/


All Articles