You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Groovy 4.0.x编译包含静态方法的接口?

Groovy 4.0.x 允许接口包含静态方法的解决方案

直接修改编译器目标版本(推荐方案)

Groovy 4.0.x 默认以JDK 8为兼容目标,而JDK 8不支持接口静态方法,只要将编译目标指定为JDK 11及以上,就能解决这个编译报错问题。

命令行编译时

直接在groovyc命令中加入--release或--target参数指定版本:

groovyc --release 11 YourInterface.groovy

Gradle 构建配置

在项目的build.gradle中配置Groovy插件的编译参数:

plugins {
    id 'groovy'
}

groovy {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
    // 额外指定release参数确保编译行为对齐JDK11
    options.compilerArgs += ['--release', '11']
}

Maven 构建配置

使用gmavenplus-plugin时,在pom.xml中添加release配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.gmavenplus</groupId>
            <artifactId>gmavenplus-plugin</artifactId>
            <version>1.13.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
    </plugins>
</build>

AST转换方案(不推荐)

其实完全没必要用AST转换来解决这个问题——调整编译参数已经是最直接且符合规范的方式。如果硬要实现的话,你可以编写AST转换将接口中的静态方法提取到一个同名的辅助类中,但这会增加不必要的复杂度,违背了JDK11+原生支持接口静态方法的设计初衷。

内容的提问来源于stack exchange,提问作者Choosechee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:34:57