felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickh...@apache.org
Subject svn commit: r752985 - /felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleImpl.java
Date Thu, 12 Mar 2009 19:50:35 GMT
Author: rickhall
Date: Thu Mar 12 19:50:35 2009
New Revision: 752985

URL: http://svn.apache.org/viewvc?rev=752985&view=rev
Log:
Applied patch (FELIX-962) to further improve our class loading delegation
hack.

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleImpl.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleImpl.java?rev=752985&r1=752984&r2=752985&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleImpl.java
(original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/searchpolicy/ModuleImpl.java
Thu Mar 12 19:50:35 2009
@@ -18,6 +18,7 @@
  */
 package org.apache.felix.framework.searchpolicy;
 
+import java.lang.reflect.InvocationTargetException;
 import org.apache.felix.moduleloader.*;
 import java.io.IOException;
 import java.io.InputStream;
@@ -1244,25 +1245,71 @@
         return null;
     }
 
-    private boolean isClassNotLoadedFromBundle(Class clazz) throws ClassNotFoundException
+    private boolean isClassNotLoadedFromBundle(Class clazz)
     {
-        int idx = clazz.getName().lastIndexOf('$');
-        if (idx > 0)
+        // If this is an inner class, try to get the enclosing class
+        // because we can assume that inner classes of class loaders
+        // are really just the class loader and we should ignore them.
+        clazz = getEnclosingClass(clazz);
+        return (this.getClass().getClassLoader() != clazz.getClassLoader())
+            && !ClassLoader.class.isAssignableFrom(clazz)
+            && !Class.class.equals(clazz)
+            && !Proxy.class.equals(clazz);
+    }
+
+    private volatile static Method getEnclosingClassMethod = null;
+    private volatile static boolean getEnclosingClassMethodInitialized = false;
+
+    private static Class getEnclosingClass(Class clazz)
+    {
+        if (!getEnclosingClassMethodInitialized)
+        {
+	        // Check if we have the getEnclosingClass() method available from
+	        // JDK 1.5.
+	        try
+	        {
+	            getEnclosingClassMethod =
+                    Class.class.getDeclaredMethod("getEnclosingClass", null);
+	        }
+	        catch (NoSuchMethodException ex)
+	        {
+	            // Ignore it then.
+	        }
+			getEnclosingClassMethodInitialized = true;
+		}
+        if (getEnclosingClassMethod != null)
+        {
+            try
+            {
+                Class enclosing = (Class) getEnclosingClassMethod.invoke(clazz, null);
+                clazz = (enclosing != null) ? enclosing : clazz;
+            }
+            catch (Throwable t)
+            {
+                // Ignore everything and use original class.
+            }
+        }
+        else
         {
-            if (clazz.getClassLoader() != null)
+            // If we are not on JDK 1.5, try to figure out enclosing class.
+            int idx = clazz.getName().lastIndexOf('$');
+            if (idx > 0)
             {
-                Class outerClass = clazz.getClassLoader().loadClass(
-                    clazz.getName().substring(0, idx));
-                if (outerClass != null)
+                ClassLoader cl = clazz.getClassLoader() != null ? clazz.getClassLoader()
: ClassLoader.getSystemClassLoader();
+                try
                 {
-                    clazz = outerClass;
+                    Class enclosing = cl.loadClass(clazz.getName().substring(0, idx));
+                    clazz = (enclosing != null) ? enclosing : clazz;
+                }
+                catch (Throwable t)
+                {
+                    // Ignore all problems since we are trying to load a class
+                    // inside the class loader and this can lead to
+                    // ClassCircularityError, for example.
                 }
             }
         }
-        return this.getClass().getClassLoader() != clazz.getClassLoader()
-            && !ClassLoader.class.isAssignableFrom(clazz)
-            && !Class.class.equals(clazz)
-            && !Proxy.class.equals(clazz);
+        return clazz;
     }
 
     private boolean shouldBootDelegate(String pkgName)



Mime
View raw message