Browse Source

improves the backward compatibility of LoaderClassPath.

LoaderClassPath is modified to show the same behavior in both Java 8 and 9.
tags/rel_3_21_0-java9-ea2^0
chibash 7 years ago
parent
commit
fb93ae8b8f

+ 1
- 1
build.xml View File

<classpath refid="classpath"/> <classpath refid="classpath"/>
</javac> </javac>
</target> </target>
<target name="test-compile" depends="compile"> <target name="test-compile" depends="compile">
<javac srcdir="${test.src.dir}" <javac srcdir="${test.src.dir}"
destdir="${test.build.dir}" destdir="${test.build.dir}"

BIN
javassist.jar View File


+ 1
- 2
src/main/javassist/ClassPoolTail.java View File

} }
else { else {
ClassLoader cl = Thread.currentThread().getContextClassLoader(); ClassLoader cl = Thread.currentThread().getContextClassLoader();
appendClassPath(new LoaderClassPath(cl));
return appendClassPath(new ModuleClassPath());
return appendClassPath(new LoaderClassPath(cl, true));
} }
} }



+ 1
- 1
src/main/javassist/CtClass.java View File

/** /**
* The version number of this release. * 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. * Prints the version number and the copyright notice.

+ 44
- 5
src/main/javassist/LoaderClassPath.java View File

public class LoaderClassPath implements ClassPath { public class LoaderClassPath implements ClassPath {
private WeakReference clref; 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. * Creates a search path representing a class loader.
*/ */
public LoaderClassPath(ClassLoader cl) { public LoaderClassPath(ClassLoader cl) {
this(cl, fallbackOnModuleClassPath);
}

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


public String toString() { public String toString() {
* This method calls <code>getResourceAsStream(String)</code> * This method calls <code>getResourceAsStream(String)</code>
* on the class loader. * on the class loader.
*/ */
public InputStream openClassfile(String classname) {
public InputStream openClassfile(String classname) throws NotFoundException {
String cname = classname.replace('.', '/') + ".class"; String cname = classname.replace('.', '/') + ".class";
ClassLoader cl = (ClassLoader)clref.get(); ClassLoader cl = (ClassLoader)clref.get();
if (cl == null) if (cl == null)
return null; // not found 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;
}
} }


/** /**
ClassLoader cl = (ClassLoader)clref.get(); ClassLoader cl = (ClassLoader)clref.get();
if (cl == null) if (cl == null)
return null; // not found 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;
}
} }


/** /**

+ 1
- 0
src/main/javassist/ModuleClassPath.java View File

* @see ClassPool#appendClassPath(ClassPath) * @see ClassPool#appendClassPath(ClassPath)
* @see LoaderClassPath * @see LoaderClassPath
* @see ClassClassPath * @see ClassClassPath
* @see 3.21
*/ */
public class ModuleClassPath implements ClassPath { public class ModuleClassPath implements ClassPath {
private HashMap packages = new HashMap(); private HashMap packages = new HashMap();

+ 7
- 0
src/test/javassist/JvstTest5.java View File

Object obj = make(ctClass.getName()); Object obj = make(ctClass.getName());
assertEquals(1, invoke(obj, "run")); 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()));
}
} }

Loading…
Cancel
Save