Skip to content

Commit

Permalink
Use auxclasspath ClassLoader to resolve type qualified names
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Mar 24, 2018
1 parent 0453089 commit a193af1
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void symbolFacade(Node rootNode, LanguageVersionHandler languageVersionH

private void resolveQualifiedNames(Node rootNode, LanguageVersionHandler handler) {
long start = System.nanoTime();
handler.getQualifiedNameResolutionFacade().start(rootNode);
handler.getQualifiedNameResolutionFacade(configuration.getClassLoader()).start(rootNode);
long end = System.nanoTime();
Benchmarker.mark(Benchmark.QualifiedNameResolution, end - start, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public VisitorStarter getMultifileFacade() {


@Override
public VisitorStarter getQualifiedNameResolutionFacade() {
public VisitorStarter getQualifiedNameResolutionFacade(ClassLoader classLoader) {
return VisitorStarter.DUMMY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ public interface LanguageVersionHandler {
* Gets the visitor that populates the qualified names of the
* nodes.
*
* @param classLoader The classloader to use to resolve the types of type qualified names
*
* @return The visitor starter
*/
VisitorStarter getQualifiedNameResolutionFacade();
VisitorStarter getQualifiedNameResolutionFacade(ClassLoader classLoader);


DFAGraphRule getDFAGraphRule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ public void start(Node rootNode) {


@Override
public VisitorStarter getQualifiedNameResolutionFacade() {
public VisitorStarter getQualifiedNameResolutionFacade(final ClassLoader classLoader) {
return new VisitorStarter() {
@Override
public void start(Node rootNode) {
new QualifiedNameResolver().initializeWith((ASTCompilationUnit) rootNode);
new QualifiedNameResolver().initializeWith(classLoader, (ASTCompilationUnit) rootNode);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public final class JavaTypeQualifiedName extends JavaQualifiedName {
/** Local index value for when the class is not local. */
static final int NOTLOCAL_PLACEHOLDER = -1;

// Should we share that with ClassTypeResolver?
private static final PMDASMClassLoader CLASS_LOADER = PMDASMClassLoader.getInstance(JavaTypeQualifiedName.class.getClassLoader());
private static ClassLoader classLoader = PMDASMClassLoader.getInstance(JavaTypeQualifiedName.class.getClassLoader());

// since we prepend each time, these lists are in the reversed order (innermost elem first).
// we use ImmutableList.reverse() to get them in their usual, user-friendly order
Expand Down Expand Up @@ -61,6 +60,20 @@ public final class JavaTypeQualifiedName extends JavaQualifiedName {
}


/**
* Sets the class loader used by qualified names to resolve their types.
* This method is called by the qualified name resolver during initialization.
*
* @param cl a class loader
*/
static void setClassLoader(ClassLoader cl) {
ClassLoader asmCL = PMDASMClassLoader.getInstance(cl);
if (asmCL != classLoader) {
classLoader = asmCL;
}
}


@Override
public JavaTypeQualifiedName getClassName() {
return this;
Expand Down Expand Up @@ -191,7 +204,7 @@ public Class<?> getType() {
*/
private Class<?> loadType() throws ClassNotFoundException {
// hence why the toString should follow binary name specification
return CLASS_LOADER.loadClass(getBinaryName());
return classLoader.loadClass(getBinaryName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ public class QualifiedNameResolver extends JavaParserVisitorReducedAdapter {
private ImmutableList<String> classNames;


public void initializeWith(ASTCompilationUnit rootNode) {
/**
* Initialises the visitor and starts it.
* @param classLoader The classloader that will be used by type qualified names
* to load their type.
* @param rootNode The root hierarchy
*/
public void initializeWith(ClassLoader classLoader, ASTCompilationUnit rootNode) {
JavaTypeQualifiedName.setClassLoader(classLoader);
rootNode.jjtAccept(this, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static <E> List<E> getOrderedNodes(Class<E> clazz, String javaCode) {
JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(),
new Class[] { JavaParserVisitor.class }, coll);
jpv.visit(cu, null);
new QualifiedNameResolver().initializeWith(cu);
new QualifiedNameResolver().initializeWith(ParserTstUtil.class.getClassLoader(), cu);
new SymbolFacade().initializeWith(cu);
new DataFlowFacade().initializeWith(languageVersionHandler.getDataFlowHandler(), cu);

Expand Down Expand Up @@ -214,7 +214,7 @@ public static LanguageVersionHandler getDefaultLanguageVersionHandler() {
public static ASTCompilationUnit parseJava(LanguageVersionHandler languageVersionHandler, String code) {
ASTCompilationUnit rootNode = (ASTCompilationUnit) languageVersionHandler
.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
languageVersionHandler.getQualifiedNameResolutionFacade().start(rootNode);
languageVersionHandler.getQualifiedNameResolutionFacade(ParserTstUtil.class.getClassLoader()).start(rootNode);
languageVersionHandler.getSymbolFacade().start(rootNode);
return rootNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void testBothClassesFieldsAreThere() {
static ASTCompilationUnit parseAndVisitForClass(Class<?> clazz) {
ASTCompilationUnit acu = ParserTstUtil.parseJavaDefaultVersion(clazz);
LanguageVersionHandler handler = ParserTstUtil.getDefaultLanguageVersionHandler();
handler.getQualifiedNameResolutionFacade().start(acu);
handler.getQualifiedNameResolutionFacade(JavaMultifileVisitorTest.class.getClassLoader()).start(acu);
handler.getTypeResolutionFacade(JavaMultifileVisitorTest.class.getClassLoader()).start(acu);
handler.getMultifileFacade().start(acu);
return acu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ private ASTCompilationUnit parseAndTypeResolveForString(String source, String ve
.getVersion(version).getLanguageVersionHandler();
ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler
.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(source));
languageVersionHandler.getQualifiedNameResolutionFacade().start(acu);
languageVersionHandler.getQualifiedNameResolutionFacade(ClassTypeResolverTest.class.getClassLoader()).start(acu);
languageVersionHandler.getSymbolFacade().start(acu);
languageVersionHandler.getTypeResolutionFacade(ClassTypeResolverTest.class.getClassLoader()).start(acu);
return acu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private ASTCompilationUnit parseAndTypeResolveForClass(Class<?> clazz, String ve
ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler
.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new InputStreamReader(is));
languageVersionHandler.getSymbolFacade().start(acu);
languageVersionHandler.getQualifiedNameResolutionFacade().start(acu);
languageVersionHandler.getQualifiedNameResolutionFacade(ClassTypeResolverJava8Test.class.getClassLoader()).start(acu);
languageVersionHandler.getTypeResolutionFacade(ClassTypeResolverJava8Test.class.getClassLoader()).start(acu);
return acu;
}
Expand Down

0 comments on commit a193af1

Please sign in to comment.