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 2011/02/02 21:29:43 UTC

svn commit: r1066614 - /incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicMetaGraphManagerImpl.java

Author: mbenson
Date: Wed Feb  2 20:29:43 2011
New Revision: 1066614

URL: http://svn.apache.org/viewvc?rev=1066614&view=rev
Log:
fmt; refactor to make more clear that only when dynamic metadata is present do we merge

Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicMetaGraphManagerImpl.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicMetaGraphManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicMetaGraphManagerImpl.java?rev=1066614&r1=1066613&r2=1066614&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicMetaGraphManagerImpl.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/jsr303/dynamic/DynamicMetaGraphManagerImpl.java Wed Feb  2 20:29:43 2011
@@ -60,7 +60,7 @@ import org.slf4j.LoggerFactory;
 
 /**
  * {@link DynamicMetaGraphManager} implementation.
- *
+ * 
  * @version $Rev$ $Date$
  */
 final class DynamicMetaGraphManagerImpl implements DynamicMetaGraphManager {
@@ -121,7 +121,7 @@ final class DynamicMetaGraphManagerImpl 
 
         /**
          * Create a new SetupConstraintCallback instance.
-         *
+         * 
          * @param root
          */
         NavigateOrBuildGraph(MetaBean root) {
@@ -220,7 +220,6 @@ final class DynamicMetaGraphManagerImpl 
          */
         public MetaBean getMetaBean(Class<?> type) {
             final MetaBean initial = metaBeanFinder.findForClass(type);
-            MetaBean result = initial;
 
             final PathImpl path;
             final Object rootBean;
@@ -238,17 +237,15 @@ final class DynamicMetaGraphManagerImpl 
             }
             try {
                 /*
-                 * For each step of the path, we need to know whether the bean
-                 * we are looking for matches a mapped property, so we build a
-                 * stack of path/bean pairs. We use a stack so that when we
-                 * process, the longest path goes last and therefore wins over
-                 * short paths: the rationale here is that the longer path is
-                 * more specific thus more likely to be rooted at more important
-                 * part of your domain model.
+                 * For each step of the path, we need to know whether the bean we are looking for matches a mapped
+                 * property, so we build a stack of path/bean pairs. We use a stack so that when we process, the longest
+                 * path goes last and therefore wins over short paths: the rationale here is that the longer path is
+                 * more specific thus more likely to be rooted at more important part of your domain model.
                  */
                 Deque<Pair<? extends PathImpl, Class<?>>> pathStack =
                     buildPathStack(path, rootBean, rootMetaBean.getBeanClass());
 
+                ArrayList<MetaBean> toMerge = new ArrayList<MetaBean>();
                 while (!pathStack.isEmpty()) {
                     Pair<? extends PathImpl, Class<?>> pair = pathStack.pop();
                     List<MetaBean> dynamicInfo = getAssignableDynamicInfo(pair.right);
@@ -256,13 +253,16 @@ final class DynamicMetaGraphManagerImpl 
                     PathImpl relativePath = pair.left;
                     for (MetaBean root : dynamicInfo) {
                         MetaBean leaf = findLeaf(root, relativePath);
-                        if (leaf == null) {
-                            continue;
-                        }
-                        if (result == initial) {
-                            result = initial.copy();
+                        if (leaf != null) {
+                            toMerge.add(leaf);
                         }
+                    }
+                }
+                if (!toMerge.isEmpty()) {
+
+                    MetaBean result = initial.copy();
 
+                    for (MetaBean leaf : toMerge) {
                         // copy bean + property features and constraints:
                         mergeFeatures(result, leaf);
 
@@ -270,36 +270,31 @@ final class DynamicMetaGraphManagerImpl 
                             MetaProperty targetProperty = getRequiredProperty(result, sourceProperty.getName());
                             mergeFeatures(targetProperty, sourceProperty);
                         }
-
-                        // TODO try paring down data returned from readOnly implementation
                     }
-                }
 
-                if (result == initial) {
-                    return result;
-                }
-                // now the dynamic constraints should FINALLY be fully
-                // populated:
+                    // now the dynamic constraints should FINALLY be fully
+                    // populated:
 
-                {
-                    Collection<Annotation> constraints =
-                        result.getFeature(DYNAMIC_CONSTRAINT_COLLECTION, EMPTY_DYNAMIC_ANNOTATIONS);
-                    for (Annotation constraint : constraints) {
-                        annotationProcessor.processAnnotation(constraint, result.getBeanClass(),
-                            new AppendValidationToMeta(result));
+                    {
+                        Collection<Annotation> constraints =
+                            result.getFeature(DYNAMIC_CONSTRAINT_COLLECTION, EMPTY_DYNAMIC_ANNOTATIONS);
+                        for (Annotation constraint : constraints) {
+                            annotationProcessor.processAnnotation(constraint, result.getBeanClass(),
+                                new AppendValidationToMeta(result));
+                        }
                     }
-                }
-                for (MetaProperty property : result.getProperties()) {
-                    String propertyName = property.getName();
-                    Collection<Annotation> constraints =
-                        property.getFeature(DYNAMIC_CONSTRAINT_COLLECTION, EMPTY_DYNAMIC_ANNOTATIONS);
-                    for (Annotation constraint : constraints) {
-                        annotationProcessor.processAnnotation(constraint, property, result.getBeanClass(),
-                            new PropertyAccess(result.getBeanClass(), propertyName), new AppendValidationToMeta(
-                                property));
+                    for (MetaProperty property : result.getProperties()) {
+                        String propertyName = property.getName();
+                        Collection<Annotation> constraints =
+                            property.getFeature(DYNAMIC_CONSTRAINT_COLLECTION, EMPTY_DYNAMIC_ANNOTATIONS);
+                        for (Annotation constraint : constraints) {
+                            annotationProcessor.processAnnotation(constraint, property, result.getBeanClass(),
+                                new PropertyAccess(result.getBeanClass(), propertyName), new AppendValidationToMeta(
+                                    property));
+                        }
                     }
+                    return result;
                 }
-
             } catch (Exception e) {
                 StringBuilder msg =
                     new StringBuilder("Encountered error applying dynamic constraints for type ")
@@ -309,7 +304,7 @@ final class DynamicMetaGraphManagerImpl 
                 }
                 log.error(msg.toString(), e);
             }
-            return result;
+            return initial;
         }
 
         /**
@@ -372,7 +367,7 @@ final class DynamicMetaGraphManagerImpl 
 
     /**
      * Create a new DynamicMetaGraphManagerImpl instance.
-     *
+     * 
      * @param annotationProcessor
      * @param metaBeanFinder
      */
@@ -460,8 +455,8 @@ final class DynamicMetaGraphManagerImpl 
     }
 
     /*
-     * build a stack of pairs of path + metabean representing each object in the
-     * currently known path, i.e. what we're trying to complete the bean for
+     * build a stack of pairs of path + metabean representing each object in the currently known path, i.e. what we're
+     * trying to complete the bean for
      */
     private static Deque<Pair<? extends PathImpl, Class<?>>> buildPathStack(final PathImpl fullPath, Object rootBean,
         Class<?> rootType) {
@@ -503,7 +498,7 @@ final class DynamicMetaGraphManagerImpl 
                 rawType = TypeUtils.getRawType(access.getJavaType(), type);
                 type = access.getJavaType();
                 if (node.getName() != null) {
-                    //substitute a name-only node and repeat the loop:
+                    // substitute a name-only node and repeat the loop:
                     nodes.add(0, new NodeImpl(node.getName()));
                     continue;
                 }