问题
I am writing an annotation processor in Java and in this annotation processor I want to be able to find a file in the Project hierarchy of the project on which I am using this annotation processor. Through the annotation I can pass in the path of the file I am searching for relative to the project root but i cannot retrieve the project's working directory.
Let's say that the processor is MyCustomProcessor and I am using it on the project MyProject. I want to be able to access(read) a file (a properties file) from the project structure of MyProject from the "process" method of MyCustomProcessor.
I have read this link Eclipse - Annotation processor, get project path but when I use their solution I get a null returned from the StandardJavaFileManager.getLocation(StandardLocation.SOURCE_PATH)
call.
Some more details regarding the implementation:
MyAnnotationProcessor:
@SupportedAnnotationTypes(value = {"MyAnnotation" })
@SupportedSourceVersion(RELEASE_6)
public class MyCustomProcessor extends AbstractProcessor {
...
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
for (final Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
<!-- Here is where I would like to get the working directory !-->
}
}
}
More details about the testing and development environment: Eclipse Kepler, JRE 1.7.
If you need more details just ask.
回答1:
A comment on that answer points to this question as a solution: null JavaCompiler in Eclipse
Some tools aren't implemented in the JRE and only available in the JDK. It seems Eclipse runs the processor using the JRE by default, so you need to configure your project to use a JDK as runtime instead.
来源:https://stackoverflow.com/questions/25192381/how-can-i-get-the-working-directory-of-a-project-from-an-annotation-processor-in