Skip to content

Commit

Permalink
Fixed javac environment's Java8 resolve() method, implemented Java9 v…
Browse files Browse the repository at this point in the history
…ersion.

	Change on 2016/11/21 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139816214
  • Loading branch information
tomball authored and Keith Stanger committed Dec 1, 2016
1 parent 964af04 commit 2a18c2b
Showing 1 changed file with 14 additions and 4 deletions.
Expand Up @@ -23,6 +23,7 @@
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.lang.model.element.Element;
Expand Down Expand Up @@ -70,18 +71,27 @@ public Element resolve(String name) {

private ClassSymbol enterClassJavac(Name className) {
try {
Method m = ClassReader.class.getDeclaredMethod("enterName", Name.class);
Method m = ClassReader.class.getDeclaredMethod("enterClass", Name.class);
return (ClassSymbol) m.invoke(classReader, className);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
return null;
}
}

private ClassSymbol enterClassJavac9(Name className) {
// TODO(tball): remove reflection use when Java 9 is minimum version.
try {
Method m = Symtab.class.getDeclaredMethod("enterName", Name.class);
return (ClassSymbol) m.invoke(symbolTable, className);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
Field javaBaseField = Names.class.getDeclaredField("java_base");
Name javaBaseName = (Name) javaBaseField.get(javacNames);

Class<?> moduleSymbolCls = Class.forName("com.sun.tools.javac.code.Symbol.MethodSymbol");
Method enterModule = Symtab.class.getDeclaredMethod("enterModule", Name.class);
Object javaBaseModule = enterModule.invoke(symbolTable, javaBaseName);

Method enterClass = Symtab.class.getDeclaredMethod("enterClass", moduleSymbolCls, Name.class);
return (ClassSymbol) enterClass.invoke(symbolTable, javaBaseModule, className);
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException
| InvocationTargetException | IllegalAccessException e) {
return null;
}
}
Expand Down

0 comments on commit 2a18c2b

Please sign in to comment.