我有
@ColumnMetadata(index=1)
...
@ColumnMetadata(index=2)
...
@ColumnMetadata(index=3)
...
Run Code Online (Sandbox Code Playgroud)
我必须使用APT检查索引号是否是唯一的.我不知道该怎么做.我不懂教程,一般我在网上找材料有问题.
这该怎么做?有关APT的任何教程/任何内容?
我想测试我的实用程序类 ElementUtils,但我不知道如何将类作为 Element。在 AnnotationProcessors 中,我使用以下代码获取元素
Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(annotation);
Run Code Online (Sandbox Code Playgroud)
但由于 RoundEnvironment 在测试中不可用,因此这不是一个选项。
有没有办法将类作为 javax.lang.model.element.Element 获取?
我创建了一个 java 注释,将我的一些“服务”类标记为服务,然后编写了一个注释处理器来生成相应的 servicerequest 和 servicehandler 类。这是一个 GWT maven 项目,其中服务请求被发送到服务器到中央调度程序,由各种服务处理。
我的maven项目由一个父项目和5个子模块项目组成。
项目结构是这样的:
我的问题是这样的。
让注释处理器从一个项目(moodleWSAPI)读取和处理注释并在另一个项目(moodleGenerateServiceHandlers)中生成java源文件的规定方法是什么?
我似乎有很多注释处理器的例子为运行注释处理器的同一项目生成java源文件,但不为不同的项目生成java源文件。
我已经成功构建并测试了我的注释处理器,但我不知道如何让它在处理器运行的项目之外生成文件。目前它在moodleWSAPI中生成java源文件(这是错误的) 。我希望它处理moodleWSAPI中的源文件,然后在moodleGenerateServiceHandlers中输出新生成的源文件。
这是我必须创建实际的 java 源文件的代码片段..
public void createSourceFileFromTemplate(Template template, ClassSignature classSignature, VelocityContext context, Element element) {
PrintWriter pw = null;
try {
JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(classSignature.getFullName(), classSignature.getElement());
OutputStream fileStream = sourceFile.openOutputStream();
pw = new PrintWriter(fileStream);
template.merge(context, pw);
} catch (Exception e) {
e.printStackTrace();
if (element!=null) {
processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(),element);
} else { …Run Code Online (Sandbox Code Playgroud) 有许多功能已经可用Jeta,但如果缺少什么会怎么样.我可以创建自己的注释并为它们生成元代码吗?
需要一步一步的教程如何创建自定义Jeta处理器.
我想生成一个使用 JavaPoet 扩展其他类的类。
例如我有这个类:
@MyAnnotation
public class ClassA {
public ClassA(String paramA, int paramB) {
// some code
}
}
Run Code Online (Sandbox Code Playgroud)
我想生成这样的新类:
public class Generated_ClassA extends ClassA {
public Generated_ClassA (String paramA, int paramB) {
super(paramA, paramB);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我在 JavaPoet 中没有看到任何现成的 API 来创建调用超类构造函数的构造函数。如何做到这一点以及最佳实践是什么?
我怎样才能获得package name,generic type并且Parametrized type从type从field element在注释处理器?
说,如果Element.asType回来java.util.List<String>,我想得到
java.utilList<E>或原始类型List(最好是原始类型)String有没有方法element utils,type utils?
我想知道是否有一种方法可以将注释应用于函数并在注释处理期间访问这些函数的主体。如果无法通过检查Element注释处理器中的对象来直接获取方法主体,是否有其他方法可以访问应用这些注释的函数的源代码?
作为我正在从事的项目的一部分,我尝试使用 kapt 来检查使用特定类型注释进行注释的 Kotlin 函数,并基于它们生成类。例如,给定一个带注释的函数,如下所示:
@ElementaryNode
fun addTwoNumbers(x: Int, y: Int) = x + y
Run Code Online (Sandbox Code Playgroud)
我的注释处理器当前生成以下内容:
class AddTwoNumbers : Node {
val x: InputPort<Int> = TODO("implement node port property")
val y: InputPort<Int> = TODO("implement node port property")
val output: OutputPort<Int> = TODO("implement node port property")
}
Run Code Online (Sandbox Code Playgroud)
但是,我需要将原始函数本身包含在此类中,本质上就像将其作为私有函数复制/粘贴一样:
class AddTwoNumbers : Node {
val x: InputPort<Int> = TODO("implement node port property")
val y: InputPort<Int> = TODO("implement node port property")
val output: OutputPort<Int> = TODO("implement node port …Run Code Online (Sandbox Code Playgroud) 我正在尝试将Maven BOM与gradle 5.1.1一起使用,如下所述
ext {
set('spring-boot-dependencies.version', '2.1.2.RELEASE')
}
apply plugin: 'java'
group 'com.acme'
version '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
// maven bom
implementation platform("org.springframework.boot:spring-boot-dependencies:${project.'spring-boot-dependencies.version'}")
compileOnly('org.projectlombok:lombok')
annotationProcessor('org.projectlombok:lombok')
}
Run Code Online (Sandbox Code Playgroud)
当我运行./gradlew dependencies --configuration=annotationProcessor&时./gradlew dependencies --configuration=compileOnly,我分别得到以下内容
annotationProcessor - Annotation processors and their dependencies for source set 'main'.
\--- org.projectlombok:lombok FAILED
Run Code Online (Sandbox Code Playgroud)
compileOnly - Compile only dependencies for source set 'main'.
+--- org.projectlombok:lombok FAILED
Run Code Online (Sandbox Code Playgroud)
奇怪的是,IntelliJ可以compileOnly正确解决依赖关系,但不能解决annotationProcessor
我对所发生的事情很困惑。任何帮助表示赞赏
我正在开发一个带有注释处理的代码生成器,并将逻辑分为两个步骤:
注解处理APITypeMirror在大多数情况下为我们提供了。从我在网上看到的情况来看,TypeElement在数据模型中使用似乎是一个很好的实践(可以从 获得TypeMirror)。
然而,JavaPoet 在大多数情况下TypeMirror直接接受 s,并且将它们转换为TypeElement数据模型似乎没有必要。
数据模型与另一种相比是否有优势?
我尝试使用Maven和androidannotations来构建我的apk,但独立于任何IDE(我实际上使用IntelliJ IDEA而不是Eclipse,但我希望它完全独立于IDE,因此它也可以在任何IDE上完美运行构建服务器).
注释似乎得到了正确处理,但它们没有被编译到apk中,这是我目前所处的位置.
我尝试使用中的<includes>部分maven-compiler-plugin,并且路径应该是正确的 - 它存在并且还包含已处理,生成的注释,java类,这是Android主要活动但带有下划线(_)后缀.
有一个wiki页面描述如何使用Maven + Eclipse,但是它太多绑定到Eclipse IDE了.https://github.com/excilys/androidannotations/wiki/Building-Project-Maven-Eclipse 所以它无法帮助我解决问题.
这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>de-mycompany-myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>com-mycompany-base-myproject</name>
<prerequisites>
<maven>2.2.1</maven>
</prerequisites>
<properties>
<platform.version>2.3.3</platform.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${platform.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.googlecode.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<version>2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<classifier>api</classifier>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/res</resourceDirectory>
<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
<sdk>
<platform>10</platform> …Run Code Online (Sandbox Code Playgroud) android annotations maven annotation-processing android-annotations