Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: custom URLClassLoaders are now actually used #3818

Merged
merged 4 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.