Author: mbenson
Date: Fri Dec 5 22:12:54 2014
New Revision: 1643447
URL: http://svn.apache.org/r1643447
Log:
tweaksbval-jsr/src/main/java/org/apache/bval/jsr/xml/ValidationMappingParser.java
Modified:
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ConstructorAccess.java
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/ValidationMappingParser.java
Modified:
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ConstructorAccess.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ConstructorAccess.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ConstructorAccess.java
(original)
+++
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/ConstructorAccess.java
Fri Dec 5 22:12:54 2014
@@ -28,7 +28,6 @@ public class ConstructorAccess extends A
public ConstructorAccess(final Constructor<?> constructor) {
this.constructor = constructor;
- setAccessible(constructor);
}
@Override
Modified:
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java
(original)
+++
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/AccessStrategy.java
Fri Dec 5 22:12:54 2014
@@ -17,11 +17,8 @@
package org.apache.bval.util;
import java.lang.annotation.ElementType;
-import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Type;
-import org.apache.commons.weaver.privilizer.Privileged;
-
/**
* Description: abstract class to encapsulate different strategies
* to get the value of a Property. This class is designed such that
@@ -59,20 +56,4 @@ public abstract class AccessStrategy {
*/
public abstract String getPropertyName();
- /**
- * Set {@code accessibleObject} as being accessible
- * @param accessibleObject
- */
- protected boolean setAccessible(AccessibleObject accessibleObject) {
- if (accessibleObject.isAccessible()) {
- return false;
- }
- doSetAccessible(accessibleObject);
- return true;
- }
-
- @Privileged
- private void doSetAccessible(AccessibleObject accessibleObject) {
- accessibleObject.setAccessible(true);
- }
}
Modified:
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java
(original)
+++
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/FieldAccess.java
Fri Dec 5 22:12:54 2014
@@ -20,9 +20,14 @@ import java.lang.annotation.ElementType;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
+import org.apache.bval.util.reflection.Reflection;
+import org.apache.commons.weaver.privilizer.Privilizing;
+import org.apache.commons.weaver.privilizer.Privilizing.CallTo;
+
/**
* Description: direct field access strategy.<br/>
*/
+@Privilizing(@CallTo(Reflection.class))
public class FieldAccess extends AccessStrategy {
private final Field field;
@@ -33,17 +38,21 @@ public class FieldAccess extends AccessS
*/
public FieldAccess(final Field field) {
this.field = field;
- setAccessible(field);
}
/**
* {@inheritDoc}
*/
public Object get(final Object instance) {
+ final boolean mustUnset = Reflection.setAccessible(field, true);
try {
return field.get(instance);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
+ } finally {
+ if (mustUnset) {
+ Reflection.setAccessible(field, false);
+ }
}
}
Modified:
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java
(original)
+++
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/MethodAccess.java
Fri Dec 5 22:12:54 2014
@@ -22,9 +22,14 @@ import java.lang.reflect.InvocationTarge
import java.lang.reflect.Method;
import java.lang.reflect.Type;
+import org.apache.bval.util.reflection.Reflection;
+import org.apache.commons.weaver.privilizer.Privilizing;
+import org.apache.commons.weaver.privilizer.Privilizing.CallTo;
+
/**
* Description: invoke a zero-argument method (getter)<br/>
*/
+@Privilizing(@CallTo(Reflection.class))
public class MethodAccess extends AccessStrategy {
private final Method method;
private final String propertyName;
@@ -45,7 +50,6 @@ public class MethodAccess extends Access
public MethodAccess(String propertyName, final Method method) {
this.method = method;
this.propertyName = propertyName;
- setAccessible(method);
}
/**
@@ -80,12 +84,17 @@ public class MethodAccess extends Access
* {@inheritDoc}
*/
public Object get(final Object instance) {
+ final boolean mustUnset = Reflection.setAccessible(method, true);
try {
return method.invoke(instance);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(e);
+ } finally {
+ if (mustUnset) {
+ Reflection.setAccessible(method, false);
+ }
}
}
Modified:
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
(original)
+++
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/PropertyAccess.java
Fri Dec 5 22:12:54 2014
@@ -16,6 +16,7 @@
*/
package org.apache.bval.util;
+import org.apache.bval.util.reflection.Reflection;
import org.apache.commons.beanutils.PropertyUtils;
import java.beans.PropertyDescriptor;
@@ -149,6 +150,17 @@ public class PropertyAccess extends Acce
return null;
}
+ private static Object readField(Field field, Object bean) throws
IllegalAccessException {
+ final boolean mustUnset = Reflection.setAccessible(field, true);
+ try {
+ return field.get(bean);
+ } finally {
+ if (mustUnset) {
+ Reflection.setAccessible(field, false);
+ }
+ }
+ }
+
/**
* {@inheritDoc}
*/
@@ -162,7 +174,7 @@ public class PropertyAccess extends Acce
public Object get(Object bean) {
try {
if (rememberField != null) { // cache field of previous access
- return rememberField.get(bean);
+ return readField(rememberField, bean);
}
try { // try public method
return getPublicProperty(bean, propertyName);
@@ -180,15 +192,12 @@ public class PropertyAccess extends Acce
Field field = getField(propertyName, beanClass);
if (field != null) {
cacheField(field);
- return rememberField.get(bean);
+ return readField(rememberField, bean);
}
throw new IllegalArgumentException("cannot access field " +
propertyName);
}
private void cacheField(Field field) {
- if (!field.isAccessible()) {
- field.setAccessible(true);
- }
this.rememberField = field;
}
Modified:
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java
(original)
+++
bval/branches/bval-11/bval-core/src/main/java/org/apache/bval/util/reflection/Reflection.java
Fri Dec 5 22:12:54 2014
@@ -17,89 +17,112 @@
package org.apache.bval.util.reflection;
import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.apache.commons.lang3.ClassUtils;
+import org.apache.commons.weaver.privilizer.Privilizing;
/**
- * Security-agnostic "blueprint" class for reflection-related operations.
+ * Security-agnostic "blueprint" class for reflection-related operations.
Intended for use by Apache BVal code.
*
* @version $Rev$ $Date$
*/
public class Reflection {
- private static void setAccessibility(final Field field) {
- // FIXME 2011-03-27 jw:
- // - Why not simply call field.setAccessible(true)?
- // - Fields can not be abstract.
- if (!Modifier.isPublic(field.getModifiers())
- || (Modifier.isPublic(field.getModifiers()) &&
Modifier.isAbstract(field.getModifiers()))) {
- field.setAccessible(true);
- }
- }
-
+ /**
+ * Get the named {@link Class} from the specified {@link ClassLoader}.
+ * @param classLoader
+ * @param className
+ * @return Class
+ * @throws Exception
+ */
public static Class<?> getClass(final ClassLoader classLoader, final
String className) throws Exception {
return ClassUtils.getClass(classLoader, className, true);
}
+ /**
+ * Get the named value from the specified {@link Annotation}.
+ * @param annotation
+ * @param name
+ * @return Object value
+ * @throws IllegalAccessException
+ * @throws InvocationTargetException
+ */
public static Object getAnnotationValue(final Annotation annotation, final
String name)
throws IllegalAccessException, InvocationTargetException {
- Method valueMethod;
+ final Method valueMethod;
try {
valueMethod = annotation.annotationType().getDeclaredMethod(name);
} catch (final NoSuchMethodException ex) {
// do nothing
- valueMethod = null;
+ return null;
}
- if (null != valueMethod) {
- if (!valueMethod.isAccessible()) {
- valueMethod.setAccessible(true);
- }
+ final boolean mustUnset = setAccessible(valueMethod, true);
+ try {
return valueMethod.invoke(annotation);
+ } finally {
+ if (mustUnset) {
+ setAccessible(valueMethod, false);
+ }
}
- return null;
}
+ /**
+ * Get a usable {@link ClassLoader}: that of {@code clazz} if {@link
Thread#getContextClassLoader()} returns {@code null}.
+ * @param clazz
+ * @return {@link ClassLoader}
+ */
public static ClassLoader getClassLoader(final Class<?> clazz) {
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
- if (cl != null) {
- return cl;
- }
- return clazz.getClassLoader();
+ return cl == null ? clazz.getClassLoader() : cl;
}
+ /**
+ * Convenient point for {@link Privilizing} {@link
System#getProperty(String)}.
+ * @param name
+ * @return String
+ */
public static String getProperty(final String name) {
return System.getProperty(name);
}
+ /**
+ * Get the declared field from {@code clazz}.
+ * @param clazz
+ * @param fieldName
+ * @return {@link Field} or {@code null}
+ */
public static Field getDeclaredField(final Class<?> clazz, final String
fieldName) {
- final Field f;
try {
- f = clazz.getDeclaredField(fieldName);
+ return clazz.getDeclaredField(fieldName);
} catch (final NoSuchFieldException e) {
return null;
}
- setAccessibility(f);
- return f;
}
+ /**
+ * Convenient point for {@link Privilizing} {@link
Class#getDeclaredFields()}.
+ * @param clazz
+ * @return {@link Field} array
+ */
public static Field[] getDeclaredFields(final Class<?> clazz) {
- final Field[] fields = clazz.getDeclaredFields();
- if (fields.length > 0) {
- for (final Field f : fields) {
- if (!f.isAccessible()) {
- f.setAccessible(true);
- }
- }
- }
- return fields;
+ return clazz.getDeclaredFields();
}
- public static Constructor<?> getDeclaredConstructor(final Class<?> clazz,
final Class<?>... parameters) {
+ /**
+ * Get the declared constructor from {@code clazz}.
+ * @param T generic type
+ * @param clazz
+ * @param parameters
+ * @return {@link Constructor} or {@code null}
+ */
+ public static <T> Constructor<T> getDeclaredConstructor(final Class<T>
clazz, final Class<?>... parameters) {
try {
return clazz.getDeclaredConstructor(parameters);
} catch (final NoSuchMethodException e) {
@@ -107,6 +130,13 @@ public class Reflection {
}
}
+ /**
+ * Get the declared method from {@code clazz}.
+ * @param clazz
+ * @param name
+ * @param parameters
+ * @return {@link Method} or {@code null}
+ */
public static Method getDeclaredMethod(final Class<?> clazz, final String
name, final Class<?>... parameters) {
try {
return clazz.getDeclaredMethod(name, parameters);
@@ -115,22 +145,43 @@ public class Reflection {
}
}
+ /**
+ * Convenient point for {@link Privilizing} {@link
Class#getDeclaredMethods()}.
+ * @param clazz
+ * @return {@link Method} array
+ */
public static Method[] getDeclaredMethods(final Class<?> clazz) {
return clazz.getDeclaredMethods();
}
+ /**
+ * Convenient point for {@link Privilizing} {@link
Class#getDeclaredConstructors()}.
+ * @param clazz
+ * @return {@link Constructor} array
+ */
public static Constructor<?>[] getDeclaredConstructors(final Class<?>
clazz) {
return clazz.getDeclaredConstructors();
}
- public static Method getPublicMethod(final Class<?> clazz, final String
methodName) {
+ /**
+ * Get the specified {@code public} {@link Method} from {@code clazz}.
+ * @param clazz
+ * @param methodName
+ * @return {@link Method} or {@code null}
+ */
+ public static Method getPublicMethod(final Class<?> clazz, final String
methodName, Class<?>... parameterTypes) {
try {
- return clazz.getMethod(methodName);
+ return clazz.getMethod(methodName, parameterTypes);
} catch (final NoSuchMethodException e) {
return null;
}
}
+ /**
+ * Construct a new instance of {@code cls} using its default constructor.
+ * @param cls
+ * @return T
+ */
public static <T> T newInstance(final Class<T> cls) {
try {
return cls.newInstance();
@@ -139,4 +190,39 @@ public class Reflection {
}
}
+ /**
+ * Set the accessibility of {@code o} to {@code accessible}.
+ * @param o
+ * @param accessible
+ * @return whether a change was made.
+ */
+ public static boolean setAccessible(final AccessibleObject o, boolean
accessible) {
+ if (o == null || o.isAccessible() == accessible) {
+ return false;
+ }
+ final Member m = (Member) o;
+ if (Modifier.isPublic(m.getModifiers())) {
+ /*
+ * For objects with public accessibility, we do nothing and return
with one exception.
+ *
+ * Following explanation copied from Apache Commons [lang]
MemberUtils:
+ * When a {@code public} class has a default access superclass
with {@code public} members,
+ * these members are accessible. Calling them from compiled code
works fine.
+ * Unfortunately, on some JVMs, using reflection to invoke these
members
+ * seems to (wrongly) prevent access even when the modifier is
{@code public}.
+ * Calling {@code setAccessible(true)} solves the problem but will
only work from
+ * sufficiently privileged code.
+ */
+ if (!isPackageAccess(m.getDeclaringClass().getModifiers())) {
+ return false;
+ }
+ }
+ o.setAccessible(accessible);
+ return true;
+ }
+
+ private static boolean isPackageAccess(final int modifiers) {
+ return (modifiers & (Modifier.PRIVATE | Modifier.PROTECTED |
Modifier.PUBLIC)) == 0;
+ }
+
}
Modified:
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java
(original)
+++
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/AnnotationProxyBuilder.java
Fri Dec 5 22:12:54 2014
@@ -30,7 +30,6 @@ import javax.validation.groups.ConvertGr
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
@@ -42,8 +41,7 @@ import java.util.concurrent.ConcurrentMa
* Description: Holds the information and creates an annotation proxy during
xml
* parsing of validation mapping constraints. <br/>
*/
-// TODO move this guy up to org.apache.bval.jsr or
-// org.apache.bval.jsr.model
+// TODO move this guy up to org.apache.bval.jsr or org.apache.bval.jsr.model
@Privilizing(@CallTo(Reflection.class))
final public class AnnotationProxyBuilder<A extends Annotation> {
private static final ConcurrentMap<Class<?>, Method[]> METHODS_CACHE = new
ConcurrentHashMap<Class<?>, Method[]>();
@@ -101,19 +99,16 @@ final public class AnnotationProxyBuilde
this((Class<A>) annot.annotationType());
// Obtain the "elements" of the annotation
for (Method m : methods) {
- if (!m.isAccessible()) {
- m.setAccessible(true);
- }
+ final boolean mustUnset = Reflection.setAccessible(m, true);
try {
Object value = m.invoke(annot);
this.elements.put(m.getName(), value);
- } catch (IllegalArgumentException e) {
- // No args, so should not happen
- throw new ValidationException("Cannot access annotation " +
annot + " element: " + m.getName());
- } catch (IllegalAccessException e) {
- throw new ValidationException("Cannot access annotation " +
annot + " element: " + m.getName());
- } catch (InvocationTargetException e) {
- throw new ValidationException("Cannot access annotation " +
annot + " element: " + m.getName());
+ } catch (Exception e) {
+ throw new ValidationException("Cannot access annotation " +
annot + " element: " + m.getName(), e);
+ } finally {
+ if (mustUnset) {
+ Reflection.setAccessible(m, false);
+ }
}
}
}
Modified:
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/ValidationMappingParser.java
URL:
http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/ValidationMappingParser.java?rev=1643447&r1=1643446&r2=1643447&view=diff
==============================================================================
---
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/ValidationMappingParser.java
(original)
+++
bval/branches/bval-11/bval-jsr/src/main/java/org/apache/bval/jsr/xml/ValidationMappingParser.java
Fri Dec 5 22:12:54 2014
@@ -54,6 +54,7 @@ import org.apache.bval.util.MethodAccess
import org.apache.bval.util.reflection.Reflection;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
+import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.weaver.privilizer.Privileged;
import org.apache.commons.weaver.privilizer.Privilizing;
@@ -95,8 +96,7 @@ public class ValidationMappingParser {
if (!processedClasses.add(beanClass)) {
// spec: A given class must not be described more than
once amongst all
// the XML mapping descriptors.
- throw new ValidationException(
- beanClass.getName() + " has already be configured in
xml.");
+ throw new ValidationException(beanClass.getName() + " has
already be configured in xml.");
}
boolean ignoreAnnotations = bean.getIgnoreAnnotations() ==
null ? true : bean.getIgnoreAnnotations();
@@ -151,8 +151,7 @@ public class ValidationMappingParser {
}
// group sequence
- Class<?>[] groupSequence =
- createGroupSequence(classType.getGroupSequence(),
defaultPackage);
+ Class<?>[] groupSequence =
createGroupSequence(classType.getGroupSequence(), defaultPackage);
if (groupSequence != null) {
factory.addDefaultSequence(beanClass, groupSequence);
}
@@ -198,8 +197,7 @@ public class ValidationMappingParser {
}
}
- private <A extends Annotation> Class<?> getAnnotationParameterType(
- final Class<A> annotationClass, final String name) {
+ private <A extends Annotation> Class<?> getAnnotationParameterType(final
Class<A> annotationClass, final String name) {
final Method m = Reflection.getPublicMethod(annotationClass, name);
if (m == null) {
throw new ValidationException("Annotation of type " +
annotationClass.getName() +
@@ -219,14 +217,12 @@ public class ValidationMappingParser {
"Attempt to specify an array where single value is
expected.");
}
return getSingleValue(elementType.getContent().get(0), returnType,
defaultPackage);
- } else {
- List<Object> values = new ArrayList<Object>();
- for (Serializable s : elementType.getContent()) {
- values.add(getSingleValue(s, returnType.getComponentType(),
defaultPackage));
- }
- return values.toArray(
- (Object[]) Array.newInstance(returnType.getComponentType(),
values.size()));
}
+ List<Object> values = new ArrayList<Object>();
+ for (Serializable s : elementType.getContent()) {
+ values.add(getSingleValue(s, returnType.getComponentType(),
defaultPackage));
+ }
+ return values.toArray((Object[])
Array.newInstance(returnType.getComponentType(), values.size()));
}
private void removeEmptyContentElements(ElementType elementType) {
@@ -240,40 +236,31 @@ public class ValidationMappingParser {
}
@SuppressWarnings("unchecked")
- private Object getSingleValue(Serializable serializable, Class<?>
returnType,
- String defaultPackage) {
-
- Object returnValue;
+ private Object getSingleValue(Serializable serializable, Class<?>
returnType, String defaultPackage) {
if (serializable instanceof String) {
String value = (String) serializable;
- returnValue = convertToResultType(returnType, value,
defaultPackage);
- } else if (serializable instanceof JAXBElement<?> &&
- ((JAXBElement<?>) serializable).getDeclaredType()
- .equals(String.class)) {
- JAXBElement<?> elem = (JAXBElement<?>) serializable;
- String value = (String) elem.getValue();
- returnValue = convertToResultType(returnType, value,
defaultPackage);
- } else if (serializable instanceof JAXBElement<?> &&
- ((JAXBElement<?>) serializable).getDeclaredType()
- .equals(AnnotationType.class)) {
+ return convertToResultType(returnType, value, defaultPackage);
+ }
+ if (serializable instanceof JAXBElement<?>) {
JAXBElement<?> elem = (JAXBElement<?>) serializable;
- AnnotationType annotationType = (AnnotationType) elem.getValue();
- try {
- Class<? extends Annotation> annotationClass = (Class<? extends
Annotation>) returnType;
- returnValue =
- createAnnotation(annotationType, annotationClass,
defaultPackage);
- } catch (ClassCastException e) {
- throw new ValidationException("Unexpected parameter value");
+ if (String.class.equals(elem.getDeclaredType())) {
+ String value = (String) elem.getValue();
+ return convertToResultType(returnType, value, defaultPackage);
+ }
+ if (AnnotationType.class.equals(elem.getDeclaredType())) {
+ AnnotationType annotationType = (AnnotationType)
elem.getValue();
+ try {
+ Class<? extends Annotation> annotationClass = (Class<?
extends Annotation>) returnType;
+ return createAnnotation(annotationType, annotationClass,
defaultPackage);
+ } catch (ClassCastException e) {
+ throw new ValidationException("Unexpected parameter
value");
+ }
}
- } else {
- throw new ValidationException("Unexpected parameter value");
}
- return returnValue;
-
+ throw new ValidationException("Unexpected parameter value");
}
- private Object convertToResultType(Class<?> returnType, String value,
- String defaultPackage) {
+ private Object convertToResultType(Class<?> returnType, String value,
String defaultPackage) {
/**
* Class is represented by the fully qualified class name of the class.
* spec: Note that if the raw string is unqualified,
@@ -281,21 +268,29 @@ public class ValidationMappingParser {
*/
if (returnType.equals(Class.class)) {
value = toQualifiedClassName(value, defaultPackage);
- } else if (Byte.class.equals(returnType) ||
byte.class.equals(returnType)) { // spec mandates it
+ }
+ if (Byte.class.equals(returnType) || byte.class.equals(returnType)) {
// spec mandates it
return Byte.parseByte(value);
- } else if (Short.class.equals(returnType) ||
short.class.equals(returnType)) {
+ }
+ if (Short.class.equals(returnType) || short.class.equals(returnType)) {
return Short.parseShort(value);
- } else if (Integer.class.equals(returnType) ||
int.class.equals(returnType)) {
+ }
+ if (Integer.class.equals(returnType) || int.class.equals(returnType)) {
return Integer.parseInt(value);
- } else if (Long.class.equals(returnType) ||
long.class.equals(returnType)) {
+ }
+ if (Long.class.equals(returnType) || long.class.equals(returnType)) {
return Long.parseLong(value);
- } else if (Float.class.equals(returnType) ||
float.class.equals(returnType)) {
+ }
+ if (Float.class.equals(returnType) || float.class.equals(returnType)) {
return Float.parseFloat(value);
- } else if (Double.class.equals(returnType) ||
double.class.equals(returnType)) {
+ }
+ if (Double.class.equals(returnType) ||
double.class.equals(returnType)) {
return Double.parseDouble(value);
- } else if (Boolean.class.equals(returnType) ||
boolean.class.equals(returnType)) {
+ }
+ if (Boolean.class.equals(returnType) ||
boolean.class.equals(returnType)) {
return Boolean.parseBoolean(value);
- } else if (Character.class.equals(returnType) ||
char.class.equals(returnType)) {
+ }
+ if (Character.class.equals(returnType) ||
char.class.equals(returnType)) {
if (value.length() > 1) {
throw new IllegalArgumentException("a char has a length of 1");
}
@@ -308,11 +303,10 @@ public class ValidationMappingParser {
converter = EnumerationConverter.getInstance();
}
- if (converter != null) {
- return converter.convert(returnType, value);
- } else {
+ if (converter == null) {
return converter;
}
+ return converter.convert(returnType, value);
}
private <A extends Annotation> Annotation createAnnotation(AnnotationType
annotationType,
@@ -330,7 +324,7 @@ public class ValidationMappingParser {
private Class<?>[] getGroups(GroupsType groupsType, String defaultPackage)
{
if (groupsType == null) {
- return new Class[]{};
+ return ArrayUtils.EMPTY_CLASS_ARRAY;
}
List<Class<?>> groupList = new ArrayList<Class<?>>();
@@ -340,10 +334,8 @@ public class ValidationMappingParser {
return groupList.toArray(new Class[groupList.size()]);
}
-
@SuppressWarnings("unchecked")
- private Class<? extends Payload>[] getPayload(PayloadType payloadType,
- String defaultPackage) {
+ private Class<? extends Payload>[] getPayload(PayloadType payloadType,
String defaultPackage) {
if (payloadType == null) {
return new Class[]{};
}
@@ -354,15 +346,13 @@ public class ValidationMappingParser {
if (!Payload.class.isAssignableFrom(payload)) {
throw new ValidationException("Specified payload class " +
payload.getName() +
" does not implement javax.validation.Payload");
- } else {
- payloadList.add((Class<? extends Payload>) payload);
}
+ payloadList.add((Class<? extends Payload>) payload);
}
return payloadList.toArray(new Class[payloadList.size()]);
}
- private Class<?>[] createGroupSequence(GroupSequenceType groupSequenceType,
- String defaultPackage) {
+ private Class<?>[] createGroupSequence(GroupSequenceType
groupSequenceType, String defaultPackage) {
if (groupSequenceType != null) {
Class<?>[] groupSequence = new
Class<?>[groupSequenceType.getValue().size()];
int i=0;
@@ -371,20 +361,20 @@ public class ValidationMappingParser {
groupSequence[i++] = group;
}
return groupSequence;
- } else {
- return null;
}
+ return null;
}
- private <A> void processMethodLevel(final List<MethodType> methods, final
Class<A> beanClass, final String defaultPackage, final boolean parentIgnoreAnn,
final Collection<String> getters) {
+ private <A> void processMethodLevel(final List<MethodType> methods, final
Class<A> beanClass,
+ final String defaultPackage, final boolean parentIgnoreAnn, final
Collection<String> getters) {
final List<String> methodNames = new ArrayList<String>();
for (final MethodType methodType : methods) {
final String methodName = methodType.getName();
if (methodNames.contains(methodName) ||
getters.contains(methodName)) {
throw new ValidationException(methodName + " is defined more
than once in mapping xml for bean " + beanClass.getName());
- } else {
- methodNames.add(methodName);
- }
+ }
+ methodNames.add(methodName);
+
final Method method = Reflection.getDeclaredMethod(beanClass,
methodName, toTypes(methodType.getParameter(), defaultPackage));
if (method == null) {
throw new ValidationException(beanClass.getName() + " does not
contain the method " + methodName);
@@ -395,10 +385,10 @@ public class ValidationMappingParser {
factory.getAnnotationIgnores().setIgnoreAnnotationsOnMember(method,
ignoreMethodAnnotation);
final boolean ignoreAnn;
- if (methodType.getIgnoreAnnotations() != null) {
- ignoreAnn = methodType.getIgnoreAnnotations();
- } else {
+ if (methodType.getIgnoreAnnotations() == null) {
ignoreAnn = parentIgnoreAnn;
+ } else {
+ ignoreAnn = methodType.getIgnoreAnnotations();
}
// constraints
@@ -410,7 +400,9 @@ public class ValidationMappingParser {
factory.addMetaConstraint(beanClass, constraint);
}
if (p.getValid() != null) {
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, method,
AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass, method,
+ AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
constraint.setIndex(i);
factory.addMetaConstraint(beanClass, constraint);
}
@@ -419,7 +411,9 @@ public class ValidationMappingParser {
for (final GroupConversionType groupConversion :
p.getConvertGroup()) {
final Class<?> from =
loadClass(groupConversion.getFrom(), defaultPackage);
final Class<?> to = loadClass(groupConversion.getTo(),
defaultPackage);
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, method, new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass,
method,
+ new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
constraint.setIndex(i);
factory.addMetaConstraint(beanClass, constraint);
}
@@ -438,7 +432,9 @@ public class ValidationMappingParser {
factory.addMetaConstraint(beanClass, constraint);
}
if (returnValue.getValid() != null) {
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, method,
AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass, method,
+ AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
factory.addMetaConstraint(beanClass, constraint);
}
@@ -446,11 +442,14 @@ public class ValidationMappingParser {
for (final GroupConversionType groupConversion :
returnValue.getConvertGroup()) {
final Class<?> from =
loadClass(groupConversion.getFrom(), defaultPackage);
final Class<?> to = loadClass(groupConversion.getTo(),
defaultPackage);
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, method, new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass,
method,
+ new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
factory.addMetaConstraint(beanClass, constraint);
}
}
-
factory.getAnnotationIgnores().setIgnoreAnnotationOnReturn(method,
returnValue.getIgnoreAnnotations() == null ? ignoreAnn :
returnValue.getIgnoreAnnotations());
+
factory.getAnnotationIgnores().setIgnoreAnnotationOnReturn(method,
+ returnValue.getIgnoreAnnotations() == null ? ignoreAnn :
returnValue.getIgnoreAnnotations());
}
final CrossParameterType crossParameter =
methodType.getCrossParameter();
@@ -459,27 +458,31 @@ public class ValidationMappingParser {
final MetaConstraint<?, ?> constraint =
createConstraint(constraintType, beanClass, method, defaultPackage);
factory.addMetaConstraint(beanClass, constraint);
}
-
factory.getAnnotationIgnores().setIgnoreAnnotationOnCrossParameter(method,
crossParameter.getIgnoreAnnotations() != null ?
crossParameter.getIgnoreAnnotations() : ignoreAnn);
+
factory.getAnnotationIgnores().setIgnoreAnnotationOnCrossParameter(method,
+ crossParameter.getIgnoreAnnotations() != null ?
crossParameter.getIgnoreAnnotations() : ignoreAnn);
}
}
}
- private <A> void processConstructorLevel(final List<ConstructorType>
constructors, final Class<A> beanClass, final String defaultPackage, final
boolean parentIgnore) {
+ private <A> void processConstructorLevel(final List<ConstructorType>
constructors, final Class<A> beanClass,
+ final String defaultPackage, final boolean parentIgnore) {
for (final ConstructorType constructorType : constructors) {
- final Constructor<?> constructor =
Reflection.getDeclaredConstructor(beanClass,
toTypes(constructorType.getParameter(), defaultPackage));
+ final Constructor<?> constructor =
+ Reflection.getDeclaredConstructor(beanClass,
toTypes(constructorType.getParameter(), defaultPackage));
if (constructor == null) {
throw new ValidationException(beanClass.getName() + " does not
contain the constructor " + constructorType);
}
// ignore annotations
- final boolean ignoreMethodAnnotation =
constructorType.getIgnoreAnnotations() == null ? parentIgnore :
constructorType.getIgnoreAnnotations();
+ final boolean ignoreMethodAnnotation =
+ constructorType.getIgnoreAnnotations() == null ? parentIgnore
: constructorType.getIgnoreAnnotations();
factory.getAnnotationIgnores().setIgnoreAnnotationsOnMember(constructor,
ignoreMethodAnnotation);
final boolean ignoreAnn;
- if (constructorType.getIgnoreAnnotations() != null) {
- ignoreAnn = constructorType.getIgnoreAnnotations();
- } else {
+ if (constructorType.getIgnoreAnnotations() == null) {
ignoreAnn = parentIgnore;
+ } else {
+ ignoreAnn = constructorType.getIgnoreAnnotations();
}
// constraints
@@ -491,7 +494,9 @@ public class ValidationMappingParser {
factory.addMetaConstraint(beanClass, constraint);
}
if (p.getValid() != null) {
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, constructor,
AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass,
constructor,
+ AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
constraint.setIndex(i);
factory.addMetaConstraint(beanClass, constraint);
}
@@ -500,17 +505,21 @@ public class ValidationMappingParser {
for (final GroupConversionType groupConversion :
p.getConvertGroup()) {
final Class<?> from =
loadClass(groupConversion.getFrom(), defaultPackage);
final Class<?> to = loadClass(groupConversion.getTo(),
defaultPackage);
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, constructor, new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass,
constructor,
+ new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
constraint.setIndex(i);
factory.addMetaConstraint(beanClass, constraint);
}
}
- boolean ignoreParametersAnnotation = p.getIgnoreAnnotations()
== null ? ignoreMethodAnnotation : p.getIgnoreAnnotations();
+ boolean ignoreParametersAnnotation =
+ p.getIgnoreAnnotations() == null ? ignoreMethodAnnotation
: p.getIgnoreAnnotations();
if (ignoreParametersAnnotation || (ignoreMethodAnnotation &&
p.getIgnoreAnnotations() == null)) {
-
+ // TODO what ?
}
-
factory.getAnnotationIgnores().setIgnoreAnnotationsOnParameter(constructor, i,
p.getIgnoreAnnotations() != null ? p.getIgnoreAnnotations() : ignoreAnn);
+
factory.getAnnotationIgnores().setIgnoreAnnotationsOnParameter(constructor, i,
+ p.getIgnoreAnnotations() != null ?
p.getIgnoreAnnotations() : ignoreAnn);
i++;
}
@@ -523,7 +532,9 @@ public class ValidationMappingParser {
factory.addMetaConstraint(beanClass, constraint);
}
if (returnValue.getValid() != null) {
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, constructor,
AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass,
constructor,
+ AnnotationProxyBuilder.ValidAnnotation.INSTANCE);
constraint.setIndex(-1);
factory.addMetaConstraint(beanClass, constraint);
}
@@ -532,12 +543,15 @@ public class ValidationMappingParser {
for (final GroupConversionType groupConversion :
returnValue.getConvertGroup()) {
final Class<?> from =
loadClass(groupConversion.getFrom(), defaultPackage);
final Class<?> to = loadClass(groupConversion.getTo(),
defaultPackage);
- final MetaConstraint<?, ?> constraint = new
MetaConstraint<A, Annotation>(beanClass, constructor, new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
+ final MetaConstraint<?, ?> constraint =
+ new MetaConstraint<A, Annotation>(beanClass,
constructor,
+ new
AnnotationProxyBuilder.ConvertGroupAnnotation(from, to));
constraint.setIndex(-1);
factory.addMetaConstraint(beanClass, constraint);
}
}
-
factory.getAnnotationIgnores().setIgnoreAnnotationOnReturn(constructor,
returnValue.getIgnoreAnnotations() != null ? returnValue.getIgnoreAnnotations()
: ignoreAnn);
+
factory.getAnnotationIgnores().setIgnoreAnnotationOnReturn(constructor,
+ returnValue.getIgnoreAnnotations() != null ?
returnValue.getIgnoreAnnotations() : ignoreAnn);
}
final CrossParameterType crossParameter =
constructorType.getCrossParameter();
@@ -546,7 +560,8 @@ public class ValidationMappingParser {
final MetaConstraint<?, ?> constraint =
createConstraint(constraintType, beanClass, constructor, defaultPackage);
factory.addMetaConstraint(beanClass, constraint);
}
-
factory.getAnnotationIgnores().setIgnoreAnnotationOnCrossParameter(constructor,
crossParameter.getIgnoreAnnotations() != null ?
crossParameter.getIgnoreAnnotations() : ignoreAnn);
+
factory.getAnnotationIgnores().setIgnoreAnnotationOnCrossParameter(constructor,
+ crossParameter.getIgnoreAnnotations() != null ?
crossParameter.getIgnoreAnnotations() : ignoreAnn);
}
}
}
@@ -569,16 +584,14 @@ public class ValidationMappingParser {
for (FieldType fieldType : fields) {
String fieldName = fieldType.getName();
if (fieldNames.contains(fieldName)) {
- throw new ValidationException(fieldName +
- " is defined more than once in mapping xml for bean " +
- beanClass.getName());
- } else {
- fieldNames.add(fieldName);
- }
+ throw new ValidationException(fieldName + " is defined more
than once in mapping xml for bean "
+ + beanClass.getName());
+ }
+ fieldNames.add(fieldName);
+
final Field field = Reflection.getDeclaredField(beanClass,
fieldName);
if (field == null) {
- throw new ValidationException(
- beanClass.getName() + " does not contain the fieldType
" + fieldName);
+ throw new ValidationException(beanClass.getName() + " does not
contain the fieldType " + fieldName);
}
// ignore annotations
@@ -616,9 +629,9 @@ public class ValidationMappingParser {
throw new ValidationException(getterName +
" is defined more than once in mapping xml for bean " +
beanClass.getName());
- } else {
- getterNames.add(methodName);
}
+ getterNames.add(methodName);
+
final Method method = getGetter(beanClass, getterName);
if (method == null) {
throw new ValidationException(
@@ -716,14 +729,14 @@ public class ValidationMappingParser {
Class<? extends ConstraintValidator<?, ?>>[] validator =
factory.getDefaultConstraints().getValidatorClasses(annotationType);
- if (validator != null) {
- classes
- .addAll(Arrays.asList(validator));
- } else {
- Class<? extends ConstraintValidator<?, ?>>[] validatedBy =
annotationType
- .getAnnotation(Constraint.class)
- .validatedBy();
+ if (validator == null) {
+ /* Collections.addAll() would be more straightforward here, but
there is an Oracle compiler bug of some sort
+ * that precludes this:
+ */
+ Class<? extends ConstraintValidator<?, ?>>[] validatedBy =
annotationType.getAnnotation(Constraint.class).validatedBy();
classes.addAll(Arrays.asList(validatedBy));
+ } else {
+ Collections.addAll(classes, validator);
}
return classes;
}
@@ -748,7 +761,7 @@ public class ValidationMappingParser {
}
@Privileged
- private static Method getGetter(Class<?> clazz,String propertyName) {
+ private static Method getGetter(Class<?> clazz, String propertyName) {
try {
final String p = StringUtils.capitalize(propertyName);
try {