Author: cziegeler
Date: Tue Nov  3 22:40:42 2009
New Revision: 832582

URL: http://svn.apache.org/viewvc?rev=832582&view=rev
Log:
FELIX-1725 :  SCR Annotations - Support "Add" Operator in annotation values. 
Applying patch from Stefan Seifert.

Modified:
    
felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java

Modified: 
felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java?rev=832582&r1=832581&r2=832582&view=diff
==============================================================================
--- 
felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java
 (original)
+++ 
felix/trunk/scrplugin/src/main/java/org/apache/felix/scrplugin/tags/annotation/defaulttag/Util.java
 Tue Nov  3 22:40:42 2009
@@ -24,12 +24,12 @@
 import org.apache.felix.scrplugin.tags.ClassUtil;
 import org.apache.felix.scrplugin.tags.JavaClassDescription;
 import org.apache.felix.scrplugin.tags.JavaField;
+import 
org.apache.felix.scrplugin.tags.annotation.AnnotationJavaClassDescription;
+import org.apache.felix.scrplugin.tags.annotation.AnnotationJavaField;
 
 import com.thoughtworks.qdox.model.Annotation;
-import com.thoughtworks.qdox.model.annotation.AnnotationConstant;
 import com.thoughtworks.qdox.model.annotation.AnnotationFieldRef;
-import com.thoughtworks.qdox.model.annotation.AnnotationValue;
-import com.thoughtworks.qdox.model.annotation.AnnotationValueList;
+import com.thoughtworks.qdox.model.annotation.EvaluatingVisitor;
 
 /**
  * Helper class for getting values from annotations.
@@ -347,70 +347,68 @@
         return getEnumValue(annotation, name, enumClass, clazz, true);
     }
 
-    private static String getAnnotationValue(final AnnotationValue av, final 
JavaClassDescription desc)
+    public static String[] getAnnotationValues(final Annotation annotation, 
final String name, final JavaClassDescription desc)
     throws IllegalArgumentException
     {
-        if ( av instanceof AnnotationFieldRef )
-        {
-            // during prescan of 
AnnotationTagProviderManager#hasScrPluginAnnotation this method is called 
without desc attribute
-            // avoid NPE in this case and just skip value resolving
-            // FELIX-1629
-            if ( desc == null)
-            {
-                return null;
-            }
 
-            // getField throws AIOOBE
-            // return 
((AnnotationFieldRef)av).getField().getInitializationExpression();
-            final String s = av.getParameterValue().toString().trim();
-            try
-            {
-                int classSep = s.lastIndexOf('.');
-                JavaField field = null;
-                if ( classSep == -1 ) {
-                    // local variable
-                    field = desc.getFieldByName(s);
-                }
-                if ( field == null ) {
-                    field = desc.getExternalFieldByName(s);
+        EvaluatingVisitor evaluatingVisitor = new EvaluatingVisitor() {
+            
+            public Object visitAnnotationFieldRef( AnnotationFieldRef fieldRef 
) {
+                // during prescan of 
AnnotationTagProviderManager#hasScrPluginAnnotation this method is called 
without desc attribute
+                // avoid NPE in this case and just skip value resolving
+                // FELIX-1629
+                if ( desc == null)
+                {
+                    return "";
                 }
-                if ( field == null ) {
-                    throw new IllegalArgumentException("Property references 
unknown field " + s + " in class " + desc.getName());
+
+                // getField throws AIOOBE
+                // return 
((AnnotationFieldRef)av).getField().getInitializationExpression();
+                final String s = 
fieldRef.getParameterValue().toString().trim();
+                try
+                {
+                    int classSep = s.lastIndexOf('.');
+                    JavaField field = null;
+                    if ( classSep == -1 ) {
+                        // local variable
+                        field = desc.getFieldByName(s);
+                    }
+                    if ( field == null ) {
+                        field = desc.getExternalFieldByName(s);
+                    }
+                    if ( field == null ) {
+                        throw new IllegalArgumentException("Property 
references unknown field " + s + " in class " + desc.getName());
+                    }
+                    String[] values = field.getInitializationExpression();
+                    if ( values != null && values.length == 1 ) {
+                        return values[0];
+                    }
+                    throw new IllegalArgumentException("Something is wrong: " 
+ s);
                 }
-                String[] values = field.getInitializationExpression();
-                if ( values != null && values.length == 1 ) {
-                    return values[0];
+                catch (SCRDescriptorException mee)
+                {
+                    throw new IllegalArgumentException(mee);
                 }
-                throw new IllegalArgumentException("Something is wrong.");
             }
-            catch (SCRDescriptorException mee)
-            {
-                throw new IllegalArgumentException(mee);
+
+            @Override
+            protected Object 
getFieldReferenceValue(com.thoughtworks.qdox.model.JavaField javaField) {
+                // is never called because visitAnnotationFieldRef is 
overridden as well
+                return null;
             }
+        
+        };
+        List<Object> valueList = evaluatingVisitor.getListValue(annotation, 
name);
+        if (valueList==null) {
+            return null;
         }
-        return ((AnnotationConstant)av).getValue().toString();
-    }
-
-    public static String[] getAnnotationValues(final Annotation annotation, 
final String name, final JavaClassDescription desc)
-    throws IllegalArgumentException
-    {
-        final AnnotationValue av = annotation.getProperty(name);
-        if ( av != null )
-        {
-            if ( av instanceof AnnotationValueList )
-            {
-                final AnnotationValueList avl = (AnnotationValueList)av;
-                @SuppressWarnings("unchecked")
-                final List<AnnotationValue> list = avl.getValueList();
-                final String[] values = new String[list.size()];
-                for(int i=0; i < values.length; i++)
-                {
-                    values[i] = getAnnotationValue(list.get(i), desc);
-                }
-                return values;
+        String[] values = new String[valueList.size()];
+        for (int i=0; i<values.length; i++) {
+            Object value = valueList.get(i);
+            if (value!=null) {
+                values[i] = value.toString();
             }
-            return new String[] {getAnnotationValue(av, desc)};
         }
-        return null;
+        return values;
     }
 }


Reply via email to