commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r332607 - /jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistUtils.java
Date Fri, 11 Nov 2005 17:17:46 GMT
Author: jcarman
Date: Fri Nov 11 09:17:43 2005
New Revision: 332607

URL: http://svn.apache.org/viewcvs?rev=332607&view=rev
Log:
Bug fix for multiple classloader environments.

Modified:
    jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistUtils.java

Modified: jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistUtils.java?rev=332607&r1=332606&r2=332607&view=diff
==============================================================================
--- jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistUtils.java
(original)
+++ jakarta/commons/sandbox/proxy/trunk/src/java/org/apache/commons/proxy/factory/javassist/JavassistUtils.java
Fri Nov 11 09:17:43 2005
@@ -20,9 +20,13 @@
 import javassist.ClassPool;
 import javassist.CtClass;
 import javassist.CtField;
+import javassist.LoaderClassPath;
 import javassist.NotFoundException;
-import org.apache.commons.proxy.exception.ObjectProviderException;
 import org.apache.commons.proxy.ProxyUtils;
+import org.apache.commons.proxy.exception.ObjectProviderException;
+
+import java.util.HashSet;
+import java.util.Set;
 
 /**
  * @author James Carman
@@ -33,31 +37,42 @@
 //----------------------------------------------------------------------------------------------------------------------
 // Fields
 //----------------------------------------------------------------------------------------------------------------------
-
     public static final String DEFAULT_BASE_NAME = "JavassistUtilsGenerated";
     private static int classNumber = 0;
-    private static final ClassPool classPool = ClassPool.getDefault();
-
+    private static final ClassPool classPool = new ClassPool();
+    static
+    {
+        classPool.appendClassPath( new LoaderClassPath( ClassLoader.getSystemClassLoader()
) );
+    }
+    private static final Set classLoaders = new HashSet();
 //----------------------------------------------------------------------------------------------------------------------
 // Static Methods
 //----------------------------------------------------------------------------------------------------------------------
 
-    public static void addField( Class fieldType, String fieldName, CtClass enclosingClass
) throws
-                                                                                        
    CannotCompileException
+    public static void addField( Class fieldType, String fieldName, CtClass enclosingClass
) throws                                                                                 
      CannotCompileException
     {
         enclosingClass.addField( new CtField( resolve( fieldType ), fieldName, enclosingClass
) );
     }
 
     public static CtClass resolve( Class clazz )
     {
-        try
+        synchronized( classLoaders )
         {
-            return classPool.get( ProxyUtils.getJavaClassName( clazz ) );
-        }
-        catch( NotFoundException e )
-        {
-            throw new ObjectProviderException(
-                    "Unable to find class " + clazz.getName() + " in default Javassist class
pool.", e );
+            try
+            {
+                final ClassLoader loader = clazz.getClassLoader();
+                if( loader != null && !classLoaders.contains( loader ) )
+                {
+                    classLoaders.add( loader );
+                    classPool.appendClassPath( new LoaderClassPath( loader ) );
+                }
+                return classPool.get( ProxyUtils.getJavaClassName( clazz ) );
+            }
+            catch( NotFoundException e )
+            {
+                throw new ObjectProviderException(
+                        "Unable to find class " + clazz.getName() + " in default Javassist
class pool.", e );
+            }
         }
     }
 
@@ -69,7 +84,7 @@
             ctClass.addInterface( resolve( proxyInterface ) );
         }
     }
-   
+
     public static CtClass createClass( Class superclass )
     {
         return createClass( DEFAULT_BASE_NAME, superclass );



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message