Reputation: 1006744
I have the following Groovy script as test.groovy
:
import test.Vehicle
def ok=new Vehicle();
def test=new Vehicle.Deserializer();
println "Hello, world!"
And, I have code/test/Vehicle.groovy
, with the following class definition:
package test;
public class Vehicle {
public static class Deserializer {
}
}
However, the following command fails:
groovy -cp code/ test.groovy
(groovy -v
reports 2.4.7)
I would expect it to succeed and print "Hello, world". Instead, I get:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/tmp/test.groovy: 4: unable to resolve class Vehicle.Deserializer
@ line 4, column 10.
def test=new Vehicle.Deserializer();
^
1 error
Since the script is not failing on the prior line, Groovy is finding the Vehicle
class without issue. It is just not finding the Deserializer
static class.
However, this script works just fine:
public class Vehicle {
public static class Deserializer {
}
}
def ok=new Vehicle();
def test=new Vehicle.Deserializer();
println "Hello, world!"
Is there something that I need to be doing to allow Groovy to work with static classes, when the static class (and its outer class) are defined in a separate Groovy file?
UPDATE: I found this issue and can confirm that Groovy can at least sort of see the Deserializer
class:
import test.Vehicle;
import static test.Vehicle.Deserializer;
println Deserializer.class.name
def ok=new Vehicle();
// def test=new Vehicle.Deserializer();
println "Hello, world!"
This works as expected:
test.Vehicle$Deserializer
Hello, world!
However, uncommenting the def test=new Vehicle.Deserializer();
still gives me the error, as does changing that to def test=new Deserializer();
(given the import static
).
Upvotes: 2
Views: 1864
Reputation: 1006744
It looks like this is a known Groovy bug. If your static class has a zero-arguments constructor, you can work around this via newInstance()
:
import test.Vehicle;
import static test.Vehicle.Deserializer;
def ok=new Vehicle();
def test=Deserializer.class.newInstance();
println "Hello, world!"
Upvotes: 1