简体   繁体   English

如何确定注释处理器中被注释元素的类路径?

[英]How to determine the classpath of an annotated element in an annotation processor?

I have the annotation @Foo and the corresponding FooProcessor. 我有注解@Foo和相应的FooProcessor。 There I will need the location of the project root, which I could parse from the location of the class/java file, if I figured out how to get it. 在那里,我将需要项目根目录的位置,如果我想出了如何获得的话,可以从class / java文件的位置进行解析。

Eg. 例如。

  ...
   public class FooProcessor extends AbstractProcessor{
       ...
       void processElement(Element e, Foo foo){
           File f = getClassFileOrWhatever(e.getEnclosingElement());
           File projectRoot = getProjectRoot(f);
           doSomething(projectRoot);
       }
       File getClassFileOrWhatever(Element e){
           ???
       }
       ...
    }

and in a different project which has a jar containing Foo annotation and it's processor: 在另一个项目中,该项目具有一个包含Foo注释的jar和它的处理器:

/this/part/might/change/someproject/src/Foobar.java /this/part/might/change/someproject/src/Foobar.java

public class Foobar{
    @Foo public void foobar(){}
}

=> I need FooProcessor::getProjectRoot to return "/this/part/might/change/someproject/" which is obviously trivial if I have "/this/part/might/change/someproject/src/Foobar.java" from FooProcessor::getClassFileOrWhatever. =>我需要FooProcessor :: getProjectRoot返回“ / this / part / might / change / someproject /”,如果我从FooProcessor获得“ /this/part/might/change/someproject/src/Foobar.java”,这显然是微不足道的:: getClassFileOrWhatever。

It seems like new File(".") refers to my home dir when I compile the class with annotated method with Eclipse. 当我在Eclipse中使用带注释的方法编译类时,似乎new File(“。”)指向我的主目录。 I could walk the file tree from there and hope that the class file denoted by element.getEnclosingElement() is somewhere out there. 我可以从那里浏览文件树,并希望由element.getEnclosingElement()表示的类文件在那里。 This however seems a) very slow and b) error prone due to different compiling environments. 但是,由于不同的编译环境,这似乎a)非常慢,b)容易出错。

This did the trick: 这达到了目的:

 FooProcessor.class.getProtectionDomain().getCodeSource().getLocation().toString();

This gave me essentially the path to the jar file used, which I could use to figure out the project root in my case. 这从本质上给了我所用jar文件的路径,在我的案例中,我可以用它来确定项目的根目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在注释处理器中获取注释方法的返回类型? - How to get return type of annotated method in annotation processor? 如何确保注释处理器始终应用于所有带注释的元素? - How do sure that the annotation processor is always applied to all annotated elements? Java注释处理器是否能够删除注释的代码 - Is Java annotation processor capable of removing annotated code 无法在注释处理器中加载资源(不在类路径上) - Cannot load resources in Annotation Processor (Not on classpath) 如何在 Java 11 的注释处理器中获取带注释的方法参数的名称 - How can I get the names of annotated method parameters in an Annotation Processor in Java 11 Java批注以确定被批注的方法是否执行 - Java annotation to determine whether the annotated method executes Java 注释处理器 - 带注释的 Kotlin 类单元测试 - Java annotation processor - Annotated Kotlin classes unit tests 从注释处理器中的注释 Object 中获取 Class 字段 - Get Class fields from annotated Object in annotation processor Java / Kotlin注释处理器:获取带注释的字段/属性的类型 - Java/Kotlin annotation processor: get type of annotated field/property 使用Java注释处理器查找带注释方法的方法参数? - Find Method Arguments of annotated Method using Java Annotation Processor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM