Author: mbenson
Date: Fri Dec 5 22:14:29 2014
New Revision: 1643450
URL: http://svn.apache.org/r1643450
Log:
revert 1643447
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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -28,6 +28,7 @@ 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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -17,8 +17,11 @@
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
@@ -56,4 +59,20 @@ 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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -20,14 +20,9 @@ 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;
@@ -38,21 +33,17 @@ 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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -22,14 +22,9 @@ 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;
@@ -50,6 +45,7 @@ public class MethodAccess extends Access
public MethodAccess(String propertyName, final Method method) {
this.method = method;
this.propertyName = propertyName;
+ setAccessible(method);
}
/**
@@ -84,17 +80,12 @@ 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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -16,7 +16,6 @@
*/
package org.apache.bval.util;
-import org.apache.bval.util.reflection.Reflection;
import org.apache.commons.beanutils.PropertyUtils;
import java.beans.PropertyDescriptor;
@@ -150,17 +149,6 @@ 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}
*/
@@ -174,7 +162,7 @@ public class PropertyAccess extends Acce
public Object get(Object bean) {
try {
if (rememberField != null) { // cache field of previous access
- return readField(rememberField, bean);
+ return rememberField.get(bean);
}
try { // try public method
return getPublicProperty(bean, propertyName);
@@ -192,12 +180,15 @@ public class PropertyAccess extends Acce
Field field = getField(propertyName, beanClass);
if (field != null) {
cacheField(field);
- return readField(rememberField, bean);
+ return rememberField.get(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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -17,112 +17,89 @@
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.
Intended for use by Apache BVal code.
+ * Security-agnostic "blueprint" class for reflection-related operations.
*
* @version $Rev$ $Date$
*/
public class Reflection {
- /**
- * Get the named {@link Class} from the specified {@link ClassLoader}.
- * @param classLoader
- * @param className
- * @return Class
- * @throws Exception
- */
+ 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);
+ }
+ }
+
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 {
- final Method valueMethod;
+ Method valueMethod;
try {
valueMethod = annotation.annotationType().getDeclaredMethod(name);
} catch (final NoSuchMethodException ex) {
// do nothing
- return null;
+ valueMethod = null;
}
- final boolean mustUnset = setAccessible(valueMethod, true);
- try {
- return valueMethod.invoke(annotation);
- } finally {
- if (mustUnset) {
- setAccessible(valueMethod, false);
+ if (null != valueMethod) {
+ if (!valueMethod.isAccessible()) {
+ valueMethod.setAccessible(true);
}
+ return valueMethod.invoke(annotation);
}
+ 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();
- return cl == null ? clazz.getClassLoader() : cl;
+ if (cl != null) {
+ return cl;
+ }
+ return clazz.getClassLoader();
}
- /**
- * 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 {
- return clazz.getDeclaredField(fieldName);
+ f = 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) {
- return clazz.getDeclaredFields();
+ final Field[] fields = clazz.getDeclaredFields();
+ if (fields.length > 0) {
+ for (final Field f : fields) {
+ if (!f.isAccessible()) {
+ f.setAccessible(true);
+ }
+ }
+ }
+ return fields;
}
- /**
- * 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) {
+ public static Constructor<?> getDeclaredConstructor(final Class<?> clazz,
final Class<?>... parameters) {
try {
return clazz.getDeclaredConstructor(parameters);
} catch (final NoSuchMethodException e) {
@@ -130,13 +107,6 @@ 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);
@@ -145,43 +115,22 @@ 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();
}
- /**
- * 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) {
+ public static Method getPublicMethod(final Class<?> clazz, final String
methodName) {
try {
- return clazz.getMethod(methodName, parameterTypes);
+ return clazz.getMethod(methodName);
} 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();
@@ -190,39 +139,4 @@ 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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -30,6 +30,7 @@ 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;
@@ -41,7 +42,8 @@ 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[]>();
@@ -99,16 +101,19 @@ final public class AnnotationProxyBuilde
this((Class<A>) annot.annotationType());
// Obtain the "elements" of the annotation
for (Method m : methods) {
- final boolean mustUnset = Reflection.setAccessible(m, true);
+ if (!m.isAccessible()) {
+ m.setAccessible(true);
+ }
try {
Object value = m.invoke(annot);
this.elements.put(m.getName(), value);
- } catch (Exception e) {
- throw new ValidationException("Cannot access annotation " +
annot + " element: " + m.getName(), e);
- } finally {
- if (mustUnset) {
- Reflection.setAccessible(m, false);
- }
+ } 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());
}
}
}
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=1643450&r1=1643449&r2=1643450&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:14:29 2014
@@ -54,7 +54,6 @@ 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;
@@ -96,7 +95,8 @@ 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,7 +151,8 @@ public class ValidationMappingParser {
}
// group sequence
- Class<?>[] groupSequence =
createGroupSequence(classType.getGroupSequence(), defaultPackage);
+ Class<?>[] groupSequence =
+ createGroupSequence(classType.getGroupSequence(),
defaultPackage);
if (groupSequence != null) {
factory.addDefaultSequence(beanClass, groupSequence);
}
@@ -197,7 +198,8 @@ 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() +
@@ -217,12 +219,14 @@ 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) {
@@ -236,31 +240,40 @@ public class ValidationMappingParser {
}
@SuppressWarnings("unchecked")
- private Object getSingleValue(Serializable serializable, Class<?>
returnType, String defaultPackage) {
+ private Object getSingleValue(Serializable serializable, Class<?>
returnType,
+ String defaultPackage) {
+
+ Object returnValue;
if (serializable instanceof String) {
String value = (String) serializable;
- return convertToResultType(returnType, value, defaultPackage);
- }
- if (serializable instanceof JAXBElement<?>) {
+ returnValue = convertToResultType(returnType, value,
defaultPackage);
+ } else if (serializable instanceof JAXBElement<?> &&
+ ((JAXBElement<?>) serializable).getDeclaredType()
+ .equals(String.class)) {
JAXBElement<?> elem = (JAXBElement<?>) serializable;
- 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");
- }
+ String value = (String) elem.getValue();
+ returnValue = convertToResultType(returnType, value,
defaultPackage);
+ } else if (serializable instanceof JAXBElement<?> &&
+ ((JAXBElement<?>) serializable).getDeclaredType()
+ .equals(AnnotationType.class)) {
+ 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");
}
+ } else {
+ throw new ValidationException("Unexpected parameter value");
}
- throw new ValidationException("Unexpected parameter value");
+ return returnValue;
+
}
- 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,
@@ -268,29 +281,21 @@ public class ValidationMappingParser {
*/
if (returnType.equals(Class.class)) {
value = toQualifiedClassName(value, defaultPackage);
- }
- if (Byte.class.equals(returnType) || byte.class.equals(returnType)) {
// spec mandates it
+ } else if (Byte.class.equals(returnType) ||
byte.class.equals(returnType)) { // spec mandates it
return Byte.parseByte(value);
- }
- if (Short.class.equals(returnType) || short.class.equals(returnType)) {
+ } else if (Short.class.equals(returnType) ||
short.class.equals(returnType)) {
return Short.parseShort(value);
- }
- if (Integer.class.equals(returnType) || int.class.equals(returnType)) {
+ } else if (Integer.class.equals(returnType) ||
int.class.equals(returnType)) {
return Integer.parseInt(value);
- }
- if (Long.class.equals(returnType) || long.class.equals(returnType)) {
+ } else if (Long.class.equals(returnType) ||
long.class.equals(returnType)) {
return Long.parseLong(value);
- }
- if (Float.class.equals(returnType) || float.class.equals(returnType)) {
+ } else if (Float.class.equals(returnType) ||
float.class.equals(returnType)) {
return Float.parseFloat(value);
- }
- if (Double.class.equals(returnType) ||
double.class.equals(returnType)) {
+ } else if (Double.class.equals(returnType) ||
double.class.equals(returnType)) {
return Double.parseDouble(value);
- }
- if (Boolean.class.equals(returnType) ||
boolean.class.equals(returnType)) {
+ } else if (Boolean.class.equals(returnType) ||
boolean.class.equals(returnType)) {
return Boolean.parseBoolean(value);
- }
- if (Character.class.equals(returnType) ||
char.class.equals(returnType)) {
+ } else if (Character.class.equals(returnType) ||
char.class.equals(returnType)) {
if (value.length() > 1) {
throw new IllegalArgumentException("a char has a length of 1");
}
@@ -303,10 +308,11 @@ public class ValidationMappingParser {
converter = EnumerationConverter.getInstance();
}
- if (converter == null) {
+ if (converter != null) {
+ return converter.convert(returnType, value);
+ } else {
return converter;
}
- return converter.convert(returnType, value);
}
private <A extends Annotation> Annotation createAnnotation(AnnotationType
annotationType,
@@ -324,7 +330,7 @@ public class ValidationMappingParser {
private Class<?>[] getGroups(GroupsType groupsType, String defaultPackage)
{
if (groupsType == null) {
- return ArrayUtils.EMPTY_CLASS_ARRAY;
+ return new Class[]{};
}
List<Class<?>> groupList = new ArrayList<Class<?>>();
@@ -334,8 +340,10 @@ 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[]{};
}
@@ -346,13 +354,15 @@ 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;
@@ -361,20 +371,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());
- }
- methodNames.add(methodName);
-
+ } else {
+ 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);
@@ -385,10 +395,10 @@ public class ValidationMappingParser {
factory.getAnnotationIgnores().setIgnoreAnnotationsOnMember(method,
ignoreMethodAnnotation);
final boolean ignoreAnn;
- if (methodType.getIgnoreAnnotations() == null) {
- ignoreAnn = parentIgnoreAnn;
- } else {
+ if (methodType.getIgnoreAnnotations() != null) {
ignoreAnn = methodType.getIgnoreAnnotations();
+ } else {
+ ignoreAnn = parentIgnoreAnn;
}
// constraints
@@ -400,9 +410,7 @@ 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);
}
@@ -411,9 +419,7 @@ 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);
}
@@ -432,9 +438,7 @@ 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);
}
@@ -442,14 +446,11 @@ 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();
@@ -458,31 +459,27 @@ 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 = parentIgnore;
- } else {
+ if (constructorType.getIgnoreAnnotations() != null) {
ignoreAnn = constructorType.getIgnoreAnnotations();
+ } else {
+ ignoreAnn = parentIgnore;
}
// constraints
@@ -494,9 +491,7 @@ 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);
}
@@ -505,21 +500,17 @@ 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++;
}
@@ -532,9 +523,7 @@ 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);
}
@@ -543,15 +532,12 @@ 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();
@@ -560,8 +546,7 @@ 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);
}
}
}
@@ -584,14 +569,16 @@ 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());
- }
- fieldNames.add(fieldName);
-
+ throw new ValidationException(fieldName +
+ " is defined more than once in mapping xml for bean " +
+ beanClass.getName());
+ } else {
+ 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
@@ -629,9 +616,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(
@@ -729,14 +716,14 @@ public class ValidationMappingParser {
Class<? extends ConstraintValidator<?, ?>>[] validator =
factory.getDefaultConstraints().getValidatorClasses(annotationType);
- 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));
+ if (validator != null) {
+ classes
+ .addAll(Arrays.asList(validator));
} else {
- Collections.addAll(classes, validator);
+ Class<? extends ConstraintValidator<?, ?>>[] validatedBy =
annotationType
+ .getAnnotation(Constraint.class)
+ .validatedBy();
+ classes.addAll(Arrays.asList(validatedBy));
}
return classes;
}
@@ -761,7 +748,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 {