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

时间:2013-09-15 05:45:15

标签: java annotations javac checker-framework

在JDK8中添加的

The Checkers Framework个引用java.lang.annotation.ElementType.TYPE_USE。当我在JDK7下使用它时,我收到以下警告:

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

这是一个合理的警告,但我如何在我认为无害的情况下压制

2 个答案:

答案 0 :(得分:1)

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

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$000(MembersInjectorStore.java:34) ~[guice-3.0-no_aop.jar:na]
    at com.google.inject.internal.MembersInjectorStore$1.create(MembersInjectorStore.java:42) ~[guice-3.0-no_aop.jar:na]
    at com.google.inject.internal.MembersInjectorStore$1.create(MembersInjectorStore.java:39) ~[guice-3.0-no_aop.jar:na]
    at com.google.inject.internal.FailableCache$1.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

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

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

引起的

答案 1 :(得分:1)

如果出现此编译时错误:

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

然后您正在使用Java 7 JDK进行编译,但您的代码引用了仅在Java 8 JDK中定义的枚举常量。问题可能是您的代码使用引用枚举常量的库。特别是,Checker Framework参考ElementType.TYPE_USE附带的类型注释。您可以使用Checker Framework,但仍然可以按照Checker Framework手册“Class-file compatibility with Java 7”部分中的说明在Java 7 JVM中编译和运行代码。