【问题标题】:Retrofit2, maven project, Illegal reflective access warningRetrofit2,maven项目,非法反射访问警告
【发布时间】:2020-07-09 22:14:11
【问题描述】:

我刚刚开始了一个新的 maven 项目,并做了一个 Retrofit 客户端的简单实现。我收到以下警告:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by retrofit2.Platform (file:/C:/Users/Admin/.m2/repository/com/squareup/retrofit2/retrofit/2.8.1/retrofit-2.8.1.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of retrofit2.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Process finished with exit code 0

这里是代码

import retrofit2.Retrofit;
import retrofit2.SimpleXmlConverterFactory;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class RetrofitClient {

    private static Retrofit retrofit = null;
    private RetrofitClient() { }

    public static EndPoints getAPI(String baseUrl) {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(SimpleXmlConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
        return retrofit.create(EndPoints.class);
    }
}

界面很简单

import retrofit2.Call;
import retrofit2.http.GET;

public interface EndPoints {
    @GET("teststatuses")
    Call<String> testStatus();
}

调用看起来像这样:

EndPoints endPoints = RetrofitClient.getAPI("http://localhost:8080/");
Call<String> repos = endPoints.testStatus();
System.out.println(repos.execute());

该项目使用 SDK 11 运行 Java 语言级别 11

【问题讨论】:

    标签: java retrofit


    【解决方案1】:

    有一个关于它的 issue 归档,其中一位 Retrofit 维护者回复了:

    反射解决了 JDK 中的一个错误,该错误已在 14 中修复,但它仅用于默认方法。由于这只是一个警告,因此不会阻止您的电话正常工作。

    所以你的选择是要么

    1. 坚持使用 Retrofit 2.8.x,并且
      • 忽略警告,或者
      • 升级到 Java 14
    2. 降级到 Retrofit 2.7.*

    【讨论】:

    • 为什么 Retrofit 2.8.X 对 JDK >= 14 没有硬依赖,这会给您带来更好的错误?这在 Java 领域是不可能的吗?
    • 只是猜测:虽然这肯定是可能的,至少对于 Maven 和 Enforcer plugin,Retrofit 制造商可能只是不想从 Retrofit 2.8 中锁定
    • 谢谢sscuberth。很高兴知道。只是不使用JDK 14的用户如果不行Retrofit 2.8怎么能用呢?
    【解决方案2】:

    如果您在 Java 版本 9 和 13 之间,并且想要坚持使用 2.8 或更高版本的 Retrofit,您可以像这样运行您的 jar 文件:

    java --add-opens=java.base/java.lang.invoke=ALL_UNNAMED my_jar.jar
    

    请注意,这不适用于 Java 8,因此如果您确实需要,可以编写这样的解决方案(适用于 *nix):

    if java --add-opens 2>&1 | grep 'requires modules' >/dev/null; then
      java --add-opens=java.base/java.lang.invoke=ALL-UNNAMED -jar my_jar.jar
    else
      java -jar my_jar.jar
    fi
    

    如果您有兴趣进一步研究,可以查看the code that causes this

    我也发现这有一些很好的信息:https://blog.codefx.org/java/five-command-line-options-hack-java-module-system/

    如果您使用的是 Gradle,您还可以尝试添加默认 jvm 参数:How do I add default JVM arguments with Gradle。但是,这仍然无法为您提供一个可以正常工作的普通 jar 文件。

    【讨论】:

      【解决方案3】:

      今天我遇到了同样的错误。我将 Retrofit 2.8.1 导入到我的项目中,它出现了。我尝试了所有方法,但有一件事有所帮助 - 我将 Retrofit 版本更改为 2.7.2,现在一切正常。祝你好运!) 如果你使用 maven:

      <dependency>
          <groupId>com.squareup.retrofit2</groupId>
          <artifactId>retrofit</artifactId>
          <version>2.7.2</version>
      </dependency>
      

      分级:

      compile group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.7.2'
      

      【讨论】:

        【解决方案4】:

        我已将此块添加到我的模块 build.gradle 文件中。

        分级

        tasks.withType(Test) {
          /**
           * fix for retrofit https://github.com/square/retrofit/issues/3341
           */
          jvmArgs = ["--add-opens", "java.base/java.lang.invoke=ALL-UNNAMED"]
        }
        

        Kotlin DSL

        tasks.withType<Test> {
          /**
           * fix for retrofit https://github.com/square/retrofit/issues/3341
           */
          jvmArgs = listOf("--add-opens", "java.base/java.lang.invoke=ALL-UNNAMED")
        }
        

        并且警告不再显示。我认为以这种方式删除它是安全的,因为它只影响测试任务

        【讨论】:

          【解决方案5】:

          使用版本时要小心。 降级到 Retrofit 2.7.* 为我解决了这个问题

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-05-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多