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
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@830885 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java b/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java
index adb80a4..b722013 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java
@@ -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;
}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java b/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java
index 839779c..0c5c508 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java
@@ -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;
}
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java b/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java
index 7701c79..a931eb7 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java
@@ -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 );
}