Groovy 属性的类型应为 'java.lang.Integer',但找到的类型为 'java.lang.Object'
Groovy Attribute should have type 'java.lang.Integer', but found type 'java.lang.Object'
我有以下 JUnit 测试:
public class JavaTest {
final int value = 2;
@Test
@Repeat(times = value)
public void test() {
fail("Not yet implemented");
}
}
@Repeat
注释来自 easytest-core, and the exact definition is here。
当我将其编译为 java 源代码时,一切都可以正常构建(并运行)。当我编译与 groovy 源完全相同的东西时,我得到:
Groovy:Attribute 'times' should have type 'java.lang.Integer'; but found type 'java.lang.Object' in @org.easetech.easytest.annotation.Repeat GroovyTest.groovy
在网上搜索后,我在 SO and jira.codehaus 上找到了一些类似的讨论,但是那些讨论的是 String - GString 问题,所以这些解决方案对我不起作用。
我该如何解决这个问题?
更新:
- java.version=1.7.0_76
- groovy.version=2.3.7
认为您遇到了这样一个事实 groovyc 不像 javac 那样将最终变量视为内联常量
我试过像这样更改您的 int 变量:
final Integer value = Integer.valueOf(2).intValue()
这可以防止变量被视为内联常量。更改之后,我从 @Repeat
注释中得到一个编译错误:
Expected Integer.valueOf(2).intValue() to be an inline constant
似乎在 Groovy JIRA 中承认了这里的不一致:https://issues.apache.org/jira/browse/GROOVY-1628
在此 SO 线程中还有一些进一步的讨论:
Does it make sense to mark variable as final in groovy?
您似乎无法获得 groovy 来匹配此场景的 Java 行为。
我有以下 JUnit 测试:
public class JavaTest {
final int value = 2;
@Test
@Repeat(times = value)
public void test() {
fail("Not yet implemented");
}
}
@Repeat
注释来自 easytest-core, and the exact definition is here。
当我将其编译为 java 源代码时,一切都可以正常构建(并运行)。当我编译与 groovy 源完全相同的东西时,我得到:
Groovy:Attribute 'times' should have type 'java.lang.Integer'; but found type 'java.lang.Object' in @org.easetech.easytest.annotation.Repeat GroovyTest.groovy
在网上搜索后,我在 SO and jira.codehaus 上找到了一些类似的讨论,但是那些讨论的是 String - GString 问题,所以这些解决方案对我不起作用。
我该如何解决这个问题?
更新:
- java.version=1.7.0_76
- groovy.version=2.3.7
认为您遇到了这样一个事实 groovyc 不像 javac 那样将最终变量视为内联常量
我试过像这样更改您的 int 变量:
final Integer value = Integer.valueOf(2).intValue()
这可以防止变量被视为内联常量。更改之后,我从 @Repeat
注释中得到一个编译错误:
Expected Integer.valueOf(2).intValue() to be an inline constant
似乎在 Groovy JIRA 中承认了这里的不一致:https://issues.apache.org/jira/browse/GROOVY-1628
在此 SO 线程中还有一些进一步的讨论: Does it make sense to mark variable as final in groovy?
您似乎无法获得 groovy 来匹配此场景的 Java 行为。