diff options
author | chibash <chiba@javassist.org> | 2016-09-28 00:51:15 +0900 |
---|---|---|
committer | chibash <chiba@javassist.org> | 2016-09-28 00:51:15 +0900 |
commit | fb93ae8b8f6e0646ed9b95d4d8d0abe77d601a62 (patch) | |
tree | 4e7c46b44dfd861a084d3917de9fad4a7e883682 /src/main/javassist/LoaderClassPath.java | |
parent | 9f1d7a2cf0408d92309145a3d63620d64206a38a (diff) | |
download | javassist-fb93ae8b8f6e0646ed9b95d4d8d0abe77d601a62.tar.gz javassist-fb93ae8b8f6e0646ed9b95d4d8d0abe77d601a62.zip |
improves the backward compatibility of LoaderClassPath.rel_3_21_0-java9-ea2
LoaderClassPath is modified to show the same behavior in both Java 8 and 9.
Diffstat (limited to 'src/main/javassist/LoaderClassPath.java')
-rw-r--r-- | src/main/javassist/LoaderClassPath.java | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/src/main/javassist/LoaderClassPath.java b/src/main/javassist/LoaderClassPath.java index d516323e..daf43c20 100644 --- a/src/main/javassist/LoaderClassPath.java +++ b/src/main/javassist/LoaderClassPath.java @@ -48,10 +48,39 @@ 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() { @@ -67,13 +96,18 @@ public class LoaderClassPath implements ClassPath { * 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; + } } /** @@ -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; + } } /** |