bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject [bval] 10/16: code audit: data integrity; removed a useless/misleading protected method (pre 1.0); DRY; unnecessary else; etc.
Date Mon, 10 Jun 2019 05:02:24 GMT
This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to tag 0.5
in repository https://gitbox.apache.org/repos/asf/bval.git

commit c005f77d79c299c74c6b84a5d8df92b59cca27d0
Author: mbenson <mbenson@13f79535-47bb-0310-9956-ffa450edef68>
AuthorDate: Mon Sep 17 16:09:28 2012 +0000

    code audit: data integrity; removed a useless/misleading protected method (pre 1.0); DRY;
unnecessary else; etc.
    
    git-svn-id: http://svn.apache.org/repos/asf/bval/trunk@1386687 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/bval/jsr303/ApacheValidatorFactory.java | 113 ++++++++++-----------
 1 file changed, 51 insertions(+), 62 deletions(-)

diff --git a/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorFactory.java
b/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorFactory.java
index ac5b8e8..0c85083 100644
--- a/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorFactory.java
+++ b/bval-jsr303/src/main/java/org/apache/bval/jsr303/ApacheValidatorFactory.java
@@ -41,6 +41,7 @@ import org.apache.bval.jsr303.xml.AnnotationIgnores;
 import org.apache.bval.jsr303.xml.MetaConstraint;
 import org.apache.bval.jsr303.xml.ValidationMappingParser;
 import org.apache.bval.util.AccessStrategy;
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.ClassUtils;
 
 /**
@@ -132,16 +133,6 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
     }
 
     /**
-     * Get the default {@link MessageInterpolator} used by this
-     * {@link ApacheValidatorFactory}.
-     *
-     * @return {@link MessageInterpolator}
-     */
-    protected MessageInterpolator getDefaultMessageInterpolator() {
-        return messageResolver;
-    }
-
-    /**
      * Shortcut method to create a new Validator instance with factory's
      * settings
      *
@@ -185,8 +176,7 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      * {@inheritDoc}
      */
     public MessageInterpolator getMessageInterpolator() {
-        return ((messageResolver != null) ? messageResolver
-                : getDefaultMessageInterpolator());
+        return messageResolver;
     }
 
     /**
@@ -233,31 +223,32 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      * @throws ValidationException if the provider does not support the call.
      */
     public <T> T unwrap(final Class<T> type) {
-        // FIXME 2011-03-27 jw:
-        // This code is unsecure.
-        // It should allow only a fixed set of classes.
-        // Can't fix this because don't know which classes this method should support.
-
         if (type.isInstance(this)) {
             @SuppressWarnings("unchecked")
             T result = (T) this;
             return result;
-        } else if (!(type.isInterface() || Modifier.isAbstract(type
+        }
+
+        // FIXME 2011-03-27 jw:
+        // This code is unsecure.
+        // It should allow only a fixed set of classes.
+        // Can't fix this because don't know which classes this method should support.
+        
+        if (!(type.isInterface() || Modifier.isAbstract(type
                 .getModifiers()))) {
             return newInstance(type);
-        } else {
-            try {
-                Class<?> cls = ClassUtils.getClass(type.getName() + "Impl");
-                if (type.isAssignableFrom(cls)) {
-                    @SuppressWarnings("unchecked")
-                    T result = (T) newInstance(cls);
-                    return result;
-                }
-            } catch (ClassNotFoundException e) {
-                // do nothing
+        }
+        try {
+            Class<?> cls = ClassUtils.getClass(type.getName() + "Impl");
+            if (type.isAssignableFrom(cls)) {
+                @SuppressWarnings("unchecked")
+                T result = (T) newInstance(cls);
+                return result;
             }
-            throw new ValidationException("Type " + type + " not supported");
+        } catch (ClassNotFoundException e) {
+            // do nothing
         }
+        throw new ValidationException("Type " + type + " not supported");
     }
 
     private <T> T newInstance(final Class<T> cls) {
@@ -308,16 +299,15 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      */
     public void addMetaConstraint(Class<?> beanClass,
                                   MetaConstraint<?, ?> metaConstraint) {
-        List<MetaConstraint<?, ? extends Annotation>> slot =
-                constraintMap.get(beanClass);
-        if (slot != null) {
-            slot.add(metaConstraint);
-        } else {
-            List<MetaConstraint<?, ? extends Annotation>> constraintList =
-                    new ArrayList<MetaConstraint<?, ? extends Annotation>>();
-            constraintList.add(metaConstraint);
-            constraintMap.put(beanClass, constraintList);
+        List<MetaConstraint<?, ? extends Annotation>> slot;
+        synchronized (constraintMap) {
+            slot = constraintMap.get(beanClass);
+            if (slot == null) {
+                slot = new ArrayList<MetaConstraint<?, ? extends Annotation>>();
+                constraintMap.put(beanClass, slot);
+            }
         }
+        slot.add(metaConstraint);
     }
 
     /**
@@ -328,14 +318,15 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      *            defining the property to validate
      */
     public void addValid(Class<?> beanClass, AccessStrategy accessStrategy) {
-        List<AccessStrategy> slot = validAccesses.get(beanClass);
-        if (slot != null) {
-            slot.add(accessStrategy);
-        } else {
-            List<AccessStrategy> tmpList = new ArrayList<AccessStrategy>();
-            tmpList.add(accessStrategy);
-            validAccesses.put(beanClass, tmpList);
+        List<AccessStrategy> slot;
+        synchronized (validAccesses) {
+            slot = validAccesses.get(beanClass);
+            if (slot == null) {
+                slot = new ArrayList<AccessStrategy>();
+                validAccesses.put(beanClass, slot);
+            }
         }
+        slot.add(accessStrategy);
     }
 
     /**
@@ -344,8 +335,8 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      * @param beanClass
      * @param groupSequence
      */
-    public void addDefaultSequence(Class<?> beanClass, Class<?>[] groupSequence)
{
-        defaultSequences.put(beanClass, groupSequence);
+    public void addDefaultSequence(Class<?> beanClass, Class<?>... groupSequence)
{
+        defaultSequences.put(beanClass, safeArray(groupSequence));
     }
 
     /**
@@ -356,17 +347,16 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      * @return List of {@link MetaConstraint}s applicable to
      *         <code>beanClass</code>
      */
-    @SuppressWarnings({ "unchecked", "rawtypes" })
     public <T> List<MetaConstraint<T, ? extends Annotation>> getMetaConstraints(
             Class<T> beanClass) {
-        List<MetaConstraint<?, ? extends Annotation>> slot =
-                constraintMap.get(beanClass);
-        if (slot != null) {
-            // noinspection RedundantCast
-            return (List) slot;
-        } else {
-            return Collections.EMPTY_LIST;
+        final List<MetaConstraint<?, ? extends Annotation>> slot = constraintMap.get(beanClass);
+        if (slot == null) {
+            return Collections.emptyList();
         }
+        // noinspection RedundantCast
+        @SuppressWarnings({ "unchecked", "rawtypes" })
+        final List<MetaConstraint<T, ? extends Annotation>> result = (List) slot;
+        return Collections.unmodifiableList(result);
     }
 
     /**
@@ -378,12 +368,8 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      * @return {@link List} of {@link AccessStrategy}
      */
     public List<AccessStrategy> getValidAccesses(Class<?> beanClass) {
-        List<AccessStrategy> slot = validAccesses.get(beanClass);
-        if (slot != null) {
-            return slot;
-        } else {
-            return Collections.<AccessStrategy>emptyList();
-        }
+        final List<AccessStrategy> slot = validAccesses.get(beanClass);
+        return slot == null ? Collections.<AccessStrategy> emptyList() : Collections.unmodifiableList(slot);
     }
 
     /**
@@ -393,7 +379,10 @@ public class ApacheValidatorFactory implements ValidatorFactory, Cloneable
{
      * @return group Class array
      */
     public Class<?>[] getDefaultSequence(Class<?> beanClass) {
-        return defaultSequences.get(beanClass);
+        return safeArray(defaultSequences.get(beanClass));
     }
 
+    private static Class<?>[] safeArray(Class<?>... array) {
+        return ArrayUtils.isEmpty(array) ? ArrayUtils.EMPTY_CLASS_ARRAY : ArrayUtils.clone(array);
+    }
 }


Mime
View raw message