You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by mb...@apache.org on 2010/09/30 23:59:40 UTC

svn commit: r1003311 - in /incubator/bval/sandbox/lang3-work: bval-core/src/main/java/org/apache/bval/util/ bval-jsr303/src/main/java/org/apache/bval/jsr303/ bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ bval-jsr303d/src/main/java/org/apache/b...

Author: mbenson
Date: Thu Sep 30 21:59:40 2010
New Revision: 1003311

URL: http://svn.apache.org/viewvc?rev=1003311&view=rev
Log:
extract ValidationContextTraversal from NestedPathNavigation.Traversal; make navigation an overridable function of the ClassValidator; DynamicClassValidator has the ability to handle generic paths when invoked outside the context of a particular object graph

Added:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ValidationContextTraversal.java   (with props)
Removed:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/NestedPathNavigator.java
Modified:
    incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicClassValidator.java

Modified: incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java?rev=1003311&r1=1003310&r2=1003311&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-core/src/main/java/org/apache/bval/util/IndexedAccess.java Thu Sep 30 21:59:40 2010
@@ -49,7 +49,7 @@ public class IndexedAccess extends Acces
     }
 
     private Type containerType;
-    private int index;
+    private Integer index;
 
     /**
      * Create a new IndexedAccessStrategy instance.
@@ -57,7 +57,7 @@ public class IndexedAccess extends Acces
      * @param containerType
      * @param index
      */
-    public IndexedAccess(Type containerType, int index) {
+    public IndexedAccess(Type containerType, Integer index) {
         super();
         this.containerType = containerType;
         this.index = index;
@@ -68,6 +68,9 @@ public class IndexedAccess extends Acces
      */
     @Override
     public Object get(Object instance) {
+        if (index == null) {
+            throw new UnsupportedOperationException("Cannot read null index");
+        }
         if (instance != null && instance.getClass().isArray()) {
             if (Array.getLength(instance) - index > 0) {
                 return Array.get(instance, index);

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java?rev=1003311&r1=1003310&r2=1003311&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java Thu Sep 30 21:59:40 2010
@@ -31,9 +31,10 @@ import org.apache.bval.jsr303.groups.Gro
 import org.apache.bval.jsr303.groups.Groups;
 import org.apache.bval.jsr303.groups.GroupsComputer;
 import org.apache.bval.jsr303.util.ClassHelper;
-import org.apache.bval.jsr303.util.NestedPathNavigator;
+import org.apache.bval.jsr303.util.ValidationContextTraversal;
 import org.apache.bval.jsr303.util.NodeImpl;
 import org.apache.bval.jsr303.util.PathImpl;
+import org.apache.bval.jsr303.util.PathNavigation;
 import org.apache.bval.jsr303.util.SecureActions;
 import org.apache.bval.model.Features;
 import org.apache.bval.model.MetaBean;
@@ -67,11 +68,6 @@ public class ClassValidator implements C
     protected final GroupsComputer groupsComputer = new GroupsComputer();
 
     /**
-     * {@link NestedPathNavigator} used
-     */
-    protected final NestedPathNavigator nestedPathNavigator = new NestedPathNavigator();
-
-    /**
      * Create a new ClassValidator instance.
      * 
      * @param factoryContext
@@ -211,7 +207,7 @@ public class ClassValidator implements C
 
             GroupValidationContext<T> context = createContext(objectMetaBean, object, objectClass, groups);
             ConstraintValidationListener<T> result = context.getListener();
-            nestedPathNavigator.navigate(context, propertyName);
+            navigateContextToPath(context, propertyName);
             Groups sequence = context.getGroups();
 
             // 1. process groups
@@ -298,7 +294,7 @@ public class ClassValidator implements C
             MetaBean metaBean = factoryContext.getMetaBeanFinder().findForClass(beanType);
             GroupValidationContext<T> context = createContext(metaBean, null, beanType, groups);
             ConstraintValidationListener<T> result = context.getListener();
-            nestedPathNavigator.navigate(context, propertyName);
+            navigateContextToPath(context, propertyName);
             if (context.getMetaProperty() == null) {
                 context.setBean(value);
             } else {
@@ -773,4 +769,12 @@ public class ClassValidator implements C
 
     }
 
+    /**
+     * Navigate the specified {@link GroupValidationContext} to <code>propertyPath</code>.
+     * @param validationContext
+     * @param propertyPath
+     */
+    protected <T> void navigateContextToPath(GroupValidationContext<T> validationContext, String propertyPath) {
+        PathNavigation.navigate(propertyPath, new ValidationContextTraversal(validationContext));
+    }
 }

Added: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ValidationContextTraversal.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ValidationContextTraversal.java?rev=1003311&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ValidationContextTraversal.java (added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/util/ValidationContextTraversal.java Thu Sep 30 21:59:40 2010
@@ -0,0 +1,155 @@
+/*
+ * 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.util;
+
+import java.lang.reflect.Type;
+
+import org.apache.bval.DynamicMetaBean;
+import org.apache.bval.jsr303.UnknownPropertyException;
+import org.apache.bval.jsr303.util.PathNavigation.CallbackProcedure;
+import org.apache.bval.model.MetaBean;
+import org.apache.bval.model.MetaProperty;
+import org.apache.bval.model.ValidationContext;
+import org.apache.bval.util.AccessStrategy;
+import org.apache.bval.util.IndexedAccess;
+import org.apache.bval.util.KeyedAccess;
+import org.apache.bval.util.PropertyAccess;
+import org.apache.commons.lang3.reflect.TypeUtils;
+
+/**
+ * {@link ValidationContext} traversal {@link CallbackProcedure}.
+ * 
+ * @version $Rev$ $Date$
+ */
+public class ValidationContextTraversal extends CallbackProcedure {
+    private static class NullSafePropertyAccess extends PropertyAccess {
+
+        /**
+         * Create a new NullSafePropertyAccess instance.
+         * 
+         * @param clazz
+         * @param propertyName
+         */
+        public NullSafePropertyAccess(Class<?> clazz, String propertyName) {
+            super(clazz, propertyName);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public Object get(Object bean) {
+            return bean == null ? null : super.get(bean);
+        }
+    }
+
+    private ValidationContext<?> validationContext;
+    private Type type;
+    private Class<?> rawType;
+
+    /**
+     * Create a new {@link ValidationContextTraversal} instance.
+     * 
+     * @param validationContext
+     */
+    public ValidationContextTraversal(ValidationContext<?> validationContext) {
+        this.validationContext = validationContext;
+        this.rawType = validationContext.getMetaBean().getBeanClass();
+        this.type = this.rawType;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void handleIndexOrKey(String token) {
+        moveDownIfNecessary();
+
+        AccessStrategy access;
+        if (IndexedAccess.getJavaElementType(type) != null) {
+            try {
+                Integer index = token == null ? null : Integer.valueOf(token);
+                access = new IndexedAccess(type, index);
+                validationContext.setCurrentIndex(index);
+            } catch (NumberFormatException e) {
+                throw new UnknownPropertyException(String.format("Cannot parse %s as an array/iterable index", token),
+                    e);
+            }
+        } else if (KeyedAccess.getJavaElementType(type) != null) {
+            access = new KeyedAccess(type, token);
+            validationContext.setCurrentKey(token);
+        } else {
+            throw new UnknownPropertyException(String.format("Cannot determine index/key type for %s", type));
+        }
+        Object value = validationContext.getBean();
+        Object child = value == null ? null : access.get(value);
+        setType(access.getJavaType());
+        validationContext.setBean(child,
+            validationContext.getMetaBean().resolveMetaBean(child == null ? rawType : child));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void handleProperty(String token) {
+        moveDownIfNecessary();
+
+        MetaBean metaBean = validationContext.getMetaBean();
+
+        if (metaBean instanceof DynamicMetaBean) {
+            metaBean =
+                metaBean.resolveMetaBean(validationContext.getBean() == null ? rawType : validationContext.getBean());
+        }
+        MetaProperty mp = metaBean.getProperty(token);
+        if (mp == null) {
+            throw new UnknownPropertyException("unknown property '" + token + "' in " + metaBean.getId());
+        }
+        validationContext.setMetaProperty(mp);
+        setType(mp.getType());
+    }
+
+    /**
+     * If we currently have a property, navigate the context such that the property becomes the bean, in preparation for
+     * another property.
+     * 
+     * @param validationContext
+     */
+    protected void moveDownIfNecessary() {
+        MetaProperty mp = validationContext.getMetaProperty();
+        if (mp != null) {
+            validationContext.moveDown(mp, new NullSafePropertyAccess(validationContext.getMetaBean().getBeanClass(),
+                mp.getName()));
+        }
+    }
+
+    /**
+     * Set the type of the expression processed thus far.
+     * 
+     * @param type
+     */
+    protected void setType(Type type) {
+        this.rawType = TypeUtils.getRawType(type, this.type);
+        this.type = type;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void handleGenericInIterable() {
+        throw new UnsupportedOperationException("Cannot navigate a ValidationContext to []");
+    }
+
+}
\ No newline at end of file

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

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

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicClassValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicClassValidator.java?rev=1003311&r1=1003310&r2=1003311&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicClassValidator.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicClassValidator.java Thu Sep 30 21:59:40 2010
@@ -30,10 +30,13 @@ import org.apache.bval.jsr303.BeanDescri
 import org.apache.bval.jsr303.ClassValidator;
 import org.apache.bval.jsr303.ElementDescriptorImpl;
 import org.apache.bval.jsr303.GroupValidationContext;
+import org.apache.bval.jsr303.util.PathNavigation;
+import org.apache.bval.jsr303.util.ValidationContextTraversal;
 import org.apache.bval.model.Features;
 import org.apache.bval.model.MetaBean;
 import org.apache.bval.model.MetaProperty;
 import org.apache.bval.model.Validation;
+import org.apache.bval.model.ValidationContext;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.Validate;
 
@@ -155,7 +158,7 @@ public class DynamicClassValidator exten
                 GroupValidationContext<?> validationContext = createContext(metaBean, null, metaBean.getBeanClass());
 
                 try {
-                    nestedPathNavigator.navigate(validationContext, propertyName);
+                    navigateContextToPath(validationContext, propertyName);
                 } catch (ValidationException e) {
                     return null;
                 }
@@ -216,4 +219,38 @@ public class DynamicClassValidator exten
         }
 
     }
+
+    private static class DynamicValidationContextTraversal extends ValidationContextTraversal {
+        private final boolean allowGenericInIterable;
+
+        /**
+         * Create a new DynamicValidationContextTraversal instance.
+         * @param validationContext
+         */
+        public DynamicValidationContextTraversal(ValidationContext<?> validationContext) {
+            super(validationContext);
+            //null bean implies hypothetical, so we allow []:
+            this.allowGenericInIterable = validationContext.getBean() == null;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public void handleGenericInIterable() {
+            if (allowGenericInIterable) {
+                super.handleIndexOrKey(null);
+            } else {
+                super.handleGenericInIterable();
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected <T> void navigateContextToPath(GroupValidationContext<T> validationContext, String propertyPath) {
+        PathNavigation.navigate(propertyPath, new DynamicValidationContextTraversal(validationContext));
+    }
 }