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 classloader to also allow instrumentation of e.g. commons-io version 2.6 #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -27,7 +29,7 @@ public class Instrumentor {
public static void main(String[] args) {

// get class loader
classloader = Thread.currentThread().getContextClassLoader();
classloader = new URLClassLoader(new URL[] {}, null);

// parse command line arguments
Options options = Options.v();
Expand Down Expand Up @@ -130,6 +132,9 @@ private static void writeClass(String cls, byte[] bytes) {

private static void loadAndWriteResource(String resource) {
InputStream is = classloader.getResourceAsStream(resource);
if (is == null) {
is = ClassLoader.getSystemResourceAsStream(resource);
}
if (is == null) {
System.err.println("Error loading Kelinci classes for addition to output");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ private static void addToClassPath(String url) {
File file = new File(url);
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{file.toURI().toURL()});
method.invoke(Instrumentor.classloader, new Object[]{file.toURI().toURL()});
method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{file.toURI().toURL()});
} catch (Exception e) {
throw new RuntimeException("Error adding location to class path: " + url);
}
Expand Down