Java 注释处理(自 Java 6 起)是一个非常好的概念,因为它允许通过Element接口(和其他接口)访问有关类和方法的大量信息。
但遗憾的是,我不得不凭经验发现,非注释类永远不会传递给自定义注释处理器:
warning: No SupportedAnnotationTypes annotation found on
my.TESTProcessor, returning an empty set.
Run Code Online (Sandbox Code Playgroud)
我的发现是真的吗?或者我可以“欺骗”编译器以提供有关非注释类的自定义注释处理器信息吗?
我正在为 Eclipse 构建一个注释处理器插件,我想做的是在处理过程中检查项目文件夹内的几个文件。
我想知道如何从我的处理器中获取项目路径。我相信这是可以完成的,因为项目源路径已提供给处理器 - 但我找不到到达它的方法。
我尝试查看 System.properties 和processingEnv.getOptions(),但那里没有有用的信息。
最终我也想在 Netbeans 上使用这个注释处理器,所以如果有一个公共 API 可以提供这些信息,那将是最好的 - 但任何帮助将不胜感激。
我有一节课:
public class StartPagePresenter extends AbstractPresenter<String> {
...
}
Run Code Online (Sandbox Code Playgroud)
使用Java Annotation Processing我得到TypeElement了类:
TypeElement startPagePresenterType = // get the TypeElement of StartPagePresenter
Run Code Online (Sandbox Code Playgroud)
现在我需要获得完成的超类:
startPagePresenterType.getSuperclass();
Run Code Online (Sandbox Code Playgroud)
然后我试着检查超类是否具有正确的类型:
if ( !startPagePresenterType.getSuperclass().toString().equals(
AbstractPresenter.class.getCanonicalName()) ) {
...
}
Run Code Online (Sandbox Code Playgroud)
这是问题所在:AbstractPresenter.class.getCanonicalName()导致:
core.mvp.AbstractPresenter
Run Code Online (Sandbox Code Playgroud)
并startPagePresenterType.getSuperclass().toString()导致:
core.mvp.AbstractPresenter<java.lang.String>
Run Code Online (Sandbox Code Playgroud)
当你比较这些字符串时,它们永远不会相等,尽管超类是相同的.
如何在startPagePresenterType.getSuperclass()没有通用块的情况下获取超类?
我试图让 intellij 使用各种其他插件与我的功能性 gradle build sans 一起玩。我使用的各种 插件破坏了我需要使用的另一个插件的支持(它在默认位置寻找生成的源以及其他问题)。我已经在这个问题上工作了一段时间并且取得了轻微的成功,但总是有警告。许多插件与测试目录中的 apt 不兼容,等等。
启用注释处理器并将输出目录设置为 /build/classes/main 和 /build/classes/test 不会产生结果,尽管这是 gradle 插件似乎放置通过包含生成的 .java 和 .class 文件的地方dagger2 编译器。
通过创造性地调整模块配置中生成的源集参数,我可以让所有东西都玩得很好,但这永远不会存在。即如果您关闭应用程序,您必须重新配置选项。
任何帮助,将不胜感激。
我有一个这样的界面:
interface MyInterface<V>{
}
Run Code Online (Sandbox Code Playgroud)
例如,我所有的带注释的类都以@MyAnnotation不同的方式实现了这个接口。
//first way
Class1 implement MyInterface<SomeClass>
//second way
AbstractClass<V> implement MyInterface<V>
Class2 extends AbstractClass<SomeClass>
//third way
ConcreteClass implement MyInterface<SomeClass>
Class3 extends ConcreteClass
Run Code Online (Sandbox Code Playgroud)
好吧,我有TypeElement1,2 和 3 类,我想找到类型变量的限定名称V。
我试过这个,但它返回V而不是SomeClass.
TypeElement class1 = ...
while(reachToMyInterface){
for (TypeMirror m : ((DeclaredType) class1.asType()).getTypeArguments()) {
print(m.toString()) // prints V
}
class1 = getItsSuperClass();
}
Run Code Online (Sandbox Code Playgroud)
编辑:这种方法也有同样的问题:
for (Element element : roundEnv.getElementsAnnotatedWith(Haha.class)) {
if (element instanceof TypeElement) {
TypeElement te = (TypeElement) element;
TypeElement …Run Code Online (Sandbox Code Playgroud) 有人知道如何使用插件编译器使用Android Annotations吗?
在这里我的app/build.gradle和这里我的项目build.gradle
使用此配置,我在构建项目时出现此错误消息:
Error:Could not get unknown property 'classpath' for task ':app:transformJackWithJackForDebug' of type com.android.build.gradle.internal.pipeline.TransformTask.
Run Code Online (Sandbox Code Playgroud) android gradle annotation-processing jack android-annotations
我正在尝试实施AnnotationProcessor. 我正在关注本教程。
我能够调试注释处理器,并且调试器转到方法:init, getSupportedAnnotationTypes, getSupportedOptions,getSupportedSourceVersion但它不会转到process方法。
我无法在互联网上找到有用的东西。你能告诉我为什么会发生这种情况以及如何解决吗?
谢谢你。
我使用EclipseIDE 开发了一个使用的应用程序,Mapstruct我现在正在IntelliJ继续开发。
在 Eclipse 上一切正常,但由于使用了annotationProcessorPaths.
我的配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=${mapstruct.defaultComponentModel}
</compilerArg>
<compilerArg>
-Aorg.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
</compilerArg>
</compilerArgs>
</configuration>
Run Code Online (Sandbox Code Playgroud)
在 IntelliJ 上,当我启动 Maven 全新安装时,我得到了生成的源代码:
@Component
public class FieldMapperImpl implements FieldMapper {
@Autowired
private FieldMapperResolver fieldMapperResolver;
...
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行/调试我的 Spring Boot 应用程序时,我得到的生成源是:
public class FieldMapperImpl implements FieldMapper {
private final FieldMapperResolver fieldMapperResolver = new FieldMapperResolver();
...
}
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题 ?
需要注释处理器方面的帮助。我创建了一个简单的注释处理器,它使用 @autoservice 注释来检查被注释的字段是否是最终的。但它没有显示任何编译时错误。这是我的配置
注解:
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface Store {
int temp() default 0;
}
Run Code Online (Sandbox Code Playgroud)
注释处理器:
@SupportedAnnotationTypes("com.self.Store")
@AutoService(Processor.class)
public class Process extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(Store.class)) {
TypeElement typeElement = (TypeElement) element;
for (Element element2 : typeElement.getEnclosedElements()) {
VariableElement variableElement = (VariableElement) element2;
if (!variableElement.getModifiers().contains(Modifier.FINAL)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "it should be final");
}
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
pom文件:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>annotations</groupId>
<artifactId>annotations</artifactId> …Run Code Online (Sandbox Code Playgroud) 是否可以ServiceLoader在init(ProcessingEnvironment)注释处理器的方法中使用?
interface Service {}
class AnnotationProcessor extends AbstractProcessor {
public static void main(String[] args) {
ServiceLoader<Service> loader = ServiceLoader.load(Service.class);
System.out.println("Found Services:");
for (Service service : loader) {
System.out.println(service);
}
}
@Override
public synchronized void init(ProcessingEnvironment env) {
super.init(env);
ServiceLoader<Service> loader = ServiceLoader.load(Service.class);
System.out.println("Found Services:");
for (Service service : loader) {
System.out.println(service);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
运行main方法会生成我在META-INF/services文件中指定的服务.但是,当该init(ProcessingEnvironment)方法作为另一个项目的构建的一部分调用时,它不会列出任何服务.
有没有办法让这项工作?