编译时注解(二)JavaPoet的使用

上一篇文章提到AbstractProcessor中生成java类,可以使用JavaPoet开源库进行编写。但是有个问题,addModifier提示无法找到Modifier,其实只要把
compile project(’:libprocess’)
改成
annotationProcessor project(’:libprocess’)
就可以了。
但是这么一来定义的MethodProcessor就找不到了,所以需要改变整个peoject的结构
这里有个例子
https://www.cnblogs.com/whoislcj/p/6168641.html
另外,JavaPoet的使用在这里
https://juejin.im/entry/58fefebf8d6d810058a610de/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java 中,注解是在编译处理的,因此要在编译修改属性上的注解内容,需要使用 Java 的 annotation processing 工具(APT)。 APT 是一种在编译扫描和处理 Java 注解的工具,它可以在编译期间生成新的 Java 代码,并将其编译成可执行的程序。APT 工具提供了许多 API,可以用来读取和修改源代码上的注解信息。 下面是一个简单的例子,演示如何在编译修改属性上的注解内容: ```java @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface MyAnnotation { String value(); } public class MyClass { @MyAnnotation("old value") private String myField; } public class MyProcessor extends AbstractProcessor { @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) { if (element.getKind() == ElementKind.FIELD) { VariableElement variableElement = (VariableElement) element; MyAnnotation annotation = variableElement.getAnnotation(MyAnnotation.class); String newValue = "new value"; MyAnnotation newAnnotation = new MyAnnotation() { @Override public String value() { return newValue; } @Override public Class<? extends Annotation> annotationType() { return MyAnnotation.class; } }; AnnotatedTypeMirror.AnnotatedDeclaredType fieldType = (AnnotatedTypeMirror.AnnotatedDeclaredType) variableElement.asType(); AnnotatedTypeMirror.AnnotatedDeclaredType annotatedFieldType = fieldType.replaceAnnotation(newAnnotation); variableElement.getEnclosingElement().getEnclosedElements().remove(variableElement); FieldSpec.Builder fieldBuilder = FieldSpec.builder(TypeName.get(annotatedFieldType.getUnderlyingType()), variableElement.getSimpleName().toString()); variableElement.getModifiers().forEach(modifier -> fieldBuilder.addModifiers(modifier)); fieldBuilder.addAnnotation(annotatedFieldType.getAnnotations().stream().map(AnnotationSpec::get).collect(Collectors.toList())); fieldBuilder.initializer("null"); FieldSpec fieldSpec = fieldBuilder.build(); JavaFile.builder(annotatedFieldType.getUnderlyingType().toString().replace('.', '/').replace('$', '/') + "Generated", fieldSpec) .build() .writeTo(processingEnv.getFiler()); } } return true; } } ``` 在这个例子中,`MyAnnotation` 是一个自定义的注解,`MyClass` 是一个包含注解属性的类。`MyProcessor` 是一个 APT 处理器,它会在编译扫描和处理带有 `@MyAnnotation` 注解的元素。在 `process` 方法中,我们使用 `roundEnv.getElementsAnnotatedWith` 方法获取所有带有 `@MyAnnotation` 注解的元素,然后判断元素类型是否为字段。如果是字段,我们就可以使用 `variableElement.getAnnotation` 方法获取字段上的注解对象,然后构造一个新的注解对象并使用 `replaceAnnotation` 方法替换原有的注解对象。最后,我们可以使用 `JavaFile.builder` 方法创建一个新的 Java 文件,并将修改后的字段写入该文件中。 需要注意的是,APT 处理器需要在编译自动运行,可以通过在 `build.gradle` 文件中配置以下内容来实现: ```groovy dependencies { compileOnly 'com.google.auto.service:auto-service:1.0-rc7' annotationProcessor 'your.package.MyProcessor' } sourceSets { main { java { srcDirs 'src/main/java', 'build/generated/source/apt/main' } } } compileJava.options.annotationProcessorPath = configurations.compileClasspath ``` 这个例子只是一个简单的示例,实际使用中可能需要更复杂的处理逻辑。但是,这个例子可以帮助你了解在编译修改注解内容的基本原理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值