diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2006-08-07 15:48:31 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2006-08-07 15:48:31 +0000 |
commit | 2405e6804619f75ef93c9a8e9ba366e595f4899d (patch) | |
tree | a5fac709b60ce7e9f161fe5b55e144e24283cdcf /src/main/javassist/util | |
parent | a62c5816078737cc3a392beaa88c2c03d95ea961 (diff) | |
download | javassist-2405e6804619f75ef93c9a8e9ba366e595f4899d.tar.gz javassist-2405e6804619f75ef93c9a8e9ba366e595f4899d.zip |
fixed the bug reported as JASSIST-23.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@305 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/util')
-rw-r--r-- | src/main/javassist/util/proxy/FactoryHelper.java | 54 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/ProxyFactory.java | 15 |
2 files changed, 62 insertions, 7 deletions
diff --git a/src/main/javassist/util/proxy/FactoryHelper.java b/src/main/javassist/util/proxy/FactoryHelper.java index b00b9a93..21038fe9 100644 --- a/src/main/javassist/util/proxy/FactoryHelper.java +++ b/src/main/javassist/util/proxy/FactoryHelper.java @@ -21,8 +21,10 @@ import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.security.ProtectionDomain; import javassist.CannotCompileException; +import javassist.CtClass; import javassist.bytecode.ClassFile; /** @@ -32,6 +34,24 @@ import javassist.bytecode.ClassFile; * @see ProxyFactory */ public class FactoryHelper { + private static java.lang.reflect.Method defineClass1, defineClass2; + + static { + try { + Class cl = Class.forName("java.lang.ClassLoader"); + defineClass1 = cl.getDeclaredMethod("defineClass", + new Class[] { String.class, byte[].class, + int.class, int.class }); + + defineClass2 = cl.getDeclaredMethod("defineClass", + new Class[] { String.class, byte[].class, + int.class, int.class, ProtectionDomain.class }); + } + catch (Exception e) { + throw new RuntimeException("cannot initialize"); + } + } + /** * Returns an index for accessing arrays in this class. * @@ -101,19 +121,41 @@ public class FactoryHelper { /** * Loads a class file by a given class loader. + * This method uses a default protection domain for the class + * but it may not work with a security manager or a sigend jar file. + * + * @see #toClass(CtClass,ClassLoader,ProtectionDomain) */ public static Class toClass(ClassFile cf, ClassLoader loader) + throws CannotCompileException + { + return toClass(cf, loader, null); + } + + /** + * Loads a class file by a given class loader. + * + * @param domain if it is null, a default domain is used. + */ + public static Class toClass(ClassFile cf, ClassLoader loader, ProtectionDomain domain) throws CannotCompileException { try { byte[] b = toBytecode(cf); - Class cl = Class.forName("java.lang.ClassLoader"); - java.lang.reflect.Method method = cl.getDeclaredMethod( - "defineClass", new Class[] { String.class, byte[].class, - Integer.TYPE, Integer.TYPE }); + java.lang.reflect.Method method; + Object[] args; + if (domain == null) { + method = defineClass1; + args = new Object[] { cf.getName(), b, new Integer(0), + new Integer(b.length) }; + } + else { + method = defineClass2; + args = new Object[] { cf.getName(), b, new Integer(0), + new Integer(b.length), domain }; + } + method.setAccessible(true); - Object[] args = new Object[] { cf.getName(), b, new Integer(0), - new Integer(b.length) }; Class clazz = (Class)method.invoke(loader, args); method.setAccessible(false); return clazz; diff --git a/src/main/javassist/util/proxy/ProxyFactory.java b/src/main/javassist/util/proxy/ProxyFactory.java index bf29e539..fdd397ad 100644 --- a/src/main/javassist/util/proxy/ProxyFactory.java +++ b/src/main/javassist/util/proxy/ProxyFactory.java @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Constructor; import java.lang.reflect.Member; import java.lang.reflect.Modifier; +import java.security.ProtectionDomain; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -166,7 +167,7 @@ public class ProxyFactory { if (writeDirectory != null) FactoryHelper.writeFile(cf, writeDirectory); - thisClass = FactoryHelper.toClass(cf, cl); + thisClass = FactoryHelper.toClass(cf, cl, getDomain()); setHandler(); } catch (CannotCompileException e) { @@ -194,6 +195,18 @@ public class ProxyFactory { return loader; } + protected ProtectionDomain getDomain() { + Class clazz; + if (superClass != null && !superClass.getName().equals("java.lang.Object")) + clazz = superClass; + else if (interfaces != null && interfaces.length > 0) + clazz = interfaces[0]; + else + clazz = this.getClass(); + + return clazz.getProtectionDomain(); + } + /** * Creates a proxy class and returns an instance of that class. * |