Binary/Qualified 名字错了吗?开头为:<any?>$
Binary/Qualified name is wrong? Begins with: <any?>$
我是运行一个自己写的注解处理器。它 运行 在 JDK 8 上很好,现在我在 JDK 12 上遇到问题。
我有一个 TypeElement
,我想检索它的二进制名称以传递给 Class.forName
。
我使用 javax.lang.model.util.Elements.getBinaryName(TypeElement)
并且它 returns 是一个垃圾值 <any?>$OuterClass.InnerClass
而不是预期的 example3.OuterClass$InnerClass
.
我试图用 TypeElement.getQualifiedName
替换 getBinaryName
(即使它对内部 class 不太有效)但它给了我同样的垃圾结果。我曾尝试搜索此问题,但大多数搜索引擎都会删除所有特殊字符并给我无用的结果。
TypeElement
是通过捕获 MirroredTypeException
获得的,如下所示:
try {
exampleAnnotation.value();
throw new IllegalStateException("Expected a MirroredTypeException.");
} catch (MirroredTypeException ex) {
return (TypeElement) types.asElement(ex.getTypeMirror());
}
这里是 ExampleAnnotation
的定义:
package example1;
@Target(PACKAGE)
@Retention(RUNTIME)
@Documented
public @interface ExampleAnnotation {
Class<? extends Derived> value() default Derived.class;
interface Derived<A extends Annotation> extends Base<A> {
String foo();
}
}
这是处理器在 package-info.java
中访问的注释实例:
@ExampleAnnotation(OuterClass.InnerClass.class)
package example2;
import example1.ExampleAnnotation;
我也尝试了完全限定名称 example3.OuterClass.InnerClass.class
但这也会导致垃圾:<any?>$example3.OuterClass.InnerClass
.
我怀疑这是否重要,但注释处理器仍标记为 @SupportedSourceVersion(SourceVersion.RELEASE_8)
,我在 Gradle 5.3.1.
上 运行
我已验证处理器路径包含包 example1 和 example3 的 jar,包括注释处理器。
我没有对模块系统进行任何更改,所以我想这可能会以某种方式影响代码。
刚刚尝试创建一个 Maven 项目,目前无法重现该问题,因此我的 Gradle 配置可能存在问题,类似于@Colin Alworth 所建议的。
我最近升级到 Gradle 的新版本并开始使用 "annotationProcessor" 依赖项。
如果 class 不在 [=11] 上,<any?>$
似乎会添加到 binary/qualified class 名称前面(如来源中所示) =](或者如果它不是导入的,或者拼写错误)。我在 processorpath
.
上只有注释的 jar
为了提醒我的注释处理器的消费者注意这个错误,我能够在捕获 MirroredTypeException
.
后立即通过比较 TypeElement.asType().getKind() == TypeKind.ERROR
来检测它
我是运行一个自己写的注解处理器。它 运行 在 JDK 8 上很好,现在我在 JDK 12 上遇到问题。
我有一个 TypeElement
,我想检索它的二进制名称以传递给 Class.forName
。
我使用 javax.lang.model.util.Elements.getBinaryName(TypeElement)
并且它 returns 是一个垃圾值 <any?>$OuterClass.InnerClass
而不是预期的 example3.OuterClass$InnerClass
.
我试图用 TypeElement.getQualifiedName
替换 getBinaryName
(即使它对内部 class 不太有效)但它给了我同样的垃圾结果。我曾尝试搜索此问题,但大多数搜索引擎都会删除所有特殊字符并给我无用的结果。
TypeElement
是通过捕获 MirroredTypeException
获得的,如下所示:
try {
exampleAnnotation.value();
throw new IllegalStateException("Expected a MirroredTypeException.");
} catch (MirroredTypeException ex) {
return (TypeElement) types.asElement(ex.getTypeMirror());
}
这里是 ExampleAnnotation
的定义:
package example1;
@Target(PACKAGE)
@Retention(RUNTIME)
@Documented
public @interface ExampleAnnotation {
Class<? extends Derived> value() default Derived.class;
interface Derived<A extends Annotation> extends Base<A> {
String foo();
}
}
这是处理器在 package-info.java
中访问的注释实例:
@ExampleAnnotation(OuterClass.InnerClass.class)
package example2;
import example1.ExampleAnnotation;
我也尝试了完全限定名称 example3.OuterClass.InnerClass.class
但这也会导致垃圾:<any?>$example3.OuterClass.InnerClass
.
我怀疑这是否重要,但注释处理器仍标记为 @SupportedSourceVersion(SourceVersion.RELEASE_8)
,我在 Gradle 5.3.1.
我已验证处理器路径包含包 example1 和 example3 的 jar,包括注释处理器。
我没有对模块系统进行任何更改,所以我想这可能会以某种方式影响代码。
刚刚尝试创建一个 Maven 项目,目前无法重现该问题,因此我的 Gradle 配置可能存在问题,类似于@Colin Alworth 所建议的。
我最近升级到 Gradle 的新版本并开始使用 "annotationProcessor" 依赖项。
如果 class 不在 [=11] 上,<any?>$
似乎会添加到 binary/qualified class 名称前面(如来源中所示) =](或者如果它不是导入的,或者拼写错误)。我在 processorpath
.
为了提醒我的注释处理器的消费者注意这个错误,我能够在捕获 MirroredTypeException
.
TypeElement.asType().getKind() == TypeKind.ERROR
来检测它