Improve method invocation for Lifecycle Callbacks. Allow the invocation of method from parent classes.(Felix-315).

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@551894 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java
index 2d76148..c92ae63 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallback.java
@@ -41,7 +41,7 @@
     protected static final int INVALIDATE = 0;
     
     /**
-     * Transition on hwich calling the callback.
+     * Transition on wich calling the callback.
      */
     private int m_transition;
 
@@ -61,6 +61,18 @@
         m_transition = transition;
         m_callback = new Callback(mm, hh.getInstanceManager());
     }
+    
+    /**
+     * LifecycleCallback constructor.
+     * 
+     * @param hh : the callback handler calling the callback
+     * @param transition : transition on which calling the callback
+     * @param mm : method name to invoke
+     */
+    public LifecycleCallback(LifecycleCallbackHandler hh, int transition, String mm) {
+        m_transition = transition;
+        m_callback = new Callback(mm, new String[0], false, hh.getInstanceManager());
+    }
 
     /**
      * Call the callback method when the transition from inital to final state is
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
index f602769..4add20e 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/handlers/lifecycle/callback/LifecycleCallbackHandler.java
@@ -101,10 +101,6 @@
             String methodName = hooksMetadata[i].getAttribute("method");
             
             MethodMetadata met = mm.getMethod(methodName, new String[0]);
-            if (met == null) {
-                cm.getFactory().getLogger().log(Logger.ERROR, "The method " + methodName + " is not implemented in the " + cm.getInstanceName());
-                return;
-            }
             
             int transition = -1;
             if (hooksMetadata[i].containsAttribute("transition")) {
@@ -134,7 +130,12 @@
                 return;
             }
             
-            LifecycleCallback hk = new LifecycleCallback(this, transition, met);
+            LifecycleCallback hk = null;
+            if (met != null) { 
+                hk = new LifecycleCallback(this, transition, met);
+            } else {
+                hk = new LifecycleCallback(this, transition, methodName);
+            }
             addCallback(hk);
         }
         if (m_callbacks.length > 0 || m_immediate) {
diff --git a/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Callback.java b/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Callback.java
index b3f59f5..2676dcc 100644
--- a/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Callback.java
+++ b/ipojo/core/src/main/java/org/apache/felix/ipojo/util/Callback.java
@@ -209,8 +209,31 @@
 
             }
         }
+        
+        if (m_methodObj == null) { //look at parent classes
+            methods = m_manager.getClazz().getMethods();
+            for (int i = 0; m_methodObj == null && i < methods.length; i++) {
+                // First check the method name
+                if (methods[i].getName().equals(m_method)) {
+                    // Check arguments
+                    Class[] clazzes = methods[i].getParameterTypes();
+                    if (clazzes.length == m_args.length) { // Test size to avoid useless loop
+                        boolean ok = true;
+                        for (int j = 0; ok && j < m_args.length; j++) {
+                            if (!m_args[j].equals(clazzes[j].getName())) {
+                                ok = false;
+                            }
+                        }
+                        if (ok) {
+                            m_methodObj = methods[i]; // It is the looked method.
+                        } 
+                    }
+                }
+            }
+        }
+        
         if (m_methodObj == null) {
-            m_manager.getFactory().getLogger().log(Logger.ERROR, "The method " + m_method + " is not found in the code");
+            m_manager.getFactory().getLogger().log(Logger.ERROR, "The method " + m_method + " cannot be called : method not found");
             return;
         } else {
             m_methodObj.setAccessible(true);
diff --git a/ipojo/plugin/src/main/java/org/apache/felix/ipojo/plugin/ManipulatorMojo.java b/ipojo/plugin/src/main/java/org/apache/felix/ipojo/plugin/ManipulatorMojo.java
index c9a7636..c49c454 100644
--- a/ipojo/plugin/src/main/java/org/apache/felix/ipojo/plugin/ManipulatorMojo.java
+++ b/ipojo/plugin/src/main/java/org/apache/felix/ipojo/plugin/ManipulatorMojo.java
@@ -1,3 +1,21 @@
+/* 

+ * Licensed to the Apache Software Foundation (ASF) under one

+ * or more contributor license agreements.  See the NOTICE file

+ * distributed with this work for additional information

+ * regarding copyright ownership.  The ASF licenses this file

+ * to you under the Apache License, Version 2.0 (the

+ * "License"); you may not use this file except in compliance

+ * with the License.  You may obtain a copy of the License at

+ *

+ *   http://www.apache.org/licenses/LICENSE-2.0

+ *

+ * Unless required by applicable law or agreed to in writing,

+ * software distributed under the License is distributed on an

+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

+ * KIND, either express or implied.  See the License for the

+ * specific language governing permissions and limitations

+ * under the License.

+ */

 package org.apache.felix.ipojo.plugin;

 

 import java.io.File;

@@ -8,9 +26,9 @@
 import org.apache.maven.plugin.MojoFailureException;

 

 /**

- * Package an OSGi jar "bundle."

+ * Package an OSGi jar "bundle" as an "iPOJO bundle".

  * 

- * @author <a href="mailto:felix-dev@incubator.apache.org">Apache Felix Project</a>

+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>

  * @version $Rev$, $Date$

  * @goal ipojo-bundle

  * @phase package

@@ -25,7 +43,7 @@
      * @parameter expression="${project.build.directory}"

      * @required

      */

-    private String buildDirectory;

+    private String m_buildDirectory;

 

     /**

      * The directory containing generated classes.

@@ -34,7 +52,7 @@
      * @required

      * @readonly

      */

-    private File outputDirectory;

+    private File m_outputDirectory;

 

     /**

      * The name of the generated JAR file.

@@ -42,37 +60,44 @@
      * @parameter alias="jarName" expression="${project.build.finalName}"

      * @required

      */

-    private String jarName;

+    private String m_jarName;

     

     /**

+     * Metadata file location.

      * @parameter expression="${metadata}" default-value="metadata.xml"

      */

-    private String metadata;

+    private String m_metadata;

 

+    /**

+     * Execute method : launch the pojoization.

+     * @throws MojoExecutionException : an exception occurs.

+     * @throws MojoFailureException : an failure occurs.

+     * @see org.apache.maven.plugin.AbstractMojo#execute()

+     */

     public void execute() throws MojoExecutionException, MojoFailureException {

         getLog().info("Start bundle manipulation");

         // Get metadata file

-        File meta = new File(outputDirectory + "/" + metadata);

+        File meta = new File(m_outputDirectory + "/" + m_metadata);

         getLog().info("Metadata File : " + meta.getAbsolutePath());

-        if(!meta.exists()) {

+        if (!meta.exists()) {

             throw new MojoExecutionException("the specified metadata file does not exists");

         }

 

         // Get input bundle

-        File in = new File(buildDirectory + "/" + jarName + ".jar" );

+        File in = new File(m_buildDirectory + "/" + m_jarName + ".jar");

         getLog().info("Input Bundle File : " + in.getAbsolutePath());

-        if(!in.exists()) {

+        if (!in.exists()) {

             throw new MojoExecutionException("the specified bundle file does not exists");

         }

         

-        File out = new File(buildDirectory + "/_out.jar");

+        File out = new File(m_buildDirectory + "/_out.jar");

         

         Pojoization pojo = new Pojoization();

         pojo.pojoization(in, out, meta);

-        for(int i = 0; i < pojo.getWarnings().size(); i++) {

+        for (int i = 0; i < pojo.getWarnings().size(); i++) {

             getLog().warn((String) pojo.getWarnings().get(i));

         }

-        if(pojo.getErrors().size() > 0 ) { throw new MojoExecutionException((String) pojo.getErrors().get(0)); }

+        if (pojo.getErrors().size() > 0) { throw new MojoExecutionException((String) pojo.getErrors().get(0)); }

         in.delete();

         out.renameTo(in);

         getLog().info("Bundle manipulation - SUCCESS");