geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r770440 - in /geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint: context/Instanciator.java context/Parser.java reflect/BeanMetadataImpl.java reflect/MetadataUtil.java
Date Thu, 30 Apr 2009 20:48:20 GMT
Author: gawor
Date: Thu Apr 30 20:48:19 2009
New Revision: 770440

URL: http://svn.apache.org/viewvc?rev=770440&view=rev
Log:
perform better argument index checking

Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/BeanMetadataImpl.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/MetadataUtil.java

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java?rev=770440&r1=770439&r2=770440&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Instanciator.java
Thu Apr 30 20:48:19 2009
@@ -22,7 +22,6 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -216,7 +215,7 @@
             boolean hasIndex = (beanArguments.get(0).getIndex() >= 0);
             if (hasIndex) {
                 List<BeanArgument> beanArgumentsCopy = new ArrayList<BeanArgument>(beanArguments);
-                Collections.sort(beanArgumentsCopy, new BeanArgumentComparator());
+                Collections.sort(beanArgumentsCopy, MetadataUtil.BEAN_COMPARATOR);
                 beanArguments = beanArgumentsCopy;
             }
             List<Object> arguments = new ArrayList<Object>();
@@ -347,11 +346,5 @@
         }
         return clazz;
     }
-
-    private static class BeanArgumentComparator implements Comparator<BeanArgument>
{
-        public int compare(BeanArgument object1, BeanArgument object2) {
-            return object1.getIndex() - object2.getIndex();
-        }        
-    }
-                
+                  
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java?rev=770440&r1=770439&r2=770440&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/Parser.java
Thu Apr 30 20:48:19 2009
@@ -51,6 +51,7 @@
 import org.apache.geronimo.blueprint.ExtendedComponentDefinitionRegistry;
 import org.apache.geronimo.blueprint.namespace.ParserContextImpl;
 import org.apache.geronimo.blueprint.reflect.ListenerImpl;
+import org.apache.geronimo.blueprint.reflect.MetadataUtil;
 import org.apache.geronimo.blueprint.reflect.RefCollectionMetadataImpl;
 import org.apache.geronimo.blueprint.reflect.CollectionMetadataImpl;
 import org.apache.geronimo.blueprint.reflect.BeanMetadataImpl;
@@ -509,6 +510,8 @@
             }
         }
 
+        MetadataUtil.validateBeanArguments(metadata.getArguments());
+        
         ComponentMetadata m = metadata;
 
         // Parse custom attributes

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/BeanMetadataImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/BeanMetadataImpl.java?rev=770440&r1=770439&r2=770440&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/BeanMetadataImpl.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/BeanMetadataImpl.java
Thu Apr 30 20:48:19 2009
@@ -111,14 +111,6 @@
         if (this.arguments == null) {
             this.arguments = new ArrayList<BeanArgument>();
         }
-        if (!this.arguments.isEmpty()) {
-            BeanArgument p1 = this.arguments.get(0);
-            BeanArgument p2 = argument;
-            if ( (p1.getIndex() > -1 && p2.getIndex() < 0) ||
-                 (p1.getIndex() < 0 && p2.getIndex() > -1) ) {
-                throw new IllegalArgumentException("Index attribute must be specified either
on all or none constructor arguments");
-            }
-        }
         this.arguments.add(argument);
     }
 
@@ -229,7 +221,7 @@
             this.explicitDependencies.remove(dependency);
         }
     }
-
+    
     @Override
     public String toString() {
         return "BeanMetadata[" +

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/MetadataUtil.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/MetadataUtil.java?rev=770440&r1=770439&r2=770440&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/MetadataUtil.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/reflect/MetadataUtil.java
Thu Apr 30 20:48:19 2009
@@ -17,6 +17,12 @@
 
 package org.apache.geronimo.blueprint.reflect;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.osgi.service.blueprint.reflect.BeanArgument;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
 import org.osgi.service.blueprint.reflect.IdRefMetadata;
@@ -42,6 +48,8 @@
  */
 public class MetadataUtil {
 
+    public static final Comparator<BeanArgument> BEAN_COMPARATOR = new BeanArgumentComparator();
+    
     static public Metadata cloneMetadata(Metadata source) {
         if (source == null) {
             return null;
@@ -142,5 +150,45 @@
             throw new IllegalArgumentException("Unsupport metadata type: " + (type != null
? type.getName() : null));
         }
     }
+    
+    public static List<BeanArgument> validateBeanArguments(List<BeanArgument>
arguments) {
+        if (arguments == null || arguments.isEmpty()) {
+            return arguments;
+        }
+        // check if all or none arguments have index attribute
+        boolean hasIndexFirst = (arguments.get(0).getIndex() > -1);
+        for (int i = 1; i < arguments.size(); i++) {
+            boolean hasIndex = (arguments.get(i).getIndex() > -1);
+            if ( (hasIndexFirst && !hasIndex) ||
+                 (!hasIndexFirst && hasIndex) ) {
+                throw new IllegalArgumentException("Index attribute must be specified either
on all or none constructor arguments");
+            }
+        }
+        if (hasIndexFirst) {
+            // sort the arguments
+            List<BeanArgument> argumentsCopy = new ArrayList<BeanArgument>(arguments);
+            Collections.sort(argumentsCopy, MetadataUtil.BEAN_COMPARATOR);
+            arguments = argumentsCopy;
+            
+            // check if the indexes are sequential
+            for (int i = 0; i < arguments.size(); i++) {
+                int index = arguments.get(i).getIndex();
+                if (index > i) {
+                    throw new IllegalArgumentException("Missing attribute index");      
             
+                } else if (index < i) {
+                    throw new IllegalArgumentException("Duplicate attribute index");
+                } // must be the same
+            }            
+        }
+        
+        return arguments;
+    }
+    
+    private static class BeanArgumentComparator implements Comparator<BeanArgument>
{
+        public int compare(BeanArgument object1, BeanArgument object2) {
+            return object1.getIndex() - object2.getIndex();
+        }        
+    }
+    
 }
 



Mime
View raw message