[KeepAnno] Use reflection to access type internals in preprocessor.
Bug: b/248408342
Change-Id: If0d314f08b0c67f716bbbc3538945a3d0898c7ec
diff --git a/build.gradle b/build.gradle
index 9fc38d2..f775822 100644
--- a/build.gradle
+++ b/build.gradle
@@ -319,8 +319,6 @@
keepannoCompile group: 'org.ow2.asm', name: 'asm', version: asmVersion
keepannoCompile "com.google.guava:guava:$guavaVersion"
- keepannoCompile files("third_party/openjdk/jdk8/linux-x86/lib/tools.jar")
- testCompile files("third_party/openjdk/jdk8/linux-x86/lib/tools.jar")
testCompile sourceSets.keepanno.output
testRuntime sourceSets.keepanno.output
}
diff --git a/src/keepanno/java/com/android/tools/r8/keepanno/processor/KeepEdgeProcessor.java b/src/keepanno/java/com/android/tools/r8/keepanno/processor/KeepEdgeProcessor.java
index c950191..817b069 100644
--- a/src/keepanno/java/com/android/tools/r8/keepanno/processor/KeepEdgeProcessor.java
+++ b/src/keepanno/java/com/android/tools/r8/keepanno/processor/KeepEdgeProcessor.java
@@ -15,13 +15,13 @@
import com.android.tools.r8.keepanno.ast.KeepConsequences;
import com.android.tools.r8.keepanno.ast.KeepEdge;
import com.android.tools.r8.keepanno.ast.KeepEdge.Builder;
+import com.android.tools.r8.keepanno.ast.KeepEdgeException;
import com.android.tools.r8.keepanno.ast.KeepItemPattern;
import com.android.tools.r8.keepanno.ast.KeepMethodNamePattern;
import com.android.tools.r8.keepanno.ast.KeepMethodPattern;
import com.android.tools.r8.keepanno.ast.KeepQualifiedClassNamePattern;
import com.android.tools.r8.keepanno.ast.KeepTarget;
import com.android.tools.r8.keepanno.utils.Unimplemented;
-import com.sun.tools.javac.code.Type;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@@ -126,15 +126,25 @@
edgeBuilder.setConsequences(consequencesBuilder.build());
}
+ private String getTypeNameForClassConstantElement(DeclaredType type) {
+ // The processor API does not expose the descriptor or typename, so we need to depend on the
+ // sun.tools internals to extract it. If not, this code will not work for inner classes as
+ // we cannot recover the $ separator.
+ try {
+ Object tsym = type.getClass().getDeclaredField("tsym").get(type);
+ Object flatname = tsym.getClass().getDeclaredField("flatname").get(tsym);
+ return flatname.toString();
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ throw new KeepEdgeException("Unable to obtain the class type name for: " + type);
+ }
+ }
+
private void processTarget(KeepTarget.Builder builder, AnnotationMirror mirror) {
KeepItemPattern.Builder itemBuilder = KeepItemPattern.builder();
AnnotationValue classConstantValue = getAnnotationValue(mirror, Target.classConstant);
if (classConstantValue != null) {
DeclaredType type = AnnotationClassValueVisitor.getType(classConstantValue);
- // The processor API does not expose the descriptor or typename, so we need to depend on the
- // sun.tools package and cast to its internal type to extract it. If not, this code will not
- // work for inner classes as we cannot recover the $ separator.
- String typeName = ((Type) type).tsym.flatName().toString();
+ String typeName = getTypeNameForClassConstantElement(type);
itemBuilder.setClassPattern(KeepQualifiedClassNamePattern.exact(typeName));
}
AnnotationValue methodNameValue = getAnnotationValue(mirror, Target.methodName);