felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r440172 - /incubator/felix/trunk/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java
Date Mon, 04 Sep 2006 20:27:46 GMT
Author: marrs
Date: Mon Sep  4 13:27:45 2006
New Revision: 440172

URL: http://svn.apache.org/viewvc?view=rev&rev=440172
Log:
FELIX-137 Implemented a new algorithm to invoke a callback method. This one should be able
to discover all methods in a class and its superclasses (except for private methods in superclasses).

Modified:
    incubator/felix/trunk/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java

Modified: incubator/felix/trunk/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java?view=diff&rev=440172&r1=440171&r2=440172
==============================================================================
--- incubator/felix/trunk/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java
(original)
+++ incubator/felix/trunk/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java
Mon Sep  4 13:27:45 2006
@@ -19,6 +19,7 @@
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.lang.reflect.Proxy;
 
 import org.osgi.framework.BundleContext;
@@ -247,56 +248,32 @@
         return callbackInstance;
     }
     
-    // TODO a lot of things in this method can be cached instead of done each time
-    // TODO Richard had an example where he could not invoke a private method
     private void invokeCallbackMethod(Object instance, String methodName, ServiceReference
reference, Object service) throws NoSuchMethodException {
-        Method method = null;
-        Class clazz = instance.getClass();
-        while (clazz != null) {
-	        AccessibleObject.setAccessible(clazz.getDeclaredMethods(), true);
-	        try {
-	            try {
-	                method = clazz.getDeclaredMethod(methodName, new Class[] {ServiceReference.class,
Object.class});
-	                method.invoke(instance, new Object[] {reference, service});
-	                return;
-	            }
-	            catch (NoSuchMethodException e) {
-	                try {
-	                    method = clazz.getDeclaredMethod(methodName, new Class[] {ServiceReference.class});
-	                    method.invoke(instance, new Object[] {reference});
-	                    return;
-	                } 
-	                catch (NoSuchMethodException e1) {
-	                    try {
-	                        method = clazz.getDeclaredMethod(methodName, new Class[] {Object.class});
-	                        method.invoke(instance, new Object[] {service});
-	                        return;
-	                    } 
-	                    catch (NoSuchMethodException e2) {
-	                        try {
-	                            method = clazz.getDeclaredMethod(methodName, new Class[] {m_trackedServiceName});
-	                            method.invoke(instance, new Object[] {service});
-	                            return;
-	                        } 
-	                        catch (NoSuchMethodException e3) {
-	                            method = clazz.getDeclaredMethod(methodName, null);
-	                            method.invoke(instance, null);
-	                            return;
-	                        }
-	                    }
-	                }
-	            }
-	        } 
-	        catch (IllegalArgumentException e1) {
-	        	// ignore this exception
-	        } 
-	        catch (IllegalAccessException e1) {
-	        	// ignore this exception
-	        } 
-	        catch (InvocationTargetException e1) {
-	        	// ignore this exception
-	        }
-	        clazz = clazz.getSuperclass();
+        invokeMethod(instance, instance.getClass(), methodName, 
+            new Class[][] {{ServiceReference.class, Object.class}, {ServiceReference.class},
{Object.class}, {m_trackedServiceName}, {}}, 
+            new Object[][] {{reference, service}, {reference}, {service}, {service}, {}},
+            false);
+    }
+
+    private void invokeMethod(Object object, Class clazz, String name, Class[][] signatures,
Object[][] parameters, boolean isSuper) {
+        Method m = null;
+        for (int i = 0; i < signatures.length; i++) {
+            Class[] signature = signatures[i];
+            try {
+                m = clazz.getDeclaredMethod(name, signature);
+                if (!(isSuper && Modifier.isPrivate(m.getModifiers()))) {
+                    m.setAccessible(true);
+                    m.invoke(object, parameters[i]);
+                    return;
+                }
+            }
+            catch (Exception e) {
+                // ignore any exception and keep looking for a method
+            }
+            Class c = clazz.getSuperclass();
+            if (c != null) {
+                invokeMethod(object, c, name, signatures, parameters, true);
+            }
         }
     }
     



Mime
View raw message