FELIX-3325 Apply patch by David Jencks (thanks!) to fix some issues with method resolution and code completeness in ReferenceMetadata.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1237274 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 515557a..6b9af96 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
@@ -87,7 +87,10 @@
                 try
                 {
                     // find the declared method in this class
-                    return getMethod( targetClass, getMethodName(), null, acceptPrivate, acceptPackage );
+                    Method m = getMethod( targetClass, getMethodName(), null, acceptPrivate, acceptPackage );
+                    if ( m != null ) {
+                        return m;
+                    }
                 }
                 catch ( SuitableMethodNotAccessibleException smnae )
                 {
@@ -188,7 +191,7 @@
 
         // rethrow if we looked for all method signatures and only found
         // one or more which would be suitable but not accessible
-        if ( ex != null )
+        if ( singleParameterMethod == null && ex != null )
         {
             throw ex;
         }
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 32b95d9..99a3c40 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
@@ -351,6 +351,10 @@
                 getComponentManager().log( LogService.LOG_WARNING, buf.toString(), cdfe );
             }
         }
+        catch ( SuitableMethodNotAccessibleException e)
+        {
+            throw e;
+        }
         catch ( Throwable throwable )
         {
             // unexpected problem accessing the method, don't let everything
@@ -358,7 +362,7 @@
             throw new InvocationTargetException( throwable, "Unexpected problem trying to get method " + name );
         }
 
-        // cuaght and ignored exception, assume no method and continue search
+        // caught and ignored exception, assume no method and continue search
         return null;
     }
 
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/metadata/ReferenceMetadata.java b/scr/src/main/java/org/apache/felix/scr/impl/metadata/ReferenceMetadata.java
index aeb4c16..7b0a5c7 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/metadata/ReferenceMetadata.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/metadata/ReferenceMetadata.java
@@ -428,6 +428,8 @@
                     null );
             m_updated = null;
         }
+
+        m_validated = true;
     }
 
 }
\ No newline at end of file
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java b/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java
index 13f7639..0b74bba 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/helper/ActivateMethodTest.java
@@ -208,6 +208,34 @@
         ensureMethodNotFoundMethod( level3, "activate_suitable" );
     }
 
+    public void test_unsuitable_method_selection() throws Exception
+    {
+        //check that finding an unsuitable method does not prevent finding
+        // a lower precedence suitable method.
+
+        checkMethod( level2, "activate_comp_unsuitable" );
+
+        checkMethod( level3, "activate_comp_unsuitable" );
+    }
+
+    public void test_precedence() throws Exception
+    {
+        //All tested methods are only in base.  They differ in argurments and visibility.
+        //R4.2 compendium  112.5.8
+        //private method, arg ComponentContext
+        checkMethod( base, "activate_precedence_1", "activate_precedence_1_comp" );
+        //package method, arg BundleContext
+        checkMethod( level1, "activate_precedence_1", "activate_precedence_1_bundleContext" );
+        //protected method, arg Map
+        checkMethod( level2, "activate_precedence_1", "activate_precedence_1_map" );
+
+        //private method, arg Map
+        checkMethod( base, "activate_precedence_2", "activate_precedence_2_map" );
+        //package method, args ComponentContext and Map
+        checkMethod( level1, "activate_precedence_2", "activate_precedence_2_comp_bundleContext" );
+        //protected method, no args
+        checkMethod( level2, "activate_precedence_2", "activate_precedence_2_empty" );
+    }
 
     //---------- internal
 
@@ -221,6 +249,20 @@
      */
     private void checkMethod( BaseObject obj, String methodName )
     {
+        checkMethod( obj, methodName, methodName );
+    }
+
+    /**
+     * Checks whether a method with the given name can be found for the
+     * activate/deactivate method parameter list and whether the method returns
+     * the expected description when called.
+     *
+     * @param obj
+     * @param methodName
+     * @param methodDesc
+     */
+    private void checkMethod( BaseObject obj, String methodName, String methodDesc )
+    {
         ComponentMetadata metadata = new ComponentMetadata( 0 )
         {
             public boolean isDS11()
@@ -234,7 +276,7 @@
         Method m = get(am, "m_method");
         assertNotNull( m );
         assertEquals( methodName, m.getName() );
-        assertEquals( methodName, obj.getCalledMethod() );
+        assertEquals( methodDesc, obj.getCalledMethod() );
     }
 
 
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances/BaseObject.java b/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances/BaseObject.java
index 7c8dd0f..81b194c 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances/BaseObject.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances/BaseObject.java
@@ -19,6 +19,8 @@
 package org.apache.felix.scr.impl.metadata.instances;
 
 
+import java.util.Map;
+
 import org.osgi.framework.BundleContext;
 import org.osgi.service.component.ComponentContext;
 
@@ -68,4 +70,38 @@
     {
         setCalledMethod( "activate_suitable" );
     }
+
+    //precedence rules
+
+    private void activate_precedence_1( ComponentContext ctx )
+    {
+        setCalledMethod("activate_precedence_1_comp");
+    }
+
+    void activate_precedence_1( BundleContext bundleContext )
+    {
+        setCalledMethod("activate_precedence_1_bundleContext");
+    }
+
+    protected void activate_precedence_1( Map map)
+    {
+        setCalledMethod("activate_precedence_1_map");
+    }
+
+    private void activate_precedence_2( Map map )
+    {
+        setCalledMethod("activate_precedence_2_map");
+    }
+
+    void activate_precedence_2( ComponentContext ctx, BundleContext bundle )
+    {
+        setCalledMethod("activate_precedence_2_comp_bundleContext");
+    }
+
+    protected void activate_precedence_2()
+    {
+        setCalledMethod("activate_precedence_2_empty");
+    }
+
+
 }
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances2/Level2Object.java b/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances2/Level2Object.java
index 64a24ba..a8a63ca 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances2/Level2Object.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/metadata/instances2/Level2Object.java
@@ -52,4 +52,14 @@
     {
         setCalledMethod( "activate_suitable" );
     }
+
+    private void activate_comp_unsuitable( ComponentContext ctx )
+    {
+        setCalledMethod( "activate_comp_unsuitable" );
+    }
+
+    protected void activate_comp_unsuitable( Map map )
+    {
+        setCalledMethod( "activate_comp_unsuitable" );
+    }
 }