From: chibash Date: Tue, 27 Sep 2016 15:51:15 +0000 (+0900) Subject: improves the backward compatibility of LoaderClassPath. X-Git-Tag: rel_3_21_0-java9-ea2^0 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fb93ae8b8f6e0646ed9b95d4d8d0abe77d601a62;p=javassist.git improves the backward compatibility of LoaderClassPath. LoaderClassPath is modified to show the same behavior in both Java 8 and 9. --- diff --git a/build.xml b/build.xml index e2b8819e..c4d00806 100644 --- a/build.xml +++ b/build.xml @@ -83,7 +83,7 @@ - + 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() { @@ -67,13 +96,18 @@ public class LoaderClassPath implements ClassPath { * This method calls getResourceAsStream(String) * 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; + } } /** @@ -88,8 +122,13 @@ public class LoaderClassPath implements ClassPath { 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; + } } /** diff --git a/src/main/javassist/ModuleClassPath.java b/src/main/javassist/ModuleClassPath.java index c41161f8..20f0e430 100644 --- a/src/main/javassist/ModuleClassPath.java +++ b/src/main/javassist/ModuleClassPath.java @@ -32,6 +32,7 @@ import java.util.Set; * @see ClassPool#appendClassPath(ClassPath) * @see LoaderClassPath * @see ClassClassPath + * @see 3.21 */ public class ModuleClassPath implements ClassPath { private HashMap packages = new HashMap(); diff --git a/src/test/javassist/JvstTest5.java b/src/test/javassist/JvstTest5.java index c1c10d9b..41b640c7 100644 --- a/src/test/javassist/JvstTest5.java +++ b/src/test/javassist/JvstTest5.java @@ -235,4 +235,11 @@ public class JvstTest5 extends JvstTestRoot { 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())); + } }