Groovy 和 Maven:使用泛型的 CompileStatic
Groovy and Maven: CompileStatic with generics
Groovy
中的简单泛型 class
@CompileStatic
class Foo<T> {
T member
}
以及适用于双打列表的特定子class。够简单吧?
@CompileStatic
class DoubleListFoo extends Foo<List<Double>> {
void bar() {
println member.size()
}
}
不会在 Maven 中编译:
[ERROR] DoubleListFoo.groovy:[12,9] 3. ERROR in DoubleListFoo.groovy (at line 12)
[ERROR] println member.size()
[ERROR] ^^^^^^^^^^^^^
[ERROR] Groovy:[Static type checking] - Cannot find matching method java.lang.Object#size(). Please check if the declared type is right and if the method exists.
在 IntelliJ 中编译得很好。怎么回事?
Maven 版本:
mvn -version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
Maven home: /usr/local/apache-maven-3.2.3
Java version: 1.7.0_71, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"
Groovy版本:
groovyc -version
Groovy compiler version 2.4.3
Copyright 2003-2013 The Codehaus. http://groovy.codehaus.org/
FWIW,这是我在更复杂的 class 无法编译并出现错误 "Cannot assign value of type T to variable of type double[]".
时创建的一个简单测试用例
已解决:官方Groovy documentation on Maven integration says the latest available version of the groovy-batch compiler is 2.3.7. That's not true, 2.4.3 is available.
我发现的问题是 reported as a bug 针对 2.3.7 编译器,该错误已在 2.3.11 和 2.4.3 中修复。
Groovy
中的简单泛型 class@CompileStatic
class Foo<T> {
T member
}
以及适用于双打列表的特定子class。够简单吧?
@CompileStatic
class DoubleListFoo extends Foo<List<Double>> {
void bar() {
println member.size()
}
}
不会在 Maven 中编译:
[ERROR] DoubleListFoo.groovy:[12,9] 3. ERROR in DoubleListFoo.groovy (at line 12)
[ERROR] println member.size()
[ERROR] ^^^^^^^^^^^^^
[ERROR] Groovy:[Static type checking] - Cannot find matching method java.lang.Object#size(). Please check if the declared type is right and if the method exists.
在 IntelliJ 中编译得很好。怎么回事?
Maven 版本:
mvn -version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
Maven home: /usr/local/apache-maven-3.2.3
Java version: 1.7.0_71, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.5", arch: "x86_64", family: "mac"
Groovy版本:
groovyc -version
Groovy compiler version 2.4.3
Copyright 2003-2013 The Codehaus. http://groovy.codehaus.org/
FWIW,这是我在更复杂的 class 无法编译并出现错误 "Cannot assign value of type T to variable of type double[]".
时创建的一个简单测试用例已解决:官方Groovy documentation on Maven integration says the latest available version of the groovy-batch compiler is 2.3.7. That's not true, 2.4.3 is available.
我发现的问题是 reported as a bug 针对 2.3.7 编译器,该错误已在 2.3.11 和 2.4.3 中修复。