You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by fm...@apache.org on 2009/10/29 11:37:14 UTC

svn commit: r830885 - in /felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper: ActivateMethod.java BaseMethod.java BindMethod.java

Author: fmeschbe
Date: Thu Oct 29 10:37:14 2009
New Revision: 830885

URL: http://svn.apache.org/viewvc?rev=830885&view=rev
Log:
FELIX-1686 Refactor BaseMethod.getMethod to not throw NoSuchMethodException
anymore but just return null. The call may then better act upon this null
return value instead of catching and ignoring the exception

Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java?rev=830885&r1=830884&r2=830885&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java Thu Oct 29 10:37:14 2009
@@ -43,11 +43,11 @@
 
         try
         {
-            return getSingleParameterMethod( targetClass, acceptPrivate, acceptPackage );
-        }
-        catch ( NoSuchMethodException nsme )
-        {
-            // ignore for now
+            final Method method = getSingleParameterMethod( targetClass, acceptPrivate, acceptPackage );
+            if ( method != null )
+            {
+                return method;
+            }
         }
         catch ( SuitableMethodNotAccessibleException smnae )
         {
@@ -81,10 +81,6 @@
                     // find the declared method in this class
                     return getMethod( targetClass, getMethodName(), null, acceptPrivate, acceptPackage );
                 }
-                catch ( NoSuchMethodException nsme )
-                {
-                    // ignore for now
-                }
                 catch ( SuitableMethodNotAccessibleException smnae )
                 {
                     suitableMethodNotAccessible = true;
@@ -143,24 +139,38 @@
     }
 
 
+    /**
+     * Returns a method taking a single parameter of one of the
+     * {@link #getAcceptedParameterTypes()} or <code>null</code> if no such
+     * method exists.
+     *
+     * @param targetClass The class in which to look for the method. Only this
+     *      class is searched for the method.
+     * @param acceptPrivate <code>true</code> if private methods should be
+     *      considered.
+     * @param acceptPackage <code>true</code> if package private methods should
+     *      be considered.
+     * @return The requested method or <code>null</code> if no acceptable method
+     *      can be found in the target class.
+     * @throws SuitableMethodNotAccessibleException If a suitable method was
+     *      found which is not accessible
+     * @throws InvocationTargetException If an unexpected Throwable is caught
+     *      trying to find the requested method.
+     */
     private Method getSingleParameterMethod( final Class targetClass, final boolean acceptPrivate,
-        final boolean acceptPackage ) throws SuitableMethodNotAccessibleException, InvocationTargetException,
-        NoSuchMethodException
+        final boolean acceptPackage ) throws SuitableMethodNotAccessibleException, InvocationTargetException
     {
         SuitableMethodNotAccessibleException ex = null;
+        Method singleParameterMethod = null;
         final Class[] acceptedTypes = getAcceptedParameterTypes();
-        for ( int i = 0; i < acceptedTypes.length; i++ )
+        for ( int i = 0; singleParameterMethod == null && i < acceptedTypes.length; i++ )
         {
             try
             {
                 // find the declared method in this class
-                return getMethod( targetClass, getMethodName(), new Class[]
+                singleParameterMethod = getMethod( targetClass, getMethodName(), new Class[]
                     { acceptedTypes[i] }, acceptPrivate, acceptPackage );
             }
-            catch ( NoSuchMethodException nsme )
-            {
-                // ignore for now
-            }
             catch ( SuitableMethodNotAccessibleException thrown )
             {
                 ex = thrown;
@@ -168,12 +178,15 @@
 
         }
 
+        // rethrow if we looked for all method signatures and only found
+        // one or more which would be suitable but not accessible
         if ( ex != null )
         {
             throw ex;
         }
 
-        throw new NoSuchMethodException();
+        // no method with a matching single parameter has been found
+        return singleParameterMethod;
     }
 
 

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java?rev=830885&r1=830884&r2=830885&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java Thu Oct 29 10:37:14 2009
@@ -279,18 +279,17 @@
      * @param parameterTypes The parameters to the method. Passing
      *      <code>null</code> is equivalent to using an empty array.
      *
-     * @return The named method with enforced accessibility
+     * @return The named method with enforced accessibility or <code>null</code>
+     *      if no such method exists in the class.
      *
-     * @throws NoSuchMethodException If no public or protected method with
-     *      the given name can be found in the class or any of its super classes.
      * @throws SuitableMethodNotAccessibleException If method with the given
      *      name taking the parameters is found in the class but the method
      *      is not accessible.
      * @throws InvocationTargetException If an unexpected Throwable is caught
      *      trying to access the desired method.
      */
-    public static Method getMethod( Class clazz, String name, Class[] parameterTypes, boolean acceptPrivate,
-        boolean acceptPackage ) throws NoSuchMethodException, SuitableMethodNotAccessibleException,
+    public /* static */ Method getMethod( Class clazz, String name, Class[] parameterTypes, boolean acceptPrivate,
+        boolean acceptPackage ) throws SuitableMethodNotAccessibleException,
         InvocationTargetException
     {
         try
@@ -303,11 +302,33 @@
             {
                 return method;
             }
+
+            // the method would fit the requirements but is not acceptable
+            throw new SuitableMethodNotAccessibleException();
         }
         catch ( NoSuchMethodException nsme )
         {
-            // forward to caller
-            throw nsme;
+            // thrown if no method is declared with the given name and
+            // parameters
+        }
+        catch ( NoClassDefFoundError cdfe )
+        {
+            // may be thrown if a method would be found but the signature
+            // contains throws declaration for an exception which cannot
+            // be loaded
+            // FELIX... TODO
+            StringBuffer buf = new StringBuffer();
+            buf.append( "Failure loooking up method " ).append( name ).append( '(' );
+            for ( int i = 0; parameterTypes != null && i < parameterTypes.length; i++ )
+            {
+                buf.append( parameterTypes[i].getName() );
+                if ( i > 0 )
+                {
+                    buf.append( ", " );
+                }
+            }
+            buf.append( ") in class class " ).append( clazz.getName() ).append( ". Assuming no such method." );
+            getComponentManager().log( LogService.LOG_WARNING, buf.toString(), cdfe );
         }
         catch ( Throwable throwable )
         {
@@ -316,8 +337,8 @@
             throw new InvocationTargetException( throwable, "Unexpected problem trying to get method " + name );
         }
 
-        // suitable method found which is not accessible
-        throw new SuitableMethodNotAccessibleException();
+        // cuaght and ignored exception, assume no method and continue search
+        return null;
     }
 
 

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java?rev=830885&r1=830884&r2=830885&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java Thu Oct 29 10:37:14 2009
@@ -232,18 +232,8 @@
     private Method getServiceReferenceMethod( final Class targetClass, boolean acceptPrivate, boolean acceptPackage )
         throws SuitableMethodNotAccessibleException, InvocationTargetException
     {
-        try
-        {
-            return getMethod( targetClass, getMethodName(), new Class[]
-                { SERVICE_REFERENCE_CLASS }, acceptPrivate, acceptPackage );
-        }
-        catch ( NoSuchMethodException e )
-        {
-            // the named method could not be found
-        }
-
-        // no method taking service reference
-        return null;
+        return getMethod( targetClass, getMethodName(), new Class[]
+            { SERVICE_REFERENCE_CLASS }, acceptPrivate, acceptPackage );
     }
 
 
@@ -267,18 +257,8 @@
     private Method getServiceObjectMethod( final Class targetClass, final Class parameterClass, boolean acceptPrivate,
         boolean acceptPackage ) throws SuitableMethodNotAccessibleException, InvocationTargetException
     {
-        try
-        {
-            return getMethod( targetClass, getMethodName(), new Class[]
-                { parameterClass }, acceptPrivate, acceptPackage );
-        }
-        catch ( NoSuchMethodException nsme )
-        {
-            // no method taking service object
-        }
-
-        // no method taking service object
-        return null;
+        return getMethod( targetClass, getMethodName(), new Class[]
+            { parameterClass }, acceptPrivate, acceptPackage );
     }
 
 
@@ -372,18 +352,8 @@
         boolean acceptPrivate, boolean acceptPackage ) throws SuitableMethodNotAccessibleException,
         InvocationTargetException
     {
-        try
-        {
-            return getMethod( targetClass, getMethodName(), new Class[]
-                { parameterClass, MAP_CLASS }, acceptPrivate, acceptPackage );
-        }
-        catch ( NoSuchMethodException nsme )
-        {
-            // no method taking service object
-        }
-
-        // no method taking service object
-        return null;
+        return getMethod( targetClass, getMethodName(), new Class[]
+            { parameterClass, MAP_CLASS }, acceptPrivate, acceptPackage );
     }