Skip to content

Commit

Permalink
fix: custom URLClassLoaders are now actually used (INRIA#3818)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbwogi authored and woutersmeenk committed Aug 29, 2021
1 parent 136709b commit c121af4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/main/java/spoon/support/StandardEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ public void setInputClassLoader(ClassLoader aClassLoader) {
throw new SpoonException("Spoon does not support a URLClassLoader containing other resources than local file.");
}
}
return;
}
this.classloader = aClassLoader;
}
Expand Down
22 changes: 18 additions & 4 deletions src/test/java/spoon/test/compilation/CompilationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,16 +458,30 @@ public <T> void visitCtTypeReference(CtTypeReference<T> reference) {

@Test
public void testURLClassLoader() throws Exception {
// contract: Spoon handles URLClassLoader and retrieves path elements
// contract: Spoon uses the manually set and not the default URLClassLoader
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/classloader-test/LauncherUser.java");
launcher.getEnvironment().setNoClasspath(false);
String classpath = "src/test/resources/classloader-test/";
URL url = new File(classpath).toURI().toURL();
URL[] urls = new URL[]{url};
URLClassLoader urlClassloaderNullParent = new URLClassLoader(urls,null);
launcher.getEnvironment().setInputClassLoader(urlClassloaderNullParent);
CtModel model = launcher.buildModel();
List<CtType> ctTypeList = model.getElements(new TypeFilter(CtType.class));
CtTypeReference launcherField = ctTypeList.get(0).getField("launcher").getType();
assertEquals(0, launcherField.getTypeDeclaration().getFields().size());
}

@Test
public void testPathRetrievalFromURLClassLoader() throws Exception {
// contract: Spoon retrieves path elements from a manually set URLClassloader
String expected = "target/classes/";

File f = new File(expected);
URL[] urls = {f.toURL()};
URL[] urls = {f.toURI().toURL()};
URLClassLoader urlClassLoader = new URLClassLoader(urls);
Launcher launcher = new Launcher();
launcher.getEnvironment().setInputClassLoader(urlClassLoader);

String[] sourceClassPath = launcher.getEnvironment().getSourceClasspath();
assertEquals(1, sourceClassPath.length);
String tail = sourceClassPath[0].substring(sourceClassPath[0].length() - expected.length());
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/classloader-test/LauncherUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import spoon.Launcher;

public class LauncherUser {
Launcher launcher;

}
Binary file not shown.

0 comments on commit c121af4

Please sign in to comment.