Groovy和Maven:使用泛型编译CompileStatic

时间:2015-07-01 18:36:00

标签: maven generics groovy

Groovy中的简单泛型类

@CompileStatic
class Foo<T> { 
    T member
}

一个适用于双打列表的特定子类。很容易,对吧?

@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,这是一个简单的测试用例,当一个更复杂的类无法编译时出现错误“无法将类型T的值赋给double []类型的变量”。

1 个答案:

答案 0 :(得分:1)

解决:官方Groovy documentation on Maven integration说最新版本的groovy-batch编译器是2.3.7。那不是真的,2.4.3 is available

我发现的问题是针对2.3.7编译器的reported as a bug,并且该错误已在2.3.11和2.4.3中修复。