incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r993591 - in /incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303: AnnotationConstraintBuilder.java AnnotationProcessor.java ConstraintAnnotationAttributes.java xml/AnnotationProxyBuilder.java
Date Wed, 08 Sep 2010 02:47:30 GMT
Author: mbenson
Date: Wed Sep  8 02:47:29 2010
New Revision: 993591

URL: http://svn.apache.org/viewvc?rev=993591&view=rev
Log:
ConstraintAnnotationAttributes enum to centralize treatment of well-known attribute names

Added:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java
  (with props)
Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationProxyBuilder.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java?rev=993591&r1=993590&r2=993591&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
Wed Sep  8 02:47:29 2010
@@ -52,10 +52,6 @@ import org.apache.commons.logging.LogFac
 final class AnnotationConstraintBuilder<A extends Annotation> {
     private static final Log log = LogFactory.getLog(AnnotationConstraintBuilder.class);
 
-    // TODO extract these to some neutral location; see AnnotationProxyBuilder
-    private static final String ANNOTATION_PAYLOAD = "payload";
-    private static final String ANNOTATION_GROUPS = "groups";
-
     private final ConstraintValidation<?> constraintValidation;
     private List<ConstraintOverrides> overrides;
 
@@ -88,9 +84,10 @@ final class AnnotationConstraintBuilder<
                         // checked by TCK-Tests)
                         if (method.getParameterTypes().length == 0) {
                             try {
-                                if (ANNOTATION_PAYLOAD.equals(method.getName())) {
+                                if (ConstraintAnnotationAttributes.PAYLOAD.getAttributeName().equals(method.getName()))
{
                                     buildPayload(method);
-                                } else if (ANNOTATION_GROUPS.equals(method.getName())) {
+                                } else if (ConstraintAnnotationAttributes.GROUPS.getAttributeName().equals(
+                                    method.getName())) {
                                     buildGroups(method);
                                 } else {
                                     constraintValidation.getAttributes().put(method.getName(),

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java?rev=993591&r1=993590&r2=993591&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationProcessor.java
Wed Sep  8 02:47:29 2010
@@ -137,7 +137,7 @@ public final class AnnotationProcessor {
          * array of constraint annotations in a special way.
          */
         Object result =
-            SecureActions.getAnnotationValue(annotation, "value");
+            SecureActions.getAnnotationValue(annotation, ConstraintAnnotationAttributes.VALUE.getAttributeName());
         if (result != null && result instanceof Annotation[]) {
             boolean changed = false;
             for (Annotation each : (Annotation[]) result) {

Added: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java?rev=993591&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java
(added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java
Wed Sep  8 02:47:29 2010
@@ -0,0 +1,123 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.bval.jsr303;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import org.apache.commons.lang3.reflect.TypeUtils;
+
+/**
+ * Defines the well-known attributes of {@link Constraint} annotations.
+ * 
+ * @version $Rev$ $Date$
+ */
+public enum ConstraintAnnotationAttributes {
+    /**
+     * "message"
+     */
+    MESSAGE,
+
+    /**
+     * "groups"
+     */
+    GROUPS,
+
+    /**
+     * "payload"
+     */
+    PAYLOAD,
+
+    /**
+     * "value" for multi-valued constraints
+     */
+    VALUE;
+
+    @SuppressWarnings("unused")
+    private static class Types {
+        String message;
+        Class<?>[] groups;
+        Class<? extends Payload>[] payload;
+        Annotation[] value;
+    }
+
+    private Type type;
+
+    private ConstraintAnnotationAttributes() {
+        try {
+            this.type = Types.class.getDeclaredField(getAttributeName()).getGenericType();
+        } catch (Exception e) {
+            //should never happen
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Get the expected type of the represented attribute.
+     * 
+     * @return Class<?>
+     */
+    public Type getType() {
+        return type;
+    }
+
+    /**
+     * Get the attribute name represented.
+     * 
+     * @return String
+     */
+    public String getAttributeName() {
+        return name().toLowerCase(Locale.US);
+    }
+
+    /**
+     * Put <code>value</code> into a map with <code>this.attributeName</code>
as
+     * key.
+     * 
+     * @param <V>
+     * @param map
+     * @param value
+     * @return previous value mapped to <code>this.attributeName</code>
+     */
+    public <V> Object put(Map<? super String, ? super V> map, V value) {
+        if (!TypeUtils.isInstance(value, getType())) {
+            throw new IllegalArgumentException(String.format("Invalid '%s' value: %s", getAttributeName(),
value));
+        }
+        return map.put(getAttributeName(), value);
+    }
+
+    /**
+     * Get the value of <code>this.attributeName</code> from <code>map</code>.
+     * 
+     * @param <V>
+     * @param map
+     * @return V if you say so
+     */
+    public <V> V get(Map<? super String, ? super V> map) {
+        @SuppressWarnings("unchecked")
+        final V result = (V) map.get(getAttributeName());
+        if (!TypeUtils.isInstance(result, getType())) {
+            throw new IllegalStateException(String.format("Invalid '%s' value: %s", getAttributeName(),
result));
+        }
+        return result;
+    }
+}

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintAnnotationAttributes.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationProxyBuilder.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationProxyBuilder.java?rev=993591&r1=993590&r2=993591&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationProxyBuilder.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/xml/AnnotationProxyBuilder.java
Wed Sep  8 02:47:29 2010
@@ -27,6 +27,7 @@ import java.util.Map;
 import javax.validation.Payload;
 import javax.validation.ValidationException;
 
+import org.apache.bval.jsr303.ConstraintAnnotationAttributes;
 import org.apache.bval.jsr303.util.SecureActions;
 
 /**
@@ -36,14 +37,7 @@ import org.apache.bval.jsr303.util.Secur
 // TODO move this guy up to org.apache.bval.jsr303 or
 // org.apache.bval.jsr303.model
 final public class AnnotationProxyBuilder<A extends Annotation> {
-    // TODO extract these to some neutral location; see
-    // AnnotationConstraintBuilder
-    private static final String ANNOTATION_PAYLOAD = "payload";
-    private static final String ANNOTATION_GROUPS = "groups";
-    private static final String ANNOTATION_MESSAGE = "message";
-
     private final Class<A> type;
-
     private final Map<String, Object> elements = new HashMap<String, Object>();
 
     /**
@@ -153,7 +147,7 @@ final public class AnnotationProxyBuilde
      * @param message
      */
     public void setMessage(String message) {
-        putValue(ANNOTATION_MESSAGE, message);
+        ConstraintAnnotationAttributes.MESSAGE.put(elements, message);
     }
 
     /**
@@ -162,7 +156,7 @@ final public class AnnotationProxyBuilde
      * @param groups
      */
     public void setGroups(Class<?>[] groups) {
-        putValue(ANNOTATION_GROUPS, groups);
+        ConstraintAnnotationAttributes.GROUPS.put(elements, groups);
     }
 
     /**
@@ -171,7 +165,7 @@ final public class AnnotationProxyBuilde
      * @param payload
      */
     public void setPayload(Class<? extends Payload>[] payload) {
-        putValue(ANNOTATION_PAYLOAD, payload);
+        ConstraintAnnotationAttributes.PAYLOAD.put(elements, payload);
     }
 
     /**



Mime
View raw message