bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carlosv...@apache.org
Subject svn commit: r955986 - in /incubator/bval/sandbox/jsr303-impl-bench/src: main/java/org/apache/bval/bench/ main/java/org/apache/bval/bench/jsr303/ main/java/org/apache/bval/bench/metatree/ main/resources/ test/java/org/apache/bval/bench/
Date Fri, 18 Jun 2010 12:48:49 GMT
Author: carlosvara
Date: Fri Jun 18 12:48:48 2010
New Revision: 955986

URL: http://svn.apache.org/viewvc?rev=955986&view=rev
Log:
Generator adds random groups and the corresponding GroupSequence annotation

Modified:
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Util.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/MetaGroup.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractBaseMetaClass.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaField.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaInterface.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanBasicField.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanRefField.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaAnnotation.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
    incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
    incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/BasicType.java
Fri Jun 18 12:48:48 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.bval.bench;
 
+import java.util.Arrays;
+
 
 /**
  * Basic type property types.
@@ -49,8 +51,7 @@ public enum BasicType {
     public abstract Class<?> getTypeClass();
     
     public static BasicType getRandom() {
-        BasicType[] values = BasicType.values();
-        return values[Config.CFG.getRandomGen().nextInt(values.length)];
+        return Config.CFG.getRandom(Arrays.asList(BasicType.values()));
     }
     
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Config.java
Fri Jun 18 12:48:48 2010
@@ -20,6 +20,7 @@ package org.apache.bval.bench;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 import java.util.Random;
 
@@ -99,6 +100,15 @@ public enum Config {
         return new Integer(p.getProperty("generator.numgroups"));
     }
 
+    /**
+     * @param <T> The type of elements of the list.
+     * @param elements List of possible choices.
+     * @return A random element from the list of elements.
+     */
+    public <T> T getRandom(List<T> elements) {
+        return elements.get(r.nextInt(elements.size()));
+    }
+    
     // TODO: remove this method
     public Random getRandomGen() {
         return r;
@@ -126,7 +136,7 @@ public enum Config {
     /**
      * @param level
      *            The level of nesting.
-     * @return <code>true</code> if it's determined that a nested beanshould
be
+     * @return <code>true</code> if it's determined that a nested bean should
be
      *         added, <code>false</code> otherwise.
      */
     public boolean shouldAddNestedBean(int level) {
@@ -141,6 +151,15 @@ public enum Config {
         }
     }
 
+    /**
+     * @return <code>true</code> if it's determined that the fields in the bean
+     *         being annotated should have group parameters, <code>false</code>
+     *         otherwise.
+     */
+    public boolean shouldAddGroupsToBeanFields() {
+        return r.nextDouble() < new Double(p.getProperty("generator.beanswithgroups"));
+    }
+
 
     /**
      * @return A properties object with the default configuration values

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Generator.java
Fri Jun 18 12:48:48 2010
@@ -23,7 +23,6 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.bval.bench.jsr303.Jsr303Annotator;
-import org.apache.bval.bench.jsr303.MetaGroup;
 import org.apache.bval.bench.metatree.AbstractMetaField;
 import org.apache.bval.bench.metatree.JavaBeanBasicField;
 import org.apache.bval.bench.metatree.JavaBeanRefField;

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Util.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Util.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Util.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/Util.java
Fri Jun 18 12:48:48 2010
@@ -18,9 +18,11 @@
  */
 package org.apache.bval.bench;
 
+import com.sun.codemodel.JAnnotationArrayMember;
 import com.sun.codemodel.JAnnotationUse;
 import com.sun.codemodel.JExpr;
 import com.sun.codemodel.JExpression;
+import com.sun.codemodel.JType;
 
 
 /**
@@ -66,11 +68,10 @@ public final class Util {
      *            The parameter's value.
      */
     public static void addAnnotParam(JAnnotationUse annot, String key, Object value) {
-        // TODO: Remove comparisons with primitive type class?
         if (value == null) {
             // Null values not accepted
             throw new RuntimeException("Null values not supported as annotation parameters");
-        } else if (value instanceof Boolean || value.getClass() == boolean.class) {
+        } else if (value instanceof Boolean) {
             annot.param(key, (Boolean) value);
         } else if (value instanceof Integer) {
             annot.param(key, (Integer) value);
@@ -78,9 +79,26 @@ public final class Util {
             annot.param(key, (String) value);
         } else if (value instanceof Class<?>) {
             annot.param(key, (Class<?>) value);
+        } else if (value instanceof JType) {
+            annot.param(key, (JType) value);
+        } else if (value.getClass().isArray()) {
+            Object[] valueArr = (Object[]) value;
+            JAnnotationArrayMember aux = annot.paramArray(key);
+            for (int i = 0; i < valueArr.length; ++i) {
+                addAnnotArrayParam(aux, valueArr[i]);
+            }
         } else {
             throw new RuntimeException("Impossible to construct annotation param for: " +
value);
         }
     }
 
+    private static void addAnnotArrayParam(JAnnotationArrayMember aux, Object value) {
+        if (value instanceof JType) {
+            aux.param((JType) value);
+            // return aux.param((JType)value); ?? No Javadoc defining how to use the API
+        } else {
+            throw new RuntimeException("Impossible to construct annotation param array for:
" + value);
+        }
+    }
+
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/Jsr303Annotator.java
Fri Jun 18 12:48:48 2010
@@ -26,7 +26,6 @@ import java.lang.annotation.Target;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import javax.validation.Constraint;
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;
@@ -36,7 +35,6 @@ import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Null;
-import javax.validation.constraints.Pattern;
 import javax.validation.constraints.Size;
 import org.apache.bval.bench.AnnotationType;
 import org.apache.bval.bench.AnnotatorVisitor;
@@ -58,98 +56,120 @@ import com.sun.codemodel.JClassAlreadyEx
 import com.sun.codemodel.JCodeModel;
 import com.sun.codemodel.JDefinedClass;
 import com.sun.codemodel.JExpr;
-import com.sun.codemodel.JExpressionImpl;
-import com.sun.codemodel.JFormatter;
 import com.sun.codemodel.JMethod;
 import com.sun.codemodel.JMod;
-import com.sun.codemodel.JVar;
 
 
 /**
  * A class / field annotator that produces JSR-303 annotations.
+ * <p>
+ * NOTE: Class is not thread-safe.
  * 
  * @author Carlos Vara
  */
 public class Jsr303Annotator implements AnnotatorVisitor {
-
-    // TODO: Add groups to the annotations
-    // TODO: So the beans know how many groups are used, and they can create a
-    // good @GroupSequence
-
+    
+    // The code generator
     private final JCodeModel codeModel;
 
+    // All the JSR-303 groups used by the annotator
     private final List<MetaGroup> groups;
+    
+    // Available annotations for basic types
     private final ListMultimap<BasicType, Jsr303MetaAnnotationSet> basicTypeAnnotations;
+    
+    // Available annotations for bean references
+    private final List<MetaAnnotation> refFieldAnnotations;
+    
+    // Available annotations for classes
     private final List<MetaAnnotation> classAnnotations;
+    
+    
+    // State variables (they make this class thread-unsafe)
+    private boolean addGroups = false;
 
 
-
-    // Receive a codemodel, and use it to:
-    // - Create the groups
-    // - Create the class constraints
-    // - Create the complex nested constraints
-
     public Jsr303Annotator(JCodeModel cm) {
         this.codeModel = cm;
         this.groups = generateGroups();
         this.basicTypeAnnotations = buildBasicTypeAnnotations();
+        this.refFieldAnnotations = buildRefFieldAnnotations();
         this.classAnnotations = buildClassAnnotations();
     }
 
 
     // Specific annotation methods ---------------------------------------------
+    
+    @Override
+    public void annotate(MetaJavaBean mjb) {
+        
+        // If the bean should have groups
+        addGroups = Config.CFG.shouldAddGroupsToBeanFields();
+        
+        // Annotate the fields
+        for (AbstractMetaField amf : mjb.getFields()) {
+            amf.accept(this);
+        }
 
+        // Annotate the java bean
+        mjb.addAnnotation(classAnnotations.get(0));
+        
+        // Build and add the GroupSequence annotation if required
+        MetaAnnotation gsAnnot = mjb.buildGroupSequenceAnnot();
+        if ( gsAnnot != null ) {
+            mjb.addAnnotation(gsAnnot);
+        }
+        
+    }
+    
     @Override
     public void annotate(JavaBeanRefField jbrf) {
         // Always annotate with @Valid
         MetaAnnotation valid = new MetaAnnotation(codeModel, Valid.class, AnnotationType.JSR_303,
new HashMap<String, Object>());
         jbrf.addAnnotation(valid);
 
-        if (Config.CFG.getRandomGen().nextBoolean()) { // Annotate with @NotNull
-                                                       // 50% of the time
-            Map<String, Object> notNullParams = Maps.newHashMap();
-            // TODO: Group for null
-            MetaAnnotation notNull = new MetaAnnotation(codeModel, NotNull.class, AnnotationType.JSR_303,
notNullParams);
-            jbrf.addAnnotation(notNull);
-        } else { // The other 50%, annotate with @Null
-            Map<String, Object> nullParams = Maps.newHashMap();
-            MetaAnnotation mustBeNull = new MetaAnnotation(codeModel, Null.class, AnnotationType.JSR_303,
nullParams);
-            jbrf.addAnnotation(mustBeNull);
+        // And a random annotation
+        MetaAnnotation annotCopy = new MetaAnnotation(Config.CFG.getRandom(refFieldAnnotations));
+        if ( addGroups ) {
+            MetaGroup group = Config.CFG.getRandom(groups);
+            jbrf.getOwner().addGroup(group);
+            annotCopy.setGroup(group);
         }
-
+        
+        jbrf.addAnnotation(annotCopy);
     }
 
     @Override
     public void annotate(JavaBeanBasicField jbbf) {
+        
         // Select a random set of annotations for the field's type
         BasicType type = jbbf.getBasicType();
-        int rndIdx = Config.CFG.getRandomGen().nextInt(basicTypeAnnotations.get(type).size());
-        Jsr303MetaAnnotationSet mas = basicTypeAnnotations.get(type).get(rndIdx);
+        Jsr303MetaAnnotationSet mas = Config.CFG.getRandom(basicTypeAnnotations.get(type));
 
         // Annotate the field
-        // TODO: Groups
         for (MetaAnnotation ma : mas.getAnnotations()) {
-            jbbf.addAnnotation(ma);
+            MetaAnnotation maCopy = new MetaAnnotation(ma);
+            
+            if ( addGroups ) {
+                MetaGroup group = Config.CFG.getRandom(groups);
+                jbbf.getOwner().addGroup(group);
+                maCopy.setGroup(group);
+            }
+            
+            jbbf.addAnnotation(maCopy);
         }
 
         jbbf.setValidValues(mas.getValidValues());
         jbbf.setInvalidValues(mas.getInvalidValues());
     }
 
-    @Override
-    public void annotate(MetaJavaBean mjb) {
-        // Annotate the fields
-        for (AbstractMetaField amf : mjb.getFields()) {
-            amf.accept(this);
-        }
-
-        // Annotate the java bean
-        mjb.addAnnotation(classAnnotations.get(0));
-    }
-
-
+    
     // Init methods ------------------------------------------------------------
 
+    /**
+     * @return The list of groups that the annotator will use when assigning
+     *         constraints.
+     */
     private List<MetaGroup> generateGroups() {
         List<MetaGroup> groups = Lists.newArrayList();
         for (int i = 0; i < Config.CFG.getNumGroups(); ++i) {
@@ -159,6 +179,10 @@ public class Jsr303Annotator implements 
         return groups;
     }
 
+    /**
+     * @return A map of lists of annotation constraints available for every
+     *         basic type.
+     */
     private ListMultimap<BasicType, Jsr303MetaAnnotationSet> buildBasicTypeAnnotations()
{
 
         ListMultimap<BasicType, Jsr303MetaAnnotationSet> anns = ArrayListMultimap.create();
@@ -185,6 +209,11 @@ public class Jsr303Annotator implements 
         annotParams.put("max", 12);
         annotationsSet.add(new MetaAnnotation(codeModel, Size.class, AnnotationType.JSR_303,
annotParams));
         anns.put(BasicType.STRING, new Jsr303MetaAnnotationSet(annotationsSet, Lists.newArrayList((Object)
"goodgood"), Lists.newArrayList((Object) null, "bad")));
+        
+        // TODO: A nested constraint
+        // @GoodCode
+        //  -> @Pattern(regexp=".*")
+        //  -> @Size.List( {@Size(min=5), @Size(max=8)} )
 
 
         // ### INTEGER ---------------------------------------------------------
@@ -210,78 +239,87 @@ public class Jsr303Annotator implements 
 
         return anns;
     }
+    
+    /**
+     * @return The list of constraints for fields that reference other beans.
+     */
+    private List<MetaAnnotation> buildRefFieldAnnotations() {
+        List<MetaAnnotation> anns = Lists.newArrayList();
+        HashMap<String, Object> annotParams;
+        
+        annotParams = Maps.newHashMap();
+        anns.add(new MetaAnnotation(codeModel, NotNull.class, AnnotationType.JSR_303, annotParams));
+        annotParams = Maps.newHashMap();
+        anns.add(new MetaAnnotation(codeModel, Null.class, AnnotationType.JSR_303, annotParams));
+        return anns;
+    }
 
+    /**
+     * @return A list of the class level annotations that the annotator will
+     *         use.
+     */
     private List<MetaAnnotation> buildClassAnnotations() {
 
         List<MetaAnnotation> anns = Lists.newArrayList();
-
         HashMap<String, Object> annotParams;
 
+        // AlwaysValid
+        JDefinedClass alwaysValid = buildTemplateConstraint("AlwaysValid");
+        JDefinedClass alwaysValidValidator = buildTemplateConstraintValidator("AlwaysValidValidator",
alwaysValid, Object.class);
+        JMethod isValid = getIsValidMethod(alwaysValidValidator);
+        isValid.body()._return(JExpr.TRUE);
+        alwaysValid.annotate(Constraint.class).param("validatedBy", alwaysValidValidator);
+        
+        annotParams = Maps.newHashMap();
+        anns.add(new MetaAnnotation(alwaysValid, AnnotationType.JSR_303, annotParams));
+        
+        return anns;
+    }
+    
+    
+    private JMethod getIsValidMethod(JDefinedClass alwaysValidValidator) {
+        for ( JMethod method : alwaysValidValidator.methods() ) {
+            if ( method.name().equals("isValid") ) {
+                return method;
+            }
+        }
+        return null;
+    }
 
-        try {
-
-            // OddHashcode class constraint ------------------------------------
-
-            JDefinedClass oddHashCode = codeModel._class(Config.CFG.getBasePackageName()
+ ".annot.OddHashCode", ClassType.ANNOTATION_TYPE_DECL);
-            oddHashCode.annotate(Documented.class);
-            oddHashCode.annotate(Retention.class).param("value", RetentionPolicy.RUNTIME);
-            oddHashCode.annotate(Target.class).paramArray("value").param(ElementType.TYPE).param(ElementType.ANNOTATION_TYPE);
-
-            // TODO: This part will very likely be added as a string
-            JMethod grpMethod = oddHashCode.method(JMod.NONE, Class[].class, "groups");
-            grpMethod.declareDefaultValue(new JExpressionImpl() {
-                @Override
-                public void generate(JFormatter f) {
-                    f.p("{ }");
-                }
-            });
 
-            // Create the validator
+    private JDefinedClass buildTemplateConstraint(String name) {
+        try {
+            JDefinedClass tplConstraint = codeModel._class(Config.CFG.getBasePackageName()
+ ".annot."+name, ClassType.ANNOTATION_TYPE_DECL);
+            tplConstraint.annotate(Documented.class);
+            tplConstraint.annotate(Retention.class).param("value", RetentionPolicy.RUNTIME);
+            tplConstraint.annotate(Target.class).paramArray("value").param(ElementType.TYPE).param(ElementType.ANNOTATION_TYPE).param(ElementType.FIELD).param(ElementType.METHOD);
+            
+            // Using direct as I don't know how to build default { } with code model
+            tplConstraint.direct("\n" + "    Class<?>[] groups() default {};\n" + "
   String message() default \"Invalid value\";\n" + "    Class<? extends Payload>[]
payload() default {};\n");
+            
+            // Hack to force the import of javax.validation.Payload
+            tplConstraint.javadoc().addThrows((JClass) codeModel._ref(Payload.class)).add("Force
import");
+            
+            return tplConstraint;
+        } catch (JClassAlreadyExistsException e) {
+            throw new RuntimeException("Tried to create an already existing class: " + name,
e);
+        }
+    }
+    
+    private JDefinedClass buildTemplateConstraintValidator(String name, JDefinedClass constraint,
Class<?> param) {
+        try {
             JClass cv = (JClass) codeModel._ref(ConstraintValidator.class);
-            cv = cv.narrow(oddHashCode, (JClass) codeModel._ref(Object.class));
-            JDefinedClass validator = oddHashCode._class(JMod.STATIC | JMod.PUBLIC, "OddValidator");
+            cv = cv.narrow(constraint, (JClass) codeModel._ref(param));
+            JDefinedClass validator = constraint._class(JMod.STATIC | JMod.PUBLIC, name);
             validator._implements(cv);
-            validator.method(JMod.PUBLIC, void.class, "initialize").param(oddHashCode, "parameters");
+            validator.method(JMod.PUBLIC, void.class, "initialize").param(constraint, "parameters");
             JMethod isValid = validator.method(JMod.PUBLIC, boolean.class, "isValid");
-            JVar value = isValid.param(Object.class, "value");
-            isValid.param(ConstraintValidatorContext.class, "context");
-            isValid.body()._return(value.invoke("hashCode").mod(JExpr.lit(2)).eq(JExpr.lit(1)));
-
-            oddHashCode.annotate(Constraint.class).param("validatedBy", validator);
-
-
-            // AlwaysValid class constraint ------------------------------------
-
-            JDefinedClass alwaysValid = codeModel._class(Config.CFG.getBasePackageName()
+ ".annot.AlwaysValid", ClassType.ANNOTATION_TYPE_DECL);
-            alwaysValid.annotate(Documented.class);
-            alwaysValid.annotate(Retention.class).param("value", RetentionPolicy.RUNTIME);
-            alwaysValid.annotate(Target.class).paramArray("value").param(ElementType.TYPE).param(ElementType.ANNOTATION_TYPE);
-            alwaysValid.direct("\n" + "    Class<?>[] groups() default {};\n" + " 
  String message() default \"Invalid value\";\n" + "    Class<? extends Payload>[] payload()
default {};\n");
-
-            alwaysValid.javadoc().addThrows((JClass) codeModel._ref(Payload.class)).add("Force
import");
-
-            // Create the validator
-            cv = (JClass) codeModel._ref(ConstraintValidator.class);
-            cv = cv.narrow(alwaysValid, (JClass) codeModel._ref(Object.class));
-            validator = alwaysValid._class(JMod.STATIC | JMod.PUBLIC, "AlwaysValidValidator");
-            validator._implements(cv);
-            validator.method(JMod.PUBLIC, void.class, "initialize").param(alwaysValid, "parameters");
-            isValid = validator.method(JMod.PUBLIC, boolean.class, "isValid");
             isValid.param(Object.class, "value");
             isValid.param(ConstraintValidatorContext.class, "context");
-            isValid.body()._return(JExpr.TRUE);
-
-            alwaysValid.annotate(Constraint.class).param("validatedBy", validator);
-
-            annotParams = Maps.newHashMap();
-
-            anns.add(new MetaAnnotation(alwaysValid, AnnotationType.JSR_303, annotParams));
-
+            return validator;
         } catch (JClassAlreadyExistsException e) {
-            e.printStackTrace();
+            throw new RuntimeException("Tried to create an already existing class: " + name,
e);
         }
-
-        return anns;
     }
     
     /*

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/MetaGroup.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/MetaGroup.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/MetaGroup.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/jsr303/MetaGroup.java
Fri Jun 18 12:48:48 2010
@@ -28,7 +28,7 @@ import com.sun.codemodel.JCodeModel;
  * @author Carlos Vara
  */
 public class MetaGroup extends AbstractMetaInterface {
-
+    
     public MetaGroup(JCodeModel cm, String fqn) {
         super(cm, fqn);
     }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractBaseMetaClass.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractBaseMetaClass.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractBaseMetaClass.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractBaseMetaClass.java
Fri Jun 18 12:48:48 2010
@@ -64,6 +64,9 @@ public abstract class AbstractBaseMetaCl
         return generatedClass.getPackage().name();
     }
 
+    // TODO: equals and hashCode are coded in a defensive manner, with the current
+    //  contract, generatedClass can never be null
+    
     @Override
     public boolean equals(Object obj) {
         if ( (obj == null) || !(obj instanceof AbstractBaseMetaClass) ) {
@@ -80,8 +83,12 @@ public abstract class AbstractBaseMetaCl
 
     @Override
     public int hashCode() {
-        // TODO Auto-generated method stub
-        return super.hashCode();
+        if ( this.generatedClass != null ) {
+            return this.generatedClass.hashCode();
+        }
+        else {
+            return super.hashCode();
+        }
     }
 
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaField.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaField.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaField.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaField.java
Fri Jun 18 12:48:48 2010
@@ -38,8 +38,15 @@ public abstract class AbstractMetaField 
     protected Set<MetaAnnotation> annotations = Sets.newHashSet();
 
     // The name of the field
-    protected String name;
+    private String name;
+    
+    // The owner of this field
+    private MetaJavaBean owner;
 
+    public AbstractMetaField(MetaJavaBean owner, String name) {
+        this.owner = owner;
+        this.name = name;
+    }
 
     public JFieldVar getGeneratedField() {
         return generatedField;
@@ -48,5 +55,9 @@ public abstract class AbstractMetaField 
     public String getName() {
         return name;
     }
+    
+    public MetaJavaBean getOwner() {
+        return owner;
+    }
 
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaInterface.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaInterface.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaInterface.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/AbstractMetaInterface.java
Fri Jun 18 12:48:48 2010
@@ -30,9 +30,7 @@ import com.sun.codemodel.JCodeModel;
 public abstract class AbstractMetaInterface extends AbstractBaseMetaClass {
 
     public AbstractMetaInterface(JCodeModel cm, String fqn) {
-
         super(cm, fqn, ClassType.INTERFACE);
-
     }
 
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanBasicField.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanBasicField.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanBasicField.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanBasicField.java
Fri Jun 18 12:48:48 2010
@@ -58,8 +58,9 @@ public class JavaBeanBasicField extends 
      *            The name of the meta field.
      */
     public JavaBeanBasicField(MetaJavaBean owner, String name) {
-
-        this.name = name;
+        
+        super(owner, name);
+        
         this.basicType = BasicType.getRandom();
 
         // Generate the field declaration

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanRefField.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanRefField.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanRefField.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/JavaBeanRefField.java
Fri Jun 18 12:48:48 2010
@@ -44,7 +44,8 @@ public class JavaBeanRefField extends Ab
     private JMethod setter;
     
     public JavaBeanRefField(MetaJavaBean owner, String name, MetaJavaBean refBean) {
-        this.name = name;
+        super(owner, name);
+        
         this.refBean = refBean;
         
         // Generate the field declaration

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaAnnotation.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaAnnotation.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaAnnotation.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaAnnotation.java
Fri Jun 18 12:48:48 2010
@@ -22,10 +22,13 @@ import java.lang.annotation.Annotation;
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.bval.bench.AnnotationType;
+import org.apache.bval.bench.jsr303.MetaGroup;
+import com.google.common.collect.Maps;
 import com.sun.codemodel.JClass;
 import com.sun.codemodel.JCodeModel;
 import com.sun.codemodel.JDefinedClass;
 
+
 /**
  * Wraps a JClass that represents an annotation and its parameters.
  * 
@@ -35,41 +38,48 @@ public class MetaAnnotation {
 
     // An annotation type: JSR-303, OTHER
     private final AnnotationType annotationType;
-    
+
     // The class of the annotation, which is what it defines its equals()
     private JClass annotationJClass;
 
     // A HashMap of the attributes
-    private Map<String,Object> parameters;
-    
-    
-    public MetaAnnotation(JCodeModel cm, Class<? extends Annotation> annotationClass,
AnnotationType annotationType, Map<String,Object> parameters) {
+    private Map<String, Object> parameters;
+
+
+    public MetaAnnotation(JCodeModel cm, Class<? extends Annotation> annotationClass,
AnnotationType annotationType, Map<String, Object> parameters) {
         this.annotationJClass = (JClass) cm._ref(annotationClass);
         this.annotationType = annotationType;
         this.parameters = parameters;
     }
     
-    
     public MetaAnnotation(JDefinedClass annotationJClass, AnnotationType annotationType,
HashMap<String, Object> parameters) {
         this.annotationJClass = annotationJClass;
         this.annotationType = annotationType;
         this.parameters = parameters;
     }
 
-    
+    public MetaAnnotation(MetaAnnotation src) {
+        this.annotationJClass = src.annotationJClass;
+        this.annotationType = src.annotationType;
+        this.parameters = Maps.newHashMap(src.parameters);
+    }
+
     public JClass getAnnotationClass() {
         return annotationJClass;
     }
-    
+
     public AnnotationType getAnnotationType() {
         return this.annotationType;
     }
 
-    public Map<String,Object> getParameters() {
+    public Map<String, Object> getParameters() {
         return parameters;
     }
     
+    public void setGroup(MetaGroup group) {
+        this.parameters.put("groups", group.getGeneratedClass());
+    }
     
     // TODO: equals + hashCode
-    
+
 }

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/java/org/apache/bval/bench/metatree/MetaJavaBean.java
Fri Jun 18 12:48:48 2010
@@ -19,18 +19,23 @@
 package org.apache.bval.bench.metatree;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import javax.validation.GroupSequence;
 import org.apache.bval.bench.Annotable;
+import org.apache.bval.bench.AnnotationType;
 import org.apache.bval.bench.AnnotatorVisitor;
 import org.apache.bval.bench.Config;
 import org.apache.bval.bench.Util;
+import org.apache.bval.bench.jsr303.MetaGroup;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import com.sun.codemodel.ClassType;
 import com.sun.codemodel.JAnnotationUse;
 import com.sun.codemodel.JCodeModel;
+import com.sun.codemodel.JDefinedClass;
 import com.sun.codemodel.JMod;
 
 
@@ -49,7 +54,10 @@ public class MetaJavaBean extends Abstra
     private final Map<String, AbstractMetaField> fields;
 
     // The set of annotations for this bean
-    protected Set<MetaAnnotation> annotations = Sets.newHashSet();
+    private final Set<MetaAnnotation> annotations = Sets.newHashSet();
+    
+    // The JSR-303 groups used in this bean
+    private final Set<MetaGroup> groups = Sets.newHashSet();
 
 
     /**
@@ -88,18 +96,39 @@ public class MetaJavaBean extends Abstra
         final int numInterrelations = Config.CFG.rndNumInterrelations();
 
         for (int i = 0; i < numInterrelations; ++i) {
-            int refIndex = Config.CFG.getRandomGen().nextInt(beans.size());
             String fieldName = "beanRef" + i;
-            JavaBeanRefField field = new JavaBeanRefField(this, fieldName, beans.get(refIndex));
+            JavaBeanRefField field = new JavaBeanRefField(this, fieldName, Config.CFG.getRandom(beans));
             fields.put(fieldName, field);
         }
     }
 
-
     public Collection<AbstractMetaField> getFields() {
         return this.fields.values();
     }
 
+    public void addGroup(MetaGroup group) {
+        this.groups.add(group);
+    }
+    
+    public MetaAnnotation buildGroupSequenceAnnot() {
+        if ( this.groups.isEmpty() ) {
+            return null; // No group sequence needed
+        }
+        else {
+            // Build the array of groups
+            JDefinedClass[] gs = new JDefinedClass[groups.size()+1];
+            int i=0;
+            for ( MetaGroup group : groups ) {
+                gs[i++] = group.getGeneratedClass();
+            }
+            gs[i] = getGeneratedClass(); // Add the class itself (acts as Default)
+            HashMap<String,Object> gsParams = Maps.newHashMap();
+            gsParams.put("value", gs);
+            MetaAnnotation gsAnnot = new MetaAnnotation(getGeneratedClass().owner(), GroupSequence.class,
AnnotationType.OTHER, gsParams);
+            return gsAnnot;
+        }
+    }
+    
     @Override
     public void accept(AnnotatorVisitor annotator) {
         annotator.annotate(this);

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/main/resources/generator.default.properties
Fri Jun 18 12:48:48 2010
@@ -33,4 +33,5 @@ generator.reffields.min=1
 generator.reffields.max=3
 generator.level1.fillrate=0.80
 generator.level2.fillrate=0.40
-generator.level3.fillrate=0.20
\ No newline at end of file
+generator.level3.fillrate=0.20
+generator.beanswithgroups=0.30
\ No newline at end of file

Modified: incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java?rev=955986&r1=955985&r2=955986&view=diff
==============================================================================
--- incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java
(original)
+++ incubator/bval/sandbox/jsr303-impl-bench/src/test/java/org/apache/bval/bench/AbstractBenchTest.java
Fri Jun 18 12:48:48 2010
@@ -38,7 +38,7 @@ public abstract class AbstractBenchTest 
     public abstract Validator getValidatorInNewFactory();
     
     
-    private static int TEST_TIMES = 10;
+    private static final int TEST_TIMES = 10;
     
     private static Holder holder;
     
@@ -124,11 +124,18 @@ public abstract class AbstractBenchTest 
 
     
     /**
-     * Only create the bean graph once.
+     * Only create the bean graph once.<p>
+     * Reset the counters.
      */
     @BeforeClass
     public static void createBeans() {
         holder = new Holder();
+        fpavTime = 0l;
+        fpavErrors = 0l;
+        rvTime = 0l;
+        rvErrors = 0l;
+        rpTime = 0l;
+        rpDescs = 0l;
     }
     
     



Mime
View raw message