[jboss-cvs] javassist/src/main/javassist/util/proxy ...

Shigeru Chiba chiba at is.titech.ac.jp
Mon Aug 7 11:48:31 EDT 2006


  User: chiba   
  Date: 06/08/07 11:48:31

  Modified:    src/main/javassist/util/proxy   ProxyFactory.java
                        FactoryHelper.java
  Log:
  fixed the bug reported as JASSIST-23.
  
  Revision  Changes    Path
  1.13      +14 -1     javassist/src/main/javassist/util/proxy/ProxyFactory.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ProxyFactory.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/util/proxy/ProxyFactory.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -b -r1.12 -r1.13
  --- ProxyFactory.java	18 Jul 2006 17:51:04 -0000	1.12
  +++ ProxyFactory.java	7 Aug 2006 15:48:31 -0000	1.13
  @@ -21,6 +21,7 @@
   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 @@
                   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 @@
           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.
        *
  
  
  
  1.3       +48 -6     javassist/src/main/javassist/util/proxy/FactoryHelper.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: FactoryHelper.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/util/proxy/FactoryHelper.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- FactoryHelper.java	11 Jan 2006 06:45:55 -0000	1.2
  +++ FactoryHelper.java	7 Aug 2006 15:48:31 -0000	1.3
  @@ -21,8 +21,10 @@
   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 @@
    * @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 @@
   
       /**
        * 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 });
  -            method.setAccessible(true);
  -            Object[] args = new Object[] { cf.getName(), b, new Integer(0),
  +            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);
               Class clazz = (Class)method.invoke(loader, args);
               method.setAccessible(false);
               return clazz;
  
  
  



More information about the jboss-cvs-commits mailing list