Skip to content

Commit a193af1

Browse files
committed
Use auxclasspath ClassLoader to resolve type qualified names
1 parent 0453089 commit a193af1

File tree

10 files changed

+36
-14
lines changed

10 files changed

+36
-14
lines changed

pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void symbolFacade(Node rootNode, LanguageVersionHandler languageVersionH
123123

124124
private void resolveQualifiedNames(Node rootNode, LanguageVersionHandler handler) {
125125
long start = System.nanoTime();
126-
handler.getQualifiedNameResolutionFacade().start(rootNode);
126+
handler.getQualifiedNameResolutionFacade(configuration.getClassLoader()).start(rootNode);
127127
long end = System.nanoTime();
128128
Benchmarker.mark(Benchmark.QualifiedNameResolution, end - start, 0);
129129
}

pmd-core/src/main/java/net/sourceforge/pmd/lang/AbstractLanguageVersionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public VisitorStarter getMultifileFacade() {
6262

6363

6464
@Override
65-
public VisitorStarter getQualifiedNameResolutionFacade() {
65+
public VisitorStarter getQualifiedNameResolutionFacade(ClassLoader classLoader) {
6666
return VisitorStarter.DUMMY;
6767
}
6868

pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageVersionHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ public interface LanguageVersionHandler {
100100
* Gets the visitor that populates the qualified names of the
101101
* nodes.
102102
*
103+
* @param classLoader The classloader to use to resolve the types of type qualified names
104+
*
103105
* @return The visitor starter
104106
*/
105-
VisitorStarter getQualifiedNameResolutionFacade();
107+
VisitorStarter getQualifiedNameResolutionFacade(ClassLoader classLoader);
106108

107109

108110
DFAGraphRule getDFAGraphRule();

pmd-java/src/main/java/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ public void start(Node rootNode) {
123123

124124

125125
@Override
126-
public VisitorStarter getQualifiedNameResolutionFacade() {
126+
public VisitorStarter getQualifiedNameResolutionFacade(final ClassLoader classLoader) {
127127
return new VisitorStarter() {
128128
@Override
129129
public void start(Node rootNode) {
130-
new QualifiedNameResolver().initializeWith((ASTCompilationUnit) rootNode);
130+
new QualifiedNameResolver().initializeWith(classLoader, (ASTCompilationUnit) rootNode);
131131
}
132132
};
133133
}

pmd-java/src/main/java/net/sourceforge/pmd/lang/java/qname/JavaTypeQualifiedName.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public final class JavaTypeQualifiedName extends JavaQualifiedName {
2525
/** Local index value for when the class is not local. */
2626
static final int NOTLOCAL_PLACEHOLDER = -1;
2727

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

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

6362

63+
/**
64+
* Sets the class loader used by qualified names to resolve their types.
65+
* This method is called by the qualified name resolver during initialization.
66+
*
67+
* @param cl a class loader
68+
*/
69+
static void setClassLoader(ClassLoader cl) {
70+
ClassLoader asmCL = PMDASMClassLoader.getInstance(cl);
71+
if (asmCL != classLoader) {
72+
classLoader = asmCL;
73+
}
74+
}
75+
76+
6477
@Override
6578
public JavaTypeQualifiedName getClassName() {
6679
return this;
@@ -191,7 +204,7 @@ public Class<?> getType() {
191204
*/
192205
private Class<?> loadType() throws ClassNotFoundException {
193206
// hence why the toString should follow binary name specification
194-
return CLASS_LOADER.loadClass(getBinaryName());
207+
return classLoader.loadClass(getBinaryName());
195208
}
196209

197210

pmd-java/src/main/java/net/sourceforge/pmd/lang/java/qname/QualifiedNameResolver.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ public class QualifiedNameResolver extends JavaParserVisitorReducedAdapter {
9999
private ImmutableList<String> classNames;
100100

101101

102-
public void initializeWith(ASTCompilationUnit rootNode) {
102+
/**
103+
* Initialises the visitor and starts it.
104+
* @param classLoader The classloader that will be used by type qualified names
105+
* to load their type.
106+
* @param rootNode The root hierarchy
107+
*/
108+
public void initializeWith(ClassLoader classLoader, ASTCompilationUnit rootNode) {
109+
JavaTypeQualifiedName.setClassLoader(classLoader);
103110
rootNode.jjtAccept(this, null);
104111
}
105112

pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ParserTstUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static <E> List<E> getOrderedNodes(Class<E> clazz, String javaCode) {
9393
JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(),
9494
new Class[] { JavaParserVisitor.class }, coll);
9595
jpv.visit(cu, null);
96-
new QualifiedNameResolver().initializeWith(cu);
96+
new QualifiedNameResolver().initializeWith(ParserTstUtil.class.getClassLoader(), cu);
9797
new SymbolFacade().initializeWith(cu);
9898
new DataFlowFacade().initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
9999

@@ -214,7 +214,7 @@ public static LanguageVersionHandler getDefaultLanguageVersionHandler() {
214214
public static ASTCompilationUnit parseJava(LanguageVersionHandler languageVersionHandler, String code) {
215215
ASTCompilationUnit rootNode = (ASTCompilationUnit) languageVersionHandler
216216
.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
217-
languageVersionHandler.getQualifiedNameResolutionFacade().start(rootNode);
217+
languageVersionHandler.getQualifiedNameResolutionFacade(ParserTstUtil.class.getClassLoader()).start(rootNode);
218218
languageVersionHandler.getSymbolFacade().start(rootNode);
219219
return rootNode;
220220
}

pmd-java/src/test/java/net/sourceforge/pmd/lang/java/multifile/JavaMultifileVisitorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void testBothClassesFieldsAreThere() {
141141
static ASTCompilationUnit parseAndVisitForClass(Class<?> clazz) {
142142
ASTCompilationUnit acu = ParserTstUtil.parseJavaDefaultVersion(clazz);
143143
LanguageVersionHandler handler = ParserTstUtil.getDefaultLanguageVersionHandler();
144-
handler.getQualifiedNameResolutionFacade().start(acu);
144+
handler.getQualifiedNameResolutionFacade(JavaMultifileVisitorTest.class.getClassLoader()).start(acu);
145145
handler.getTypeResolutionFacade(JavaMultifileVisitorTest.class.getClassLoader()).start(acu);
146146
handler.getMultifileFacade().start(acu);
147147
return acu;

pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,7 @@ private ASTCompilationUnit parseAndTypeResolveForString(String source, String ve
18581858
.getVersion(version).getLanguageVersionHandler();
18591859
ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler
18601860
.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(source));
1861-
languageVersionHandler.getQualifiedNameResolutionFacade().start(acu);
1861+
languageVersionHandler.getQualifiedNameResolutionFacade(ClassTypeResolverTest.class.getClassLoader()).start(acu);
18621862
languageVersionHandler.getSymbolFacade().start(acu);
18631863
languageVersionHandler.getTypeResolutionFacade(ClassTypeResolverTest.class.getClassLoader()).start(acu);
18641864
return acu;

pmd-java8/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverJava8Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private ASTCompilationUnit parseAndTypeResolveForClass(Class<?> clazz, String ve
109109
ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler
110110
.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new InputStreamReader(is));
111111
languageVersionHandler.getSymbolFacade().start(acu);
112-
languageVersionHandler.getQualifiedNameResolutionFacade().start(acu);
112+
languageVersionHandler.getQualifiedNameResolutionFacade(ClassTypeResolverJava8Test.class.getClassLoader()).start(acu);
113113
languageVersionHandler.getTypeResolutionFacade(ClassTypeResolverJava8Test.class.getClassLoader()).start(acu);
114114
return acu;
115115
}

0 commit comments

Comments
 (0)