Require explicit classloader for runtime inheritance

Previously, the runtime inheritance providers would always use the
system classloader. Now you have to explicitly pass the classloader
to use. Also add a 'verbose' flag for debugging.
This commit is contained in:
Agaricus 2013-01-25 23:27:40 -08:00
parent 41fefc0167
commit 25fcbb40fe
3 changed files with 24 additions and 7 deletions

View File

@ -11,7 +11,9 @@ public class RemappedRuntimeInheritanceProvider extends RuntimeInheritanceProvid
private final JarMapping jarMapping; private final JarMapping jarMapping;
private final JarMapping inverseJarMapping; private final JarMapping inverseJarMapping;
public RemappedRuntimeInheritanceProvider(JarMapping jarMapping) { public RemappedRuntimeInheritanceProvider(ClassLoader classLoader, boolean verbose, JarMapping jarMapping) {
super(classLoader, verbose);
this.jarMapping = jarMapping; this.jarMapping = jarMapping;
this.inverseJarMapping = new JarMapping(); this.inverseJarMapping = new JarMapping();

View File

@ -35,18 +35,33 @@ import java.util.ArrayList;
* Lookup class inheritance from classes loaded at runtime. * Lookup class inheritance from classes loaded at runtime.
*/ */
public class RuntimeInheritanceProvider implements IInheritanceProvider { public class RuntimeInheritanceProvider implements IInheritanceProvider {
private final ClassLoader classLoader;
private final boolean verbose;
public RuntimeInheritanceProvider(ClassLoader classLoader, boolean verbose) {
this.classLoader = classLoader;
this.verbose = verbose;
}
// TODO: option to transform through a jarRemapper at runtime
@Override @Override
public List<String> getParents(String internalClassName) { public List<String> getParents(String internalClassName) {
List<String> parents = new ArrayList<String>(); List<String> parents = new ArrayList<String>();
String sourceClassName = toSourceName(internalClassName); String sourceClassName = toSourceName(internalClassName);
Class clazz; Class clazz;
try { try {
clazz = ClassLoader.getSystemClassLoader().loadClass(sourceClassName); // load class without initializing clazz = classLoader.loadClass(sourceClassName); // load class without initializing
//clazz = Class.forName(toSourceName(sourceClassName)); // runs static initializers - avoid! //clazz = Class.forName(toSourceName(sourceClassName)); // runs static initializers - avoid!
} catch (Throwable t) { } catch (Throwable t) {
SpecialSource.log("RuntimeInheritanceProvider failed: " + t); if (verbose) {
System.out.println("RuntimeInheritanceProvider failed: " + t);
t.printStackTrace();
}
return null;
}
if (clazz == null) {
if (verbose) {
System.out.println("RuntimeInheritanceProvider no class: " + sourceClassName);
}
return null; return null;
} }

View File

@ -157,11 +157,11 @@ public class SpecialSource {
inheritanceProviders.add(new JarInheritanceProvider(jar3)); inheritanceProviders.add(new JarInheritanceProvider(jar3));
if (options.has("live-remapped")) { if (options.has("live-remapped")) {
inheritanceProviders.add(new RemappedRuntimeInheritanceProvider(jarMapping)); inheritanceProviders.add(new RemappedRuntimeInheritanceProvider(ClassLoader.getSystemClassLoader(), !options.has("quiet"), jarMapping));
} }
if (options.has("live")) { if (options.has("live")) {
inheritanceProviders.add(new RuntimeInheritanceProvider()); inheritanceProviders.add(new RuntimeInheritanceProvider(ClassLoader.getSystemClassLoader(), !options.has("quiet")));
} }
@ -170,7 +170,7 @@ public class SpecialSource {
} }
} }
public static void log(String message) { private static void log(String message) {
if (options != null && !options.has("quiet")) { if (options != null && !options.has("quiet")) {
System.out.println(message); System.out.println(message);
} }