RESOLVED - issue FELIX-446: Search of activation/deactivation method might get wrong result
https://issues.apache.org/jira/browse/FELIX-446
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@608505 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java
index aab61c8..65bae9b 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/om/Component.java
@@ -18,14 +18,9 @@
*/
package org.apache.felix.scrplugin.om;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
-import org.apache.felix.scrplugin.tags.JavaClassDescription;
-import org.apache.felix.scrplugin.tags.JavaMethod;
-import org.apache.felix.scrplugin.tags.JavaParameter;
-import org.apache.felix.scrplugin.tags.JavaTag;
+import org.apache.felix.scrplugin.tags.*;
import org.apache.maven.plugin.MojoExecutionException;
/**
@@ -211,8 +206,8 @@
// no errors so far, let's continue
if ( issues.size() == 0 ) {
// check activate and deactivate methods
- this.checkActivationMethod(javaClass, "activate", warnings);
- this.checkActivationMethod(javaClass, "deactivate", warnings);
+ this.checkLifecycleMethod(javaClass, "activate", warnings);
+ this.checkLifecycleMethod(javaClass, "deactivate", warnings);
// ensure public default constructor
boolean constructorFound = true;
@@ -266,50 +261,34 @@
}
/**
- * Check methods.
- * @param javaClass
- * @param methodName
+ * Check for existence of lifecycle methods.
+ * @param javaClass The java class to inspect.
+ * @param methodName The method name.
+ * @param warnings The list of warnings used to add new warnings.
*/
- protected void checkActivationMethod(JavaClassDescription javaClass, String methodName, List warnings) {
- JavaMethod[] methods = javaClass.getMethods();
- JavaMethod activation = null;
- for (int i=0; i < methods.length; i++) {
- // ignore method not matching the name
- if (!methodName.equals(methods[i].getName())) {
- continue;
+ protected void checkLifecycleMethod(JavaClassDescription javaClass, String methodName, List warnings)
+ throws MojoExecutionException {
+ final JavaMethod method = javaClass.getMethodBySignature(methodName, new String[] {"org.osgi.service.component.ComponentContext"});
+ if ( method != null ) {
+ // check protected
+ if (method.isPublic()) {
+ warnings.add(this.getMessage("Lifecycle method " + method.getName() + " should be declared protected"));
+ } else if (!method.isProtected()) {
+ warnings.add(this.getMessage("Lifecycle method " + method.getName() + " has wrong qualifier, public or protected required"));
}
+ } else {
+ // if no method is found, we check for any method with that name
+ final JavaMethod[] methods = javaClass.getMethods();
+ for(int i=0; i<methods.length; i++) {
+ if ( methodName.equals(methods[i].getName()) ) {
- // if the method has the correct parameter type, check protected
- JavaParameter[] params = methods[i].getParameters();
- if (params == null || params.length != 1) {
- continue;
+ if ( methods[i].getParameters().length != 1 ) {
+ warnings.add(this.getMessage("Lifecycle method " + methods[i].getName() + " has wrong number of arguments"));
+ } else {
+ warnings.add(this.getMessage("Lifecycle method " + methods[i].getName() + " has wrong argument " + methods[i].getParameters()[0].getType()));
+ }
+ }
}
-
- // this might be considered, if it is an overload, drop out of check
- if (activation != null) {
- return;
- }
-
- // consider this method for further checks
- activation = methods[i];
- }
-
- // no activation method found
- if (activation == null) {
- return;
- }
-
- // check protected
- if (activation.isPublic()) {
- warnings.add(this.getMessage("Activation method " + activation.getName() + " should be declared protected"));
- } else if (!activation.isProtected()) {
- warnings.add(this.getMessage("Activation method " + activation.getName() + " has wrong qualifier, public or protected required"));
- }
-
- // check paramter (we know there is exactly one)
- JavaParameter param = activation.getParameters()[0];
- if (!"org.osgi.service.component.ComponentContext".equals(param.getType())) {
- warnings.add(this.getMessage("Activation method " + methodName + " has wrong argument type " + param.getType()));
}
}
}
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaClassDescription.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaClassDescription.java
index bd89142..cbab33a 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaClassDescription.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaClassDescription.java
@@ -98,6 +98,10 @@
*/
boolean isInterface();
+ /**
+ * Return all methods of this class
+ * @return An array of methods or an empty array.
+ */
JavaMethod[] getMethods();
/**
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaMethod.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaMethod.java
index f21770f..cf36076 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaMethod.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/JavaMethod.java
@@ -24,6 +24,8 @@
*/
public interface JavaMethod {
+ JavaMethod[] EMPTY_RESULT = new JavaMethod[0];
+
boolean isPublic();
boolean isProtected();
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java
index 7981ce4..158e8a3 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/cl/ClassLoaderJavaClassDescription.java
@@ -111,8 +111,7 @@
* @see org.apache.felix.scrplugin.tags.JavaClassDescription#getMethods()
*/
public JavaMethod[] getMethods() {
- // TODO Auto-generated method stub
- return null;
+ return JavaMethod.EMPTY_RESULT;
}
/**
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java
index 9afe8ab..bab9c77 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/qdox/QDoxJavaClassDescription.java
@@ -167,7 +167,7 @@
public JavaMethod[] getMethods() {
final com.thoughtworks.qdox.model.JavaMethod[] methods = this.javaClass.getMethods();
if ( methods == null || methods.length == 0) {
- return new JavaMethod[0];
+ return JavaMethod.EMPTY_RESULT;
}
final JavaMethod[] m = new JavaMethod[methods.length];
for(int i=0;i<methods.length;i++) {