FELIX-2753 : Support array of classes for @Service annotation
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1073402 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/annotations/pom.xml b/scrplugin/annotations/pom.xml
index 7ba16b7..9b9fbe4 100644
--- a/scrplugin/annotations/pom.xml
+++ b/scrplugin/annotations/pom.xml
@@ -47,7 +47,7 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.generator</artifactId>
- <version>0.0.1-SNAPSHOT</version>
+ <version>1.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
diff --git a/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Service.java b/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Service.java
index 3651a1f..29343f0 100644
--- a/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Service.java
+++ b/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Service.java
@@ -18,11 +18,7 @@
*/
package org.apache.felix.scr.annotations;
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
+import java.lang.annotation.*;
/**
* The <code>Service</code> annotation defines whether and which service
@@ -43,7 +39,7 @@
* property is not set provide elements will be generated for all interfaces
* generated by the class
*/
- Class<?> value() default AutoDetect.class;
+ Class<?>[] value() default AutoDetect.class;
/**
* Whether the component is registered as a
diff --git a/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java b/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java
index 7e4600a..eab1ee0 100644
--- a/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java
+++ b/scrplugin/annotations/src/main/java/org/apache/felix/scr/annotations/Services.java
@@ -18,18 +18,17 @@
*/
package org.apache.felix.scr.annotations;
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
+import java.lang.annotation.*;
/**
* Allows to define multiple {@link Service} annotations for one type.
+ *
+ * @deprecated It's now possible to use multiple classes in the {@link Service} annotation.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@Documented
+@Deprecated
public @interface Services {
/**
diff --git a/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java b/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java
index 942e7fa..0bd7ad1 100644
--- a/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java
+++ b/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java
@@ -21,13 +21,7 @@
import java.util.ArrayList;
import java.util.List;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Properties;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.References;
-import org.apache.felix.scr.annotations.Service;
-import org.apache.felix.scr.annotations.Services;
+import org.apache.felix.scr.annotations.*;
import org.apache.felix.scrplugin.tags.JavaField;
import org.apache.felix.scrplugin.tags.JavaTag;
import org.apache.felix.scrplugin.tags.annotation.AnnotationJavaClassDescription;
@@ -53,7 +47,7 @@
} else if (annotation.getType().getJavaClass().getFullyQualifiedName().equals(Property.class.getName())) {
tags.add(new PropertyTag(annotation, description, field));
} else if (annotation.getType().getJavaClass().getFullyQualifiedName().equals(Service.class.getName())) {
- tags.add(new ServiceTag(annotation, description));
+ tags.addAll(ServiceTag.createServiceTags(annotation, description));
} else if (annotation.getType().getJavaClass().getFullyQualifiedName().equals(Reference.class.getName())) {
tags.add(new ReferenceTag(annotation, description, field));
}
@@ -69,7 +63,7 @@
@SuppressWarnings("unchecked")
final List<Annotation> services = (List<Annotation>)annotation.getNamedParameter("value");
for (Annotation service : services) {
- tags.add(new ServiceTag(service, description));
+ tags.addAll(ServiceTag.createServiceTags(service, description));
}
} else if (annotation.getType().getJavaClass().getFullyQualifiedName().equals(References.class.getName())) {
@SuppressWarnings("unchecked")
diff --git a/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ServiceTag.java b/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ServiceTag.java
index dc5c863..aa1c8a4 100644
--- a/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ServiceTag.java
+++ b/scrplugin/annotations/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ServiceTag.java
@@ -18,8 +18,7 @@
*/
package org.apache.felix.scrplugin.tags.annotation.defaulttag;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
import org.apache.felix.scr.annotations.AutoDetect;
import org.apache.felix.scr.annotations.Service;
@@ -37,26 +36,19 @@
protected final Service annotation;
+ protected final String serviceInterface;
+
/**
* @param annotation Annotation
* @param desc Description
*/
- public ServiceTag(final Annotation annotation, JavaClassDescription desc) {
+ public ServiceTag(final Annotation annotation,
+ final JavaClassDescription desc,
+ final Service tag,
+ final String serviceInterface) {
super(annotation, desc, null);
- this.annotation = new Service() {
-
- public boolean serviceFactory() {
- return Util.getBooleanValue(annotation, "serviceFactory", Service.class);
- }
-
- public Class<?> value() {
- return Util.getClassValue(annotation, "value", Service.class);
- }
-
- public Class<? extends java.lang.annotation.Annotation> annotationType() {
- return null;
- }
- };
+ this.annotation = tag;
+ this.serviceInterface = serviceInterface;
}
@Override
@@ -68,15 +60,37 @@
public Map<String, String> createNamedParameterMap() {
final Map<String, String> map = new HashMap<String, String>();
- String serviceInterface = null;
- if (this.annotation.value() != AutoDetect.class) {
- serviceInterface = this.annotation.value().getName();
- }
- map.put(Constants.SERVICE_INTERFACE, serviceInterface);
-
+ map.put(Constants.SERVICE_INTERFACE, this.serviceInterface);
map.put(Constants.SERVICE_FACTORY, String.valueOf(this.annotation.serviceFactory()));
return map;
}
+ public static List<ServiceTag> createServiceTags(final Annotation annotation, JavaClassDescription desc) {
+ final Service tag = new Service() {
+
+ public boolean serviceFactory() {
+ return Util.getBooleanValue(annotation, "serviceFactory", Service.class);
+ }
+
+ public Class<?>[] value() {
+ return Util.getClassArrayValue(annotation, "value", Service.class);
+ }
+
+ public Class<? extends java.lang.annotation.Annotation> annotationType() {
+ return null;
+ }
+ };
+ final Class<?>[] classes = tag.value();
+ System.out.println("Classes: " + Arrays.toString(classes));
+ if ( classes != null && classes.length > 0
+ && (classes.length != 1 || !classes[0].getName().equals(AutoDetect.class.getName()))) {
+ final List<ServiceTag> tags = new ArrayList<ServiceTag>();
+ for(final Class<?> c : classes ) {
+ tags.add(new ServiceTag(annotation, desc, tag, c.getName()));
+ }
+ return tags;
+ }
+ return Collections.singletonList(new ServiceTag(annotation, desc, tag, null));
+ }
}
diff --git a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/tags/annotation/Util.java b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/tags/annotation/Util.java
index 7d48c4a..ea44c02 100644
--- a/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/tags/annotation/Util.java
+++ b/scrplugin/generator/src/main/java/org/apache/felix/scrplugin/tags/annotation/Util.java
@@ -18,6 +18,7 @@
*/
package org.apache.felix.scrplugin.tags.annotation;
+import java.util.Collection;
import java.util.List;
import org.apache.felix.scrplugin.SCRDescriptorException;
@@ -309,11 +310,14 @@
}
public static Class<?>[] getClassArrayValue(Annotation annotation, String name, final Class<?> clazz) {
- final Object obj = annotation.getNamedParameter(name);
+ Object obj = annotation.getNamedParameter(name);
if ( obj != null ) {
if ( obj instanceof Class<?> ) {
return new Class<?>[] {(Class<?>)obj};
}
+ if ( obj instanceof Collection<?> ) {
+ obj = ((Collection<?>)obj).toArray();
+ }
if ( obj.getClass().isArray() ) {
final Object[] objArray = (Object[])obj;
final Class<?>[] result = new Class<?>[objArray.length];