我有以下Groovy腳本爲test.groovy
:的Groovy無法解析靜態類
import test.Vehicle
def ok=new Vehicle();
def test=new Vehicle.Deserializer();
println "Hello, world!"
而且,我有code/test/Vehicle.groovy
,用下面的類定義:
package test;
public class Vehicle {
public static class Deserializer {
}
}
但是,下面的命令將失敗:
groovy -cp code/ test.groovy
(groovy -v
報告2.4.7 )
我期望它成功並打印出「Hello,world」。相反,我得到:
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
因爲腳本是不是在之前線失敗,Groovy的是找到Vehicle
類沒有問題。它只是找不到靜態類。
然而,這個腳本工作得很好:
public class Vehicle {
public static class Deserializer {
}
}
def ok=new Vehicle();
def test=new Vehicle.Deserializer();
println "Hello, world!"
是否有我需要做允許的Groovy與靜態類,當靜態類(和它的外部類)中定義工作的東西一個單獨的Groovy文件?
更新:我發現this issue,並可以確認的Groovy至少可以有點看Deserializer
類:
import test.Vehicle;
import static test.Vehicle.Deserializer;
println Deserializer.class.name
def ok=new Vehicle();
// def test=new Vehicle.Deserializer();
println "Hello, world!"
這按預期工作:
test.Vehicle$Deserializer
Hello, world!
然而,取消註釋def test=new Vehicle.Deserializer();
仍然會給我帶來錯誤,因爲會將此錯誤更改爲def test=new Deserializer();
(給定import static
)。
我沒有測試過自己,但你試過導入靜態類明確,以及在'test.groovy'爲'進口靜態測試.Vehicle.Deserializer'? – dmahapatro
@dmahapatro:'import test.Vehicle.Deserializer'導致第二個「無法解析類」錯誤,指向該行。 '輸入靜態test.Vehicle.Deserializer',奇怪的是,*不會*導致第二個錯誤,但它不會清除第一個錯誤。 – CommonsWare
是的,目擊者也一樣。在執行'test.groovy'之前,我還試圖通過'groovyc'編譯'Vehicle.groovy',但我仍然看到它抱怨。這可能是一個問題。 – dmahapatro