[hibernate-commits] Hibernate SVN: r19800 - in validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator: util/annotationfactory and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jun 23 12:18:17 EDT 2010


Author: hardy.ferentschik
Date: 2010-06-23 12:18:16 -0400 (Wed, 23 Jun 2010)
New Revision: 19800

Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java
Log:
HV-341

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java	2010-06-23 14:55:58 UTC (rev 19799)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/BeanMetaDataImpl.java	2010-06-23 16:18:16 UTC (rev 19800)
@@ -440,8 +440,9 @@
 		List<ConstraintDescriptorImpl<?>> constraintDescriptors = new ArrayList<ConstraintDescriptorImpl<?>>();
 
 		List<Annotation> constraints = new ArrayList<Annotation>();
-		if ( constraintHelper.isConstraintAnnotation( annotation ) ||
-				constraintHelper.isBuiltinConstraint( annotation.annotationType() ) ) {
+		Class<? extends Annotation> annotationType = annotation.annotationType();
+		if ( constraintHelper.isConstraintAnnotation( annotationType )
+				|| constraintHelper.isBuiltinConstraint( annotationType ) ) {
 			constraints.add( annotation );
 		}
 

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java	2010-06-23 14:55:58 UTC (rev 19799)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintDescriptorImpl.java	2010-06-23 16:18:16 UTC (rev 19800)
@@ -83,6 +83,11 @@
 	private final T annotation;
 
 	/**
+	 * The type of the annotation made instance variable, because {@code annotation.annotationType()} is quite expensive.
+	 */
+	private final Class<T> annotationType;
+
+	/**
 	 * The set of classes implementing the validation for this constraint. See also
 	 * <code>ConstraintValidator</code> resolution algorithm.
 	 */
@@ -134,10 +139,11 @@
 
 	public ConstraintDescriptorImpl(T annotation, ConstraintHelper constraintHelper, Class<?> implicitGroup, ElementType type, ConstraintOrigin definedOn) {
 		this.annotation = annotation;
+		this.annotationType = ( Class<T> ) this.annotation.annotationType();
 		this.constraintHelper = constraintHelper;
 		this.elementType = type;
 		this.definedOn = definedOn;
-		this.isReportAsSingleInvalidConstraint = annotation.annotationType().isAnnotationPresent(
+		this.isReportAsSingleInvalidConstraint = annotationType.isAnnotationPresent(
 				ReportAsSingleViolation.class
 		);
 
@@ -191,7 +197,6 @@
 	}
 
 	private List<Class<? extends ConstraintValidator<T, ?>>> findConstraintValidatorClasses() {
-		final Class<T> annotationType = getAnnotationType();
 		final List<Class<? extends ConstraintValidator<T, ?>>> constraintValidatorClasses = new ArrayList<Class<? extends ConstraintValidator<T, ?>>>();
 		if ( constraintHelper.containsConstraintValidatorDefinition( annotationType ) ) {
 			for ( Class<? extends ConstraintValidator<T, ?>> validator : constraintHelper
@@ -202,7 +207,7 @@
 		}
 
 		List<Class<? extends ConstraintValidator<? extends Annotation, ?>>> constraintDefinitionClasses = new ArrayList<Class<? extends ConstraintValidator<? extends Annotation, ?>>>();
-		if ( constraintHelper.isBuiltinConstraint( annotation.annotationType() ) ) {
+		if ( constraintHelper.isBuiltinConstraint( annotationType ) ) {
 			constraintDefinitionClasses.addAll( constraintHelper.getBuiltInConstraints( annotationType ) );
 		}
 		else {
@@ -213,7 +218,7 @@
 		}
 
 		constraintHelper.addConstraintValidatorDefinition(
-				annotation.annotationType(), constraintDefinitionClasses
+				annotationType, constraintDefinitionClasses
 		);
 
 		for ( Class<? extends ConstraintValidator<? extends Annotation, ?>> validator : constraintDefinitionClasses ) {
@@ -224,11 +229,6 @@
 		return Collections.unmodifiableList( constraintValidatorClasses );
 	}
 
-	@SuppressWarnings("unchecked")
-	private Class<T> getAnnotationType() {
-		return ( Class<T> ) annotation.annotationType();
-	}
-
 	public T getAnnotation() {
 		return annotation;
 	}
@@ -269,7 +269,7 @@
 	public String toString() {
 		final StringBuilder sb = new StringBuilder();
 		sb.append( "ConstraintDescriptorImpl" );
-		sb.append( "{annotation=" ).append( annotation.annotationType().getName() );
+		sb.append( "{annotation=" ).append( annotationType.getName() );
 		sb.append( ", payloads=" ).append( payloads );
 		sb.append( ", hasComposingConstraints=" ).append( composingConstraints.isEmpty() );
 		sb.append( ", isReportAsSingleInvalidConstraint=" ).append( isReportAsSingleInvalidConstraint );
@@ -315,7 +315,7 @@
 
 	private Map<ClassIndexWrapper, Map<String, Object>> parseOverrideParameters() {
 		Map<ClassIndexWrapper, Map<String, Object>> overrideParameters = new HashMap<ClassIndexWrapper, Map<String, Object>>();
-		final Method[] methods = ReflectionHelper.getMethods( annotation.annotationType() );
+		final Method[] methods = ReflectionHelper.getMethods( annotationType );
 		for ( Method m : methods ) {
 			if ( m.getAnnotation( OverridesAttribute.class ) != null ) {
 				addOverrideAttributes(
@@ -370,19 +370,22 @@
 		Set<ConstraintDescriptor<?>> composingConstraintsSet = new HashSet<ConstraintDescriptor<?>>();
 		Map<ClassIndexWrapper, Map<String, Object>> overrideParameters = parseOverrideParameters();
 
-		for ( Annotation declaredAnnotation : annotation.annotationType().getDeclaredAnnotations() ) {
-			if ( NON_COMPOSING_CONSTRAINT_ANNOTATIONS.contains( declaredAnnotation.annotationType().getName() ) ) {
+		for ( Annotation declaredAnnotation : annotationType.getDeclaredAnnotations() ) {
+			Class<? extends Annotation> declaredAnnotationType = declaredAnnotation.annotationType();
+			if ( NON_COMPOSING_CONSTRAINT_ANNOTATIONS.contains( declaredAnnotationType.getName() ) ) {
 				// ignore the usual suspects which will be in almost any constraint, but are no composing constraint
 				continue;
 			}
 
-			if ( constraintHelper.isConstraintAnnotation( declaredAnnotation )
-					|| constraintHelper.isBuiltinConstraint( declaredAnnotation.annotationType() ) ) {
+			if ( constraintHelper.isConstraintAnnotation( declaredAnnotationType )
+					|| constraintHelper.isBuiltinConstraint( declaredAnnotationType ) ) {
 				ConstraintDescriptorImpl<?> descriptor = createComposingConstraintDescriptor(
 						declaredAnnotation, overrideParameters, OVERRIDES_PARAMETER_DEFAULT_INDEX
 				);
 				composingConstraintsSet.add( descriptor );
-				log.debug( "Adding composing constraint: " + descriptor );
+				if ( log.isDebugEnabled() ) {
+					log.debug( "Adding composing constraint: " + descriptor );
+				}
 			}
 			else if ( constraintHelper.isMultiValueConstraint( declaredAnnotation ) ) {
 				List<Annotation> multiValueConstraints = constraintHelper.getMultiValueConstraints( declaredAnnotation );
@@ -392,7 +395,9 @@
 							constraintAnnotation, overrideParameters, index
 					);
 					composingConstraintsSet.add( descriptor );
-					log.debug( "Adding composing constraint: " + descriptor );
+					if ( log.isDebugEnabled() ) {
+						log.debug( "Adding composing constraint: " + descriptor );
+					}
 					index++;
 				}
 			}
@@ -401,8 +406,6 @@
 	}
 
 	private <U extends Annotation> ConstraintDescriptorImpl<U> createComposingConstraintDescriptor(U declaredAnnotation, Map<ClassIndexWrapper, Map<String, Object>> overrideParameters, int index) {
-		//TODO don't quite understand this warning
-		//TODO assuming U.getClass() returns Class<U>
 		@SuppressWarnings("unchecked")
 		final Class<U> annotationType = ( Class<U> ) declaredAnnotation.annotationType();
 		return createComposingConstraintDescriptor(

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java	2010-06-23 14:55:58 UTC (rev 19799)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/metadata/ConstraintHelper.java	2010-06-23 16:18:16 UTC (rev 19800)
@@ -198,7 +198,8 @@
 				if ( returnType.isArray() && returnType.getComponentType().isAnnotation() ) {
 					Annotation[] annotations = ( Annotation[] ) method.invoke( annotation );
 					for ( Annotation a : annotations ) {
-						if ( isConstraintAnnotation( a ) || isBuiltinConstraint( a.annotationType() ) ) {
+						Class<? extends Annotation> annotationType = a.annotationType();
+						if ( isConstraintAnnotation( annotationType ) || isBuiltinConstraint( annotationType ) ) {
 							isMultiValueConstraint = true;
 						}
 						else {
@@ -236,7 +237,8 @@
 				if ( returnType.isArray() && returnType.getComponentType().isAnnotation() ) {
 					Annotation[] annotations = ( Annotation[] ) method.invoke( annotation );
 					for ( Annotation a : annotations ) {
-						if ( isConstraintAnnotation( a ) || isBuiltinConstraint( a.annotationType() ) ) {
+						Class<? extends Annotation> annotationType = a.annotationType();
+						if ( isConstraintAnnotation( annotationType ) || isBuiltinConstraint( annotationType ) ) {
 							annotationList.add( a );
 						}
 					}
@@ -262,29 +264,26 @@
 	 * <li>Defines a payload parameter.</li>
 	 * </ul>
 	 *
-	 * @param annotation The annotation to test.
+	 * @param annotationType The annotation type to test.
 	 *
 	 * @return <code>true</code> if the annotation fulfills the above condtions, <code>false</code> otherwise.
 	 */
-	public boolean isConstraintAnnotation(Annotation annotation) {
-
-		Constraint constraint = annotation.annotationType()
-				.getAnnotation( Constraint.class );
+	public boolean isConstraintAnnotation(Class<? extends Annotation> annotationType) {
+		Constraint constraint = annotationType.getAnnotation( Constraint.class );
 		if ( constraint == null ) {
 			return false;
 		}
 
-		assertMessageParameterExists( annotation );
-		assertGroupsParameterExists( annotation );
-		assertPayloadParameterExists( annotation );
+		assertMessageParameterExists( annotationType );
+		assertGroupsParameterExists( annotationType );
+		assertPayloadParameterExists( annotationType );
+		assertNoParameterStartsWithValid( annotationType );
 
-		assertNoParameterStartsWithValid( annotation );
-
 		return true;
 	}
 
-	private void assertNoParameterStartsWithValid(Annotation annotation) {
-		final Method[] methods = ReflectionHelper.getMethods( annotation.annotationType() );
+	private void assertNoParameterStartsWithValid(Class<? extends Annotation> annotationType) {
+		final Method[] methods = ReflectionHelper.getMethods( annotationType );
 		for ( Method m : methods ) {
 			if ( m.getName().startsWith( "valid" ) ) {
 				String msg = "Parameters starting with 'valid' are not allowed in a constraint.";
@@ -293,64 +292,75 @@
 		}
 	}
 
-	private void assertPayloadParameterExists(Annotation annotation) {
+	private void assertPayloadParameterExists(Class<? extends Annotation> annotationType) {
 		try {
-			final Method method = ReflectionHelper.getMethod( annotation.annotationType(), "payload" );
+			final Method method = ReflectionHelper.getMethod( annotationType, "payload" );
 			if ( method == null ) {
-				String msg = annotation.annotationType().getName() + " contains Constraint annotation, but does " +
+				String msg = annotationType.getName() + " contains Constraint annotation, but does " +
 						"not contain a payload parameter.";
 				throw new ConstraintDefinitionException( msg );
 			}
 			Class<?>[] defaultPayload = ( Class<?>[] ) method.getDefaultValue();
 			if ( defaultPayload.length != 0 ) {
-				String msg = annotation.annotationType()
+				String msg = annotationType
 						.getName() + " contains Constraint annotation, but the payload " +
-						"paramter default value is not the empty array.";
+						"parameter default value is not the empty array.";
 				throw new ConstraintDefinitionException( msg );
 			}
 		}
 		catch ( ClassCastException e ) {
-			String msg = annotation.annotationType().getName() + " contains Constraint annotation, but the " +
+			String msg = annotationType.getName() + " contains Constraint annotation, but the " +
 					"payload parameter is of wrong type.";
 			throw new ConstraintDefinitionException( msg );
 		}
 	}
 
-	private void assertGroupsParameterExists(Annotation annotation) {
+	private void assertGroupsParameterExists(Class<? extends Annotation> annotationType) {
 		try {
-			final Method method = ReflectionHelper.getMethod( annotation.annotationType(), "groups" );
+			final Method method = ReflectionHelper.getMethod( annotationType, "groups" );
 			if ( method == null ) {
-				String msg = annotation.annotationType().getName() + " contains Constraint annotation, but does " +
+				String msg = annotationType.getName() + " contains Constraint annotation, but does " +
 						"not contain a groups parameter.";
 				throw new ConstraintDefinitionException( msg );
 			}
 			Class<?>[] defaultGroups = ( Class<?>[] ) method.getDefaultValue();
 			if ( defaultGroups.length != 0 ) {
-				String msg = annotation.annotationType()
+				String msg = annotationType
 						.getName() + " contains Constraint annotation, but the groups " +
-						"paramter default value is not the empty array.";
+						"parameter default value is not the empty array.";
 				throw new ConstraintDefinitionException( msg );
 			}
 		}
 		catch ( ClassCastException e ) {
-			String msg = annotation.annotationType().getName() + " contains Constraint annotation, but the " +
+			String msg = annotationType.getName() + " contains Constraint annotation, but the " +
 					"groups parameter is of wrong type.";
 			throw new ConstraintDefinitionException( msg );
 		}
 	}
 
-	private void assertMessageParameterExists(Annotation annotation) {
+	private void assertMessageParameterExists(Class<? extends Annotation> annotationType) {
 		try {
-			ReflectionHelper.getAnnotationParameter( annotation, "message", String.class );
+			final Method method = ReflectionHelper.getMethod( annotationType, "message" );
+			if ( method == null ) {
+				String msg = annotationType.getName() + " contains Constraint annotation, but does " +
+						"not contain a message parameter.";
+				throw new ConstraintDefinitionException( msg );
+			}
+			if ( method.getReturnType() != String.class ) {
+				String msg = annotationType.getName() + " contains Constraint annotation, but the message parameter " +
+						"is not of type java.lang.String.";
+				throw new ConstraintDefinitionException( msg );
+			}
 		}
-		catch ( Exception e ) {
-			String msg = annotation.annotationType().getName() + " contains Constraint annotation, but does " +
-					"not contain a message parameter.";
+		catch ( ClassCastException e ) {
+			String msg = annotationType.getName() + " contains Constraint annotation, but the " +
+					"groups parameter is of wrong type.";
 			throw new ConstraintDefinitionException( msg );
 		}
 	}
 
-	public <T extends Annotation> List<Class<? extends ConstraintValidator<T, ?>>> getConstraintValidatorDefinition(Class<T> annotationClass) {
+	public <T extends Annotation> List<Class<? extends ConstraintValidator<T, ?>>> getConstraintValidatorDefinition
+			(Class<T> annotationClass) {
 		if ( annotationClass == null ) {
 			throw new IllegalArgumentException( "Class cannot be null" );
 		}

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java	2010-06-23 14:55:58 UTC (rev 19799)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java	2010-06-23 16:18:16 UTC (rev 19800)
@@ -100,7 +100,7 @@
 
 	public String toString() {
 		StringBuilder result = new StringBuilder();
-		result.append( '@' ).append( annotationType().getName() ).append( '(' );
+		result.append( '@' ).append( annotationType.getName() ).append( '(' );
 		for ( String s : getRegisteredMethodsInAlphabeticalOrder() ) {
 			result.append( s ).append( '=' ).append( values.get( s ) ).append( ", " );
 		}



More information about the hibernate-commits mailing list