Im는 안드로이드 프로젝트를위한 주석 프로세서를 작성하고 있습니다. 나는 안드로이드 리소스 폴더의 절대 경로를 얻고 싶습니다 (나는 gradle을 통해 구성 가능하다는 것을 알고 있지만, 지금은이 경우를 무시합니다). 작업 디렉토리의 파일 경로를 얻으십시오
@MyAnnotation public class Foo{ ... }
그래서 내 AnnotationProcessor에 내가
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;
}
이 나에게 다음과 같은 출력을 제공하지만 :
경로 : /Users/XXXXX/.gradle/daemon/1.12 /Users/XXXXX/.m2/repository/com/example/myprocessor/processor/ 0.1.2-SNAPSHOT/processor-0.1.2-SNAPSHOT.jar
Foo.java
이있는 경로를 얻을 수있는 방법이 있습니까? (리소스 폴더를 계속 검색 할 시작 지점을 얻으려고)