FELIX-1010 : Fix several issues with the annotations
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@767473 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java
index df5fc0f..3db5348 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/DefaultAnnotationTagProvider.java
@@ -89,17 +89,17 @@
// check for multi-annotations
else if (annotation.getType().getJavaClass().getFullyQualifiedName().equals(Properties.class.getName())) {
- final com.thoughtworks.qdox.model.Annotation[] properties = (com.thoughtworks.qdox.model.Annotation[])annotation.getNamedParameter("value");
+ final List<com.thoughtworks.qdox.model.Annotation> properties = (List<com.thoughtworks.qdox.model.Annotation>)annotation.getNamedParameter("value");
for (com.thoughtworks.qdox.model.Annotation property : properties) {
tags.add(new PropertyTag(property, description));
}
} else if (annotation.getType().getJavaClass().getFullyQualifiedName().equals(Services.class.getName())) {
- final com.thoughtworks.qdox.model.Annotation[] services = (com.thoughtworks.qdox.model.Annotation[])annotation.getNamedParameter("value");
+ final List<com.thoughtworks.qdox.model.Annotation> services = (List<com.thoughtworks.qdox.model.Annotation>)annotation.getNamedParameter("value");
for (com.thoughtworks.qdox.model.Annotation service : services) {
tags.add(new ServiceTag(service, description));
}
} else if (annotation.getType().getJavaClass().getFullyQualifiedName().equals(References.class.getName())) {
- final com.thoughtworks.qdox.model.Annotation[] references = (com.thoughtworks.qdox.model.Annotation[])annotation.getNamedParameter("value");
+ final List<com.thoughtworks.qdox.model.Annotation> references = (List<com.thoughtworks.qdox.model.Annotation>)annotation.getNamedParameter("value");
for (com.thoughtworks.qdox.model.Annotation reference : references) {
tags.add(new ReferenceTag(reference, description, field));
}
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/PropertyTag.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/PropertyTag.java
index 3888ab7..2262e6c 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/PropertyTag.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/PropertyTag.java
@@ -88,7 +88,14 @@
}
public String[] value() {
- return Util.getStringValues(annotation, "value", Property.class);
+ // value property can be used as String[] or String property
+ Object obj = annotation.getNamedParameter("value");
+ if (obj instanceof List) {
+ return Util.getStringValues(annotation, "value", Property.class);
+ }
+ else {
+ return new String[] { Util.getStringValue(annotation, "value", Property.class) };
+ }
}
public Class<? extends java.lang.annotation.Annotation> annotationType() {
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ReferenceTag.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ReferenceTag.java
index a5509e0..219eeb7 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ReferenceTag.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/ReferenceTag.java
@@ -59,19 +59,7 @@
}
public ReferenceCardinality cardinality() {
- final Object obj = annotation.getNamedParameter("cardinality");
- if ( obj != null ) {
- if ( obj instanceof ReferenceCardinality ) {
- return (ReferenceCardinality)obj;
- }
- return ReferenceCardinality.values()[(Integer)obj];
- }
- try {
- return (ReferenceCardinality) Reference.class.getMethod("cardinality").getDefaultValue();
- } catch( NoSuchMethodException mnfe) {
- // we ignore this
- return null;
- }
+ return Util.getEnumValue(annotation, "cardinality", ReferenceCardinality.class, Reference.class);
}
public boolean checked() {
@@ -83,19 +71,7 @@
}
public ReferencePolicy policy() {
- final Object obj = annotation.getNamedParameter("policy");
- if ( obj != null ) {
- if ( obj instanceof ReferencePolicy ) {
- return (ReferencePolicy)obj;
- }
- return ReferencePolicy.values()[(Integer)obj];
- }
- try {
- return (ReferencePolicy) Reference.class.getMethod("policy").getDefaultValue();
- } catch( NoSuchMethodException mnfe) {
- // we ignore this
- return null;
- }
+ return Util.getEnumValue(annotation, "policy", ReferencePolicy.class, Reference.class);
}
public Class<?> referenceInterface() {
diff --git a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java
index 87dbffd..d27980c 100644
--- a/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java
+++ b/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java
@@ -18,6 +18,9 @@
*/
package org.apache.felix.scrplugin.tags.annotation.defaulttag;
+import java.util.EnumSet;
+import java.util.List;
+
import org.apache.felix.scrplugin.tags.ClassUtil;
import com.thoughtworks.qdox.model.Annotation;
@@ -63,7 +66,12 @@
public static String[] getStringValues(Annotation annotation, String name, final Class<?> clazz) {
final Object obj = annotation.getNamedParameter(name);
if ( obj != null ) {
- return (String[])obj;
+ List<String> list = (List<String>)obj;
+ String[] values = new String[list.size()];
+ for (int i=0; i<values.length; i++) {
+ values[i] = stripQuotes(list.get(i));
+ }
+ return values;
}
try {
return (String[]) clazz.getMethod(name).getDefaultValue();
@@ -77,11 +85,7 @@
final Object obj = annotation.getNamedParameter(name);
if ( obj != null ) {
if ( obj instanceof String ) {
- final String s = (String)obj;
- if ( s.startsWith("\"") && s.endsWith("\"")) {
- return s.substring(1, s.length() - 1);
- }
- return s;
+ return stripQuotes((String)obj);
}
return obj.toString();
}
@@ -92,6 +96,18 @@
return "";
}
}
+
+ /**
+ * QDox annotations seemt to return annotation values always with quotes - remove them
+ * @param s String with our without quotes
+ * @return String without quotes
+ */
+ private static String stripQuotes(String s) {
+ if (s.startsWith("\"") && s.endsWith("\"")) {
+ return s.substring(1, s.length() - 1);
+ }
+ return s;
+ }
public static Class<?> getClassValue(Annotation annotation, String name, final Class<?> clazz) {
final Object obj = annotation.getNamedParameter(name);
@@ -108,4 +124,30 @@
return null;
}
}
+
+ public static <T extends Enum> T getEnumValue(Annotation annotation, String name, final Class<T> enumClass, final Class<?> clazz) {
+ Object obj = annotation.getNamedParameter(name);
+ if (obj == null) {
+ try {
+ obj = clazz.getMethod(name).getDefaultValue();
+ } catch( NoSuchMethodException mnfe) {
+ // we ignore this
+ }
+ }
+ if ( obj != null ) {
+ if (enumClass.isAssignableFrom(obj.getClass())) {
+ return (T)obj;
+ }
+ else if ( obj instanceof String ) {
+ String enumName = (String)obj;
+ int dotPos = enumName.lastIndexOf('.');
+ if (dotPos >= 0) {
+ enumName = enumName.substring(dotPos+1);
+ }
+ return Enum.valueOf(enumClass, enumName);
+ }
+ }
+ return null;
+ }
+
}