Skip to content

Commit

Permalink
improves the backward compatibility of LoaderClassPath.
Browse files Browse the repository at this point in the history
LoaderClassPath is modified to show the same behavior in both Java 8 and 9.
  • Loading branch information
chibash committed Sep 27, 2016
1 parent 9f1d7a2 commit fb93ae8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<classpath refid="classpath"/>
</javac>
</target>

<target name="test-compile" depends="compile">
<javac srcdir="${test.src.dir}"
destdir="${test.build.dir}"
Expand Down
Binary file modified javassist.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions src/main/javassist/ClassPoolTail.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ public ClassPath appendSystemPath() {
}
else {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
appendClassPath(new LoaderClassPath(cl));
return appendClassPath(new ModuleClassPath());
return appendClassPath(new LoaderClassPath(cl, true));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/javassist/CtClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public abstract class CtClass {
/**
* The version number of this release.
*/
public static final String version = "3.21.0-java9";
public static final String version = "3.21.0-java9beta2";

/**
* Prints the version number and the copyright notice.
Expand Down
49 changes: 44 additions & 5 deletions src/main/javassist/LoaderClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,40 @@
public class LoaderClassPath implements ClassPath {
private WeakReference clref;

/**
* If true, this search path implicitly includes
* a {@code ModuleClassPath} as a fallback.
* For backward compatibility, this field is set to true
* if the JVM is Java 9 or later. It can be false in
* Java 9 but the behavior of {@code LoadClassPath} will
* be different from its behavior in Java 8 or older.
*
* <p>This field must be false if the JVM is Java 8 or older.
*
* @since 3.21
*/
public static boolean fallbackOnModuleClassPath
= javassist.bytecode.ClassFile.MAJOR_VERSION >= javassist.bytecode.ClassFile.JAVA_9;

private static ModuleClassPath moduleClassPath = null;

private boolean doFallback;

/**
* Creates a search path representing a class loader.
*/
public LoaderClassPath(ClassLoader cl) {
this(cl, fallbackOnModuleClassPath);
}

LoaderClassPath(ClassLoader cl, boolean fallback) {
clref = new WeakReference(cl);
doFallback = fallback;
if (fallback)
synchronized (LoaderClassPath.class) {
if (moduleClassPath == null)
moduleClassPath = new ModuleClassPath();
}
}

public String toString() {
Expand All @@ -67,13 +96,18 @@ public String toString() {
* This method calls <code>getResourceAsStream(String)</code>
* on the class loader.
*/
public InputStream openClassfile(String classname) {
public InputStream openClassfile(String classname) throws NotFoundException {
String cname = classname.replace('.', '/') + ".class";
ClassLoader cl = (ClassLoader)clref.get();
if (cl == null)
return null; // not found
else
return cl.getResourceAsStream(cname);
else {
InputStream is = cl.getResourceAsStream(cname);
if (is == null && doFallback)
return moduleClassPath.openClassfile(classname);
else
return is;
}
}

/**
Expand All @@ -88,8 +122,13 @@ public URL find(String classname) {
ClassLoader cl = (ClassLoader)clref.get();
if (cl == null)
return null; // not found
else
return cl.getResource(cname);
else {
URL url = cl.getResource(cname);
if (url == null && doFallback)
return moduleClassPath.find(classname);
else
return url;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/javassist/ModuleClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @see ClassPool#appendClassPath(ClassPath)
* @see LoaderClassPath
* @see ClassClassPath
* @see 3.21
*/
public class ModuleClassPath implements ClassPath {
private HashMap packages = new HashMap();
Expand Down
7 changes: 7 additions & 0 deletions src/test/javassist/JvstTest5.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,11 @@ public void test83StackmapWithArrayType() throws Exception {
Object obj = make(ctClass.getName());
assertEquals(1, invoke(obj, "run"));
}

public void testLoaderClassPath() throws Exception {
ClassPool cp = new ClassPool();
cp.appendClassPath(new LoaderClassPath(new Loader()));
assertNotNull(cp.get(Object.class.getName()));
assertNotNull(cp.get(this.getClass().getName()));
}
}

0 comments on commit fb93ae8

Please sign in to comment.