Im writing an annotation processor for android projects. I want to get the absolute path of the androids resource folder (I know it's configureable through gradle, but I ignore this case for now).
My idea is to get the absolute path of an processed element:
@MyAnnotation public class Foo{ ... }
So in my AnnotationProcessor I want to get the path to Foo.java
.
@Override public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class))
try {
ProcessorMessage.info(element, "Path: %s %s", getWorkingDir(), getExecutionPath());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
ProcessorMessage.error(element, "Error " + e.getMessage());
}
return false;
}
private String getExecutionPath() throws UnsupportedEncodingException {
String path = AnnotatedAdapterProcessor.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
return decodedPath;
}
private String getWorkingDir() {
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
return s;
}
However it gives me the following output:
Path: /Users/XXXXX/.gradle/daemon/1.12 /Users/XXXXX/.m2/repository/com/example/myprocessor/processor/0.1.2-SNAPSHOT/processor-0.1.2-SNAPSHOT.jar
Is there a way to get the path where Foo.java
is located? (Just to get a startpoint from where I will continue to start searching for the resource folder)