diff options
author | chibash <chiba@javassist.org> | 2018-09-08 16:26:19 +0900 |
---|---|---|
committer | chibash <chiba@javassist.org> | 2018-09-08 16:26:19 +0900 |
commit | 69aee223df48fd783fb1d0a9be0f31f0ad54147f (patch) | |
tree | d9ba2db519b53f1f199858a900ad703fbbeef024 /src | |
parent | 6320bc4e14350af392b285b1b1ea312673625b21 (diff) | |
download | javassist-69aee223df48fd783fb1d0a9be0f31f0ad54147f.tar.gz javassist-69aee223df48fd783fb1d0a9be0f31f0ad54147f.zip |
modifies javassist.util.proxy.DefinePackageHelper to be compatible to Java 11.
It also adds a javadoc comment to CtClass#toClass() to recommend the users to use #toClass(Class) or #toClass(Lookup).
Diffstat (limited to 'src')
-rw-r--r-- | src/main/javassist/CtClass.java | 31 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/DefinePackageHelper.java | 165 |
2 files changed, 105 insertions, 91 deletions
diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 333a17d4..140a9cab 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -1261,15 +1261,26 @@ public abstract class CtClass { * server, the context class loader might be inappropriate to load the * class. * - * <p>This method is provided for convenience. If you need more - * complex functionality, you should write your own class loader. + * <p><b>Warning:</b> In Java 11 or later, the call to this method will + * print a warning message: + * <blockquote><pre> + * WARNING: An illegal reflective access operation has occurred + * WARNING: Illegal reflective access by javassist.util.proxy.SecurityActions$3 ... + * WARNING: Please consider reporting this to the maintainers of javassist.util.proxy.SecurityActions$3 + * WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations + * WARNING: All illegal access operations will be denied in a future release + * </pre></blockquote> + * To avoid this message, use {@link #toClass(Class)} + * or {@link #toClass(java.lang.invoke.MethodHandles.Lookup)}. + * {@link #toClass()} will be unavailable in a future release. + * </p> + * + * <p><b>Warning:</b> A Class object returned by this method may not + * work with a security manager or a signed jar file because a + * protection domain is not specified.</p> * * <p>Note: this method calls <code>toClass()</code> - * in <code>ClassPool</code>. - * - * <p><b>Warining:</b> A Class object returned by this method may not - * work with a security manager or a signed jar file because a - * protection domain is not specified. + * in <code>ClassPool</code>.</p> * * @see #toClass(java.lang.invoke.MethodHandles.Lookup) * @see #toClass(Class) @@ -1290,7 +1301,7 @@ public abstract class CtClass { * <p>Note: this method calls <code>toClass()</code> * in <code>ClassPool</code>. * - * <p><b>Warining:</b> A Class object returned by this method may not + * <p><b>Warning:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * @@ -1315,7 +1326,7 @@ public abstract class CtClass { * <p>Note: this method calls <code>toClass()</code> * in <code>ClassPool</code>. * - * <p><b>Warining:</b> A Class object returned by this method may not + * <p><b>Warning:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * @@ -1374,7 +1385,7 @@ public abstract class CtClass { /** * Converts this class to a <code>java.lang.Class</code> object. * - * <p><b>Warining:</b> A Class object returned by this method may not + * <p><b>Warning:</b> A Class object returned by this method may not * work with a security manager or a signed jar file because a * protection domain is not specified. * diff --git a/src/main/javassist/util/proxy/DefinePackageHelper.java b/src/main/javassist/util/proxy/DefinePackageHelper.java index 2124eebe..8a91eb28 100644 --- a/src/main/javassist/util/proxy/DefinePackageHelper.java +++ b/src/main/javassist/util/proxy/DefinePackageHelper.java @@ -32,110 +32,113 @@ import javassist.bytecode.ClassFile; */ public class DefinePackageHelper { - - private static enum SecuredPrivileged - { - JAVA_9 { - // definePackage has been discontinued for JAVA 9 - @Override - protected Package definePackage(ClassLoader loader, String name, String specTitle, + private static abstract class Helper { + abstract Package definePackage(ClassLoader loader, String name, String specTitle, + String specVersion, String specVendor, String implTitle, String implVersion, + String implVendor, URL sealBase) + throws IllegalArgumentException; + } + + private static class Java9 extends Helper { + // definePackage has been discontinued for JAVA 9 + @Override + Package definePackage(ClassLoader loader, String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, - String implVendor, URL sealBase) throws IllegalArgumentException - { - throw new RuntimeException("define package has been disabled for jigsaw"); - } - }, - JAVA_7 { - private final SecurityActions stack = SecurityActions.stack; - private final MethodHandle definePackage = getDefinePackageMethodHandle(); - private MethodHandle getDefinePackageMethodHandle() - { - if (stack.getCallerClass() != this.getClass()) - throw new IllegalAccessError("Access denied for caller."); - try { - return SecurityActions.getMethodHandle(ClassLoader.class, + String implVendor, URL sealBase) + throws IllegalArgumentException + { + throw new RuntimeException("define package has been disabled for jigsaw"); + } + }; + + private static class Java7 extends Helper { + private final SecurityActions stack = SecurityActions.stack; + private final MethodHandle definePackage = getDefinePackageMethodHandle(); + + private MethodHandle getDefinePackageMethodHandle() { + if (stack.getCallerClass() != this.getClass()) + throw new IllegalAccessError("Access denied for caller."); + try { + return SecurityActions.getMethodHandle(ClassLoader.class, "definePackage", new Class[] { String.class, String.class, String.class, String.class, String.class, String.class, String.class, URL.class }); - } catch (NoSuchMethodException e) { - throw new RuntimeException("cannot initialize", e); - } + } catch (NoSuchMethodException e) { + throw new RuntimeException("cannot initialize", e); } + } - @Override - protected Package definePackage(ClassLoader loader, String name, String specTitle, + @Override + Package definePackage(ClassLoader loader, String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, - String implVendor, URL sealBase) throws IllegalArgumentException { - if (stack.getCallerClass() != DefinePackageHelper.class) - throw new IllegalAccessError("Access denied for caller."); - try { - return (Package) definePackage.invokeWithArguments(loader, name, specTitle, + String implVendor, URL sealBase) + throws IllegalArgumentException + { + if (stack.getCallerClass() != DefinePackageHelper.class) + throw new IllegalAccessError("Access denied for caller."); + try { + return (Package) definePackage.invokeWithArguments(loader, name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase); - } catch (Throwable e) { - if (e instanceof IllegalArgumentException) throw (IllegalArgumentException) e; - if (e instanceof RuntimeException) throw (RuntimeException) e; - } - return null; + } catch (Throwable e) { + if (e instanceof IllegalArgumentException) throw (IllegalArgumentException) e; + if (e instanceof RuntimeException) throw (RuntimeException) e; } - }, - JAVA_OTHER { - private final SecurityActions stack = SecurityActions.stack; - private final Method definePackage = getDefinePackageMethod(); - private Method getDefinePackageMethod() - { - if (stack.getCallerClass() != this.getClass()) - throw new IllegalAccessError("Access denied for caller."); - try { - return SecurityActions.getDeclaredMethod(ClassLoader.class, + return null; + } + } + + private static class JavaOther extends Helper { + private final SecurityActions stack = SecurityActions.stack; + private final Method definePackage = getDefinePackageMethod(); + + private Method getDefinePackageMethod() { + if (stack.getCallerClass() != this.getClass()) + throw new IllegalAccessError("Access denied for caller."); + try { + return SecurityActions.getDeclaredMethod(ClassLoader.class, "definePackage", new Class[] { String.class, String.class, String.class, String.class, String.class, String.class, String.class, URL.class }); - } catch (NoSuchMethodException e) { - throw new RuntimeException("cannot initialize", e); - } + } catch (NoSuchMethodException e) { + throw new RuntimeException("cannot initialize", e); } + } - @Override - protected Package definePackage(ClassLoader loader, String name, String specTitle, + @Override + Package definePackage(ClassLoader loader, String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, - String implVendor, URL sealBase) throws IllegalArgumentException - { - if (stack.getCallerClass() != DefinePackageHelper.class) - throw new IllegalAccessError("Access denied for caller."); - try { - definePackage.setAccessible(true); - return (Package) definePackage.invoke(loader, new Object[] { + String implVendor, URL sealBase) + throws IllegalArgumentException + { + if (stack.getCallerClass() != DefinePackageHelper.class) + throw new IllegalAccessError("Access denied for caller."); + try { + definePackage.setAccessible(true); + return (Package) definePackage.invoke(loader, new Object[] { name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, sealBase }); - } catch (Throwable e) { - if (e instanceof InvocationTargetException) { - Throwable t = ((InvocationTargetException) e).getTargetException(); - if (t instanceof IllegalArgumentException) - throw (IllegalArgumentException) t; - } - if (e instanceof RuntimeException) throw (RuntimeException) e; - } - finally { - definePackage.setAccessible(false); + } catch (Throwable e) { + if (e instanceof InvocationTargetException) { + Throwable t = ((InvocationTargetException) e).getTargetException(); + if (t instanceof IllegalArgumentException) + throw (IllegalArgumentException) t; } - return null; + if (e instanceof RuntimeException) throw (RuntimeException) e; } - }; - - protected abstract Package definePackage(ClassLoader loader, String name, String specTitle, - String specVersion, String specVendor, String implTitle, String implVersion, - String implVendor, URL sealBase) throws IllegalArgumentException; - } - - private static final SecuredPrivileged privileged = ClassFile.MAJOR_VERSION >= ClassFile.JAVA_9 - ? SecuredPrivileged.JAVA_9 - : ClassFile.MAJOR_VERSION >= ClassFile.JAVA_7 - ? SecuredPrivileged.JAVA_7 - : SecuredPrivileged.JAVA_OTHER; + finally { + definePackage.setAccessible(false); + } + return null; + } + }; + private static final Helper privileged + = ClassFile.MAJOR_VERSION >= ClassFile.JAVA_9 + ? new Java9() : ClassFile.MAJOR_VERSION >= ClassFile.JAVA_7 + ? new Java7() : new JavaOther(); /** * Defines a new package. If the package is already defined, this method |