Java 如何抑制“未知枚举常量”警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18809295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:15:30  来源:igfitidea点击:

How to suppress "unknown enum constant" warnings?

javaannotationsjavacchecker-framework

提问by Gili

The Checkers Frameworkreferences java.lang.annotation.ElementType.TYPE_USEwhich was added in JDK8. When I use it under JDK7, I get the following warning:

java.lang.annotation.ElementType.TYPE_USEJDK8 中添加的 Checkers Framework引用。当我在 JDK7 下使用它时,我收到以下警告:

unknown enum constant java.lang.annotation.ElementType.TYPE_USE

unknown enum constant java.lang.annotation.ElementType.TYPE_USE

This is a reasonable warning, but how do I suppress it for cases I believe are harmless?

这是一个合理的警告,但是对于我认为无害的情况,我该如何抑制它

采纳答案by Gili

It turns out there is no such thing as a harmless unknown enum constant. Once I got past the compiler warnings, I ran into exceptions at runtime:

事实证明,没有无害的未知枚举常量这样的东西。一旦我通过编译器警告,我在运行时遇到了异常:

java.lang.ArrayStoreException: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
    at sun.reflect.annotation.AnnotationParser.parseEnumArray(AnnotationParser.java:693) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:482) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:306) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:241) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70) ~[na:1.7.0_40]
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3168) ~[na:1.7.0_40]
    at java.lang.Class.getAnnotation(Class.java:3127) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:131) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:84) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:221) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88) ~[na:1.7.0_40]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70) ~[na:1.7.0_40]
    at java.lang.reflect.Method.declaredAnnotations(Method.java:714) ~[na:1.7.0_40]
    at java.lang.reflect.Method.getAnnotation(Method.java:700) ~[na:1.7.0_40]
    at com.google.inject.spi.InjectionPoint.getAtInject(InjectionPoint.java:466) ~[guice-3.0-no_aop.jar:na]
    at com.google.inject.spi.InjectionPoint.getInjectionPoints(InjectionPoint.java:664) ~[guice-3.0-no_aop.jar:na]
    at com.google.inject.spi.InjectionPoint.forInstanceMethodsAndFields(InjectionPoint.java:356) ~[guice-3.0-no_aop.jar:na]
    at com.google.inject.internal.MembersInjectorStore.createWithListeners(MembersInjectorStore.java:90) ~[guice-3.0-no_aop.jar:na]
    at com.google.inject.internal.MembersInjectorStore.access
  unknown enum constant java.lang.annotation.ElementType.TYPE_USE
0(MembersInjectorStore.java:34) ~[guice-3.0-no_aop.jar:na] at com.google.inject.internal.MembersInjectorStore.create(MembersInjectorStore.java:42) ~[guice-3.0-no_aop.jar:na] at com.google.inject.internal.MembersInjectorStore.create(MembersInjectorStore.java:39) ~[guice-3.0-no_aop.jar:na] at com.google.inject.internal.FailableCache.apply(FailableCache.java:39) ~[guice-3.0-no_aop.jar:na] at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549) ~[guice-3.0-no_aop.jar:na] ... 102 common frames omitted

Meaning, any code that uses java.lang.reflect.Method.getAnnotation()will fail at runtime.

意思是,任何使用的代码java.lang.reflect.Method.getAnnotation()都会在运行时失败。

In my case, this issue was caused by https://code.google.com/p/checker-framework/issues/detail?id=255

就我而言,此问题是由https://code.google.com/p/checker-framework/issues/detail?id=255引起的

回答by mernst

If you get this compile-time error:

如果您收到此编译时错误:

##代码##

then you are compiling using a Java 7 JDK, but your code references an enum constant that is only defined in the Java 8 JDK. The problem might be that your code uses a library that references the enum constant. In particular, the type annotations shipped with the Checker Framework reference ElementType.TYPE_USE. You can use the Checker Framework, but still compile and run your code in a Java 7 JVM, by following the instructions in section "Class-file compatibility with Java 7" of the Checker Framework manual.

那么您正在使用 Java 7 JDK 进行编译,但是您的代码引用了一个仅在 Java 8 JDK 中定义的枚举常量。问题可能在于您的代码使用了一个引用枚举常量的库。特别是,Checker Framework 附带的类型注释引用ElementType.TYPE_USE。您可以使用 Checker 框架,但仍可以按照 Checker 框架手册的“类文件与 Java 7 的兼容性”部分中的说明在 Java 7 JVM 中编译和运行您的代码。