我有以下注释:
@Target(ElementType.METHOD)
public @interface MyAnn {
}
Run Code Online (Sandbox Code Playgroud)
以及用以下注释的方法@MyAnn:
@MyAnn
Object myMehtod(Object x) {
...
}
Run Code Online (Sandbox Code Playgroud)
使用 Java 注释处理器,我得到的注释元素为:
Element annotatedElement // = myMehtod
Run Code Online (Sandbox Code Playgroud)
I\xe2\x80\x99m 尝试在物料清单 (BoM) 平台模块中定义所有依赖项,以便我的多模块项目中的其他模块可以使用相同的版本。除了 kapt 依赖项之外,一切正常。在那些我得到这个错误:
\n\nCould not determine the dependencies of task ':app:kaptDebugKotlin'.\n> Could not resolve all task dependencies for configuration ':app:kapt'.\n > Could not find com.google.dagger:dagger-compiler:.\n Required by:\n project :app\nRun Code Online (Sandbox Code Playgroud)\n\n例如,使用此平台 ( :bom) 模块:
plugins {\n id 'java-platform'\n}\ndependencies {\n constraints {\n api 'com.google.dagger:dagger:2.25.2'\n api 'com.google.dagger:dagger-compiler:2.25.2'\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n当我在应用程序模块中像这样使用它时,我收到该错误:
\n\ndependencies {\n implementation platform(project(':bom'))\n implementation 'com.google.dagger:dagger'\n kapt 'com.google.dagger:dagger-compiler'\n // ...\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如果我使用 .I\xe2\x80\x99m 会出现相同的错误annotationProcessor。kapt 'com.google.dagger:dagger-compiler:2.25.2'如果我像所有作品一样设置版本。
我究竟做错了什么?我可以将 BoM 用于kapt或 …
android gradle annotation-processing android-gradle-plugin kapt
考虑一下DirectMethodHandle$Holder班级。(它是 所返回的类之一Class.forName("java.lang.invoke.DirectMethodHandle").getDeclaredClasses(),记录为返回成员类。)
以下断言在 JDK 19 下失败:
assert Class.forName("java.lang.invoke.DirectMethodHandle$Holder").isMemberClass();
Run Code Online (Sandbox Code Playgroud)
为什么?它违反了第 8.5 条的哪条规则?
和一些评论者一样,我得出的结论是,这是一个非常奇怪的错误。例如,使用某些javax.annotation.processing和某些javax.lang.model.*类,以下断言不会失败:
assert NestingKind.MEMBER == ((TypeElement)processingEnvironment.getElementUtils().getTypeElement("java.lang.invoke.DirectMethodHandle.Holder")).getNestingKind();
Run Code Online (Sandbox Code Playgroud)
因此,支持编译器的机制的输出与反射机制的输出不一致。我已经针对 JDK 提交了一个错误。
我有一个带注释的类:
public class CacheMessageHolder<TestMessage> implements MessageHolder<TestMessage> {
protected @MessageHolderType TestMessage message;
@Override
@SendProtoAll (proto ="protoMessageClass", matchType=MatchType.PARTIAL)
public void setMessage( TestMessage msg) {
this.message = msg;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的注释处理器中,我想获得传递给setMessage方法的Object的getter方法列表,然后将该信息用于代码生成.
我扩展了ElementScanner6并设法获得一个似乎保存参数的VariableElement,但我不知道从哪里开始.
所以在这个例子中,我想在编译时获取TestMessage类中的所有方法.
有任何想法吗
我正在写一个注释处理器.我怎样才能获得数组的类型?
@MyAnnotation
int[] iArray;
@MyAnnotation
boolean[] bArray;
@MyAnnotation
FooClass[] fooArray;
Run Code Online (Sandbox Code Playgroud)
据我所知,我可以检查它是否是这样的数组:
if (element.asType().getKind() == TypeKind.ARRAY) {
// it's an array
// How to check if its an array of boolean or an array integer, etc.?
}
Run Code Online (Sandbox Code Playgroud)
我如何获得数组的类型?
基本上我迭代所有注释的元素@MyAnnotation,我将根据数组的类型对数组做一些特殊的事情,类似于:
for (Element element : enviroment.getElementsAnnotatedWith(MyAnnotation.class)) {
if (element.getKind() != ElementKind.FIELD)
continue;
if (element.asType().getKind() == TypeKind.ARRAY) {
// it's an array
// How to distinguish between array of boolean or an array integer, etc.?
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用java注释处理器在编译时生成其他类.使用gradle构建时它工作正常,但我无法让IntelliJ识别生成的类.每当我尝试在IntelliJ中构建项目时,它会错误地说它找不到引用生成的类的符号.同样,因为它不知道类,所以在编写使用类的代码时没有给我任何帮助,只是将它全部突出显示为错误.
我有两个兄弟模块:"处理器"模块实现注释处理器并定义注释."demo"模块只是一些JUnit测试来试用注释处理器.我可以在IntelliJ中构建"处理器"模块,但是"演示"模块给出了如上所述的错误.我有"处理器"作为"演示"模块的依赖,在"测试"范围(我也尝试过"编译"范围).
如何让IntelliJ自动识别类?
我已经能够通过在设置对话框"标注处理程序"下创建新的配置,该配置文件下移动"演示"模块,可实现批注处理该配置文件,并指定注解处理器的FQCN得到它建在"注释处理器"列表框下.
但是,实时代码帮助仍然无效,编辑只是告诉我它找不到类,这实际上更重要(因为我总是可以从gradle构建).
我正在尝试开始创建一个javax注释处理器,我现在从android工作室做它.我只需要我认为的gradle依赖.现在在gradle我有以下我尝试过:
provided 'javax.annotation:jsr250-api:1.0'
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试构建以下类时,它说它无法找到AbstractProcessor类:
class MyAnnotationProcessor extends AbstractProcessor{
}
Run Code Online (Sandbox Code Playgroud)
如何让它识别这门课?
这是确切的错误:
我的导入看起来像这样:
这是我的java版本:
$java -version
Picked up JAVA_TOOL_OPTIONS: -Xmx512m -Xms64m
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
Run Code Online (Sandbox Code Playgroud) 我们使用注释处理框架(Immutables)从接口生成Java类.
现在我必须从kotlin类访问这些生成的类.虽然在Java中这很好用,但Kotlin编译器却找不到它们.
这是maven配置:
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals> …Run Code Online (Sandbox Code Playgroud) 我正在编写自己的注释处理器,并尝试获取注释参数,如下面的处理方法中的代码所示:
roundEnv.getElementsAnnotatedWith(annotation).forEach {
val annotation = it.getAnnotation(annotation)
annotation.interfaces
}
Run Code Online (Sandbox Code Playgroud)
我得到的是An exception occurred: javax.lang.model.type.MirroredTypesException: Attempt to access Class objects for TypeMirrors []在构建期间。有人知道如何获取注释数据吗?
将 android studio 更新到 3.4 后,我遇到了 lombok 插件的问题。尽管在模型类内部,注释被识别并正确显示,但在活动中我使用的所有 getter 和 setter 函数都无法识别。我也可以构建项目,但在所有活动中,所有 getter 和 setter 函数都以红色显示。任何帮助深表感谢。
我尝试为 android-studio 启用注释处理器。我使用 lombok 作为网站中给出的依赖项,带有注释处理器。Gradle 版本是 3.4.0。
annotation-processing lombok android-studio-3.4 android-gradle-3.4.0