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

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    Loader.java ClassPool.java
                        CtClass.java
  Log:
  fixed the bug reported as JASSIST-23.
  
  Revision  Changes    Path
  1.18      +17 -1     javassist/src/main/javassist/Loader.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Loader.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/Loader.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -b -r1.17 -r1.18
  --- Loader.java	29 May 2006 16:32:17 -0000	1.17
  +++ Loader.java	7 Aug 2006 15:48:31 -0000	1.18
  @@ -18,6 +18,7 @@
   import java.io.*;
   import java.util.Hashtable;
   import java.util.Vector;
  +import java.security.ProtectionDomain;
   
   /**
    * The class loader for Javassist.
  @@ -136,6 +137,7 @@
       private Vector notDefinedPackages; // must be atomic.
       private ClassPool source;
       private Translator translator;
  +    private ProtectionDomain domain; 
   
       /**
        * Specifies the algorithm of class loading.
  @@ -183,6 +185,7 @@
           notDefinedPackages = new Vector();
           source = cp;
           translator = null;
  +        domain = null;
           delegateLoadingOf("javassist.Loader");
       }
   
  @@ -202,6 +205,16 @@
       }
   
       /**
  +     * Sets the protection domain for the classes handled by this class
  +     * loader.  Without registering an appropriate protection domain,
  +     * the program loaded by this loader will not work with a security
  +     * manager or a signed jar file.
  +     */
  +    public void setDomain(ProtectionDomain d) {
  +        domain = d;
  +    }
  +
  +    /**
        * Sets the soruce <code>ClassPool</code>.
        */
       public void setClassPool(ClassPool cp) {
  @@ -362,7 +375,10 @@
                   }
           }
   
  +        if (domain == null)
           return defineClass(name, classfile, 0, classfile.length);
  +        else
  +            return defineClass(name, classfile, 0, classfile.length, domain);
       }
   
       protected Class loadClassByDelegation(String name)
  
  
  
  1.54      +76 -14    javassist/src/main/javassist/ClassPool.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ClassPool.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/ClassPool.java,v
  retrieving revision 1.53
  retrieving revision 1.54
  diff -u -b -r1.53 -r1.54
  --- ClassPool.java	23 Mar 2006 13:52:47 -0000	1.53
  +++ ClassPool.java	7 Aug 2006 15:48:31 -0000	1.54
  @@ -21,6 +21,7 @@
   import java.io.InputStream;
   import java.io.OutputStream;
   import java.net.URL;
  +import java.security.ProtectionDomain;
   import java.util.Hashtable;
   import java.util.Iterator;
   import java.util.ArrayList;
  @@ -61,6 +62,24 @@
    * @see javassist.ClassPath
    */
   public class ClassPool {
  +    // used by toClass().
  +    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 ClassPool");
  +        }
  +    }
   
       /**
        * Determines the search order.
  @@ -752,21 +771,27 @@
        * Once this method is called, further modifications are not
        * allowed any more.
        * To load the class, this method uses the context class loader
  -     * of the current thread.  If the program is running on some application
  -     * server, the context class loader might be inappropriate to load the
  -     * class.
  +     * of the current thread.  It is obtained by calling
  +     * <code>getClassLoader()</code>.  
        * 
  -     * <p>This can be changed by subclassing the pool and changing
  +     * <p>This behavior can be changed by subclassing the pool and changing
        * the <code>getClassLoader()</code> method.
  +     * If the program is running on some application
  +     * 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.
        *
  -     * @see #toClass(CtClass, java.lang.ClassLoader)
  +     * <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.
  +     *
  +     * @see #toClass(CtClass, java.lang.ClassLoader, ProtectionDomain)
        * @see #getClassLoader()
        */
       public Class toClass(CtClass clazz) throws CannotCompileException {
  -        return toClass(clazz, getClassLoader()); 
  +        return toClass(clazz, getClassLoader(), null); 
       }
   
       /**
  @@ -793,6 +818,23 @@
   
       /**
        * Converts the class to a <code>java.lang.Class</code> object.
  +     * Do not override this method any more at a subclass because
  +     * <code>toClass(CtClass)</code> never calls this method.
  +     *
  +     * <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.
  +     * 
  +     * @deprecated      Replaced by {@link #toClass(CtClass,ClassLoader,ProtectionDomain)}
  +     */
  +    public final Class toClass(CtClass ct, ClassLoader loader)
  +        throws CannotCompileException
  +    {
  +        return toClass(ct, loader, null);
  +    }
  +
  +    /**
  +     * Converts the class to a <code>java.lang.Class</code> object.
        * Once this method is called, further modifications are not allowed
        * any more.
        *
  @@ -802,24 +844,44 @@
        * on the class loader is invoked through the reflection API,
        * the caller must have permissions to do that.
        *
  +     * <p>An easy way to obtain <code>ProtectionDomain</code> object is
  +     * to call <code>getProtectionDomain()</code>
  +     * in <code>java.lang.Class</code>.  It returns the domain that the
  +     * class belongs to.
  +     *
        * <p>This method is provided for convenience.  If you need more
        * complex functionality, you should write your own class loader.
        *
        * @param loader        the class loader used to load this class.
  +     *                      For example, the loader returned by
  +     *                      <code>getClassLoader()</code> can be used
  +     *                      for this parameter.
  +     * @param domain        the protection domain for the class.
  +     *                      If it is null, the default domain created
  +     *                      by <code>java.lang.ClassLoader</code> is used.
  +     *
  +     * @see #getContextClassLoader()
  +     * @since 3.3
        */
  -    public Class toClass(CtClass ct, ClassLoader loader)
  +    public Class toClass(CtClass ct, ClassLoader loader, ProtectionDomain domain)
           throws CannotCompileException
       {
           try {
               byte[] b = ct.toBytecode();
  -            Class cl = Class.forName("java.lang.ClassLoader");
  -            java.lang.reflect.Method method =
  -                cl.getDeclaredMethod("defineClass",
  -                                new Class[] { String.class, byte[].class,
  -                                              int.class, int.class });
  -            method.setAccessible(true);
  -            Object[] args = new Object[] { ct.getName(), b, new Integer(0),
  +            java.lang.reflect.Method method;
  +            Object[] args;
  +            if (domain == null) {
  +                method = defineClass1;
  +                args = new Object[] { ct.getName(), b, new Integer(0),
                                              new Integer(b.length)};
  +            }
  +            else {
  +                method = defineClass2;
  +                args = new Object[] { ct.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;
  
  
  
  1.76      +35 -1     javassist/src/main/javassist/CtClass.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CtClass.java
  ===================================================================
  RCS file: /cvsroot/jboss/javassist/src/main/javassist/CtClass.java,v
  retrieving revision 1.75
  retrieving revision 1.76
  diff -u -b -r1.75 -r1.76
  --- CtClass.java	6 Aug 2006 06:32:45 -0000	1.75
  +++ CtClass.java	7 Aug 2006 15:48:31 -0000	1.76
  @@ -23,6 +23,7 @@
   import java.io.IOException;
   import java.io.OutputStream;
   import java.net.URL;
  +import java.security.ProtectionDomain;
   import java.util.Collection;
   import javassist.bytecode.ClassFile;
   import javassist.bytecode.Descriptor;
  @@ -1025,6 +1026,10 @@
        * <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.
  +     *
        * @see #toClass(java.lang.ClassLoader)
        * @see ClassPool#toClass(CtClass)
        */
  @@ -1043,6 +1048,11 @@
        * on the class loader is invoked through the reflection API,
        * the caller must have permissions to do that.
        *
  +     * <p>An easy way to obtain <code>ProtectionDomain</code> object is
  +     * to call <code>getProtectionDomain()</code>
  +     * in <code>java.lang.Class</code>.  It returns the domain that
  +     * the class belongs to.
  +     *
        * <p>This method is provided for convenience.  If you need more
        * complex functionality, you should write your own class loader.
        *
  @@ -1050,9 +1060,33 @@
        * in <code>ClassPool</code>.
        *
        * @param loader        the class loader used to load this class.
  +     *                      If it is null, the class loader returned by
  +     *                      {@link ClassPool#getClassLoader()} is used.
  +     * @param domain        the protection domain that the class belongs to.
  +     *                      If it is null, the default domain created
  +     *                      by <code>java.lang.ClassLoader</code> is used.
        * @see ClassPool#toClass(CtClass,java.lang.ClassLoader)
        */
  -    public Class toClass(ClassLoader loader)
  +    public Class toClass(ClassLoader loader, ProtectionDomain domain)
  +        throws CannotCompileException
  +    {
  +        ClassPool cp = getClassPool();
  +        if (loader == null)
  +            loader = cp.getClassLoader();
  +
  +        return cp.toClass(this, loader, domain);
  +    }
  +
  +    /**
  +     * Converts this class to a <code>java.lang.Class</code> object.
  +     *
  +     * <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.
  +     *
  +     * @deprecated      Replaced by {@link #toClass(ClassLoader,ProtectionDomain)}
  +     */
  +    public final Class toClass(ClassLoader loader)
           throws CannotCompileException
       {
           return getClassPool().toClass(this, loader);
  
  
  



More information about the jboss-cvs-commits mailing list