[hibernate-commits] Hibernate SVN: r20096 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Sun Aug 1 03:54:29 EDT 2010


Author: gunnar.morling
Date: 2010-08-01 03:54:28 -0400 (Sun, 01 Aug 2010)
New Revision: 20096

Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationDescriptor.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java
Log:
HV-355: Improved error logging in case of unknown parameters given by annotation descriptor

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationDescriptor.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationDescriptor.java	2010-08-01 07:39:43 UTC (rev 20095)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationDescriptor.java	2010-08-01 07:54:28 UTC (rev 20096)
@@ -32,6 +32,7 @@
  * @author Paolo Perrotta
  * @author Davide Marchignoli
  * @author Hardy Ferentschik
+ * @author Gunnar Morling
  */
 public class AnnotationDescriptor<T extends Annotation> {
 
@@ -41,36 +42,36 @@
 
 	/**
 	 * Returns a new descriptor for the given annotation type.
-	 * 
+	 *
 	 * @param <S> The type of the annotation.
 	 * @param annotationType The annotation's class.
-	 * 
+	 *
 	 * @return A new descriptor for the given annotation type.
 	 */
 	public static <S extends Annotation> AnnotationDescriptor<S> getInstance(Class<S> annotationType) {
-		return new AnnotationDescriptor<S>(annotationType);
+		return new AnnotationDescriptor<S>( annotationType );
 	}
-	
+
 	/**
 	 * Returns a new descriptor for the given annotation type.
-	 * 
+	 *
 	 * @param <S> The type of the annotation.
 	 * @param annotationType The annotation's class.
 	 * @param elements A map with attribute values for the annotation to be created.
-	 * 
+	 *
 	 * @return A new descriptor for the given annotation type.
 	 */
 	public static <S extends Annotation> AnnotationDescriptor<S> getInstance(Class<S> annotationType, Map<String, Object> elements) {
-		return new AnnotationDescriptor<S>(annotationType, elements);
+		return new AnnotationDescriptor<S>( annotationType, elements );
 	}
-	
+
 	public AnnotationDescriptor(Class<T> annotationType) {
 		this.type = annotationType;
 	}
 
-    public AnnotationDescriptor(Class<T> annotationType, Map<String, Object> elements) {
+	public AnnotationDescriptor(Class<T> annotationType, Map<String, Object> elements) {
 		this.type = annotationType;
-		for (Map.Entry<String, Object> entry : elements.entrySet()) {
+		for ( Map.Entry<String, Object> entry : elements.entrySet() ) {
 			this.elements.put( entry.getKey(), entry.getValue() );
 		}
 	}
@@ -91,6 +92,16 @@
 		return elements.size();
 	}
 
+	/**
+	 * Returns a map with the elements contained in this descriptor keyed by name. This map is a copy
+	 * of the internally used map, so it can safely be modified without altering this descriptor.
+	 *
+	 * @return A map with this descriptor's elements.
+	 */
+	public Map<String, Object> getElements() {
+		return new HashMap<String, Object>( elements );
+	}
+
 	public Class<T> type() {
 		return type;
 	}

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-08-01 07:39:43 UTC (rev 20095)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/annotationfactory/AnnotationProxy.java	2010-08-01 07:54:28 UTC (rev 20096)
@@ -23,6 +23,7 @@
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
@@ -60,12 +61,12 @@
 	private final Map<String, Object> values;
 
 
-	public AnnotationProxy(AnnotationDescriptor descriptor) {
+	public AnnotationProxy(AnnotationDescriptor<?> descriptor) {
 		this.annotationType = descriptor.type();
 		values = getAnnotationValues( descriptor );
 	}
 
-	private Map<String, Object> getAnnotationValues(AnnotationDescriptor descriptor) {
+	private Map<String, Object> getAnnotationValues(AnnotationDescriptor<?> descriptor) {
 		Map<String, Object> result = new HashMap<String, Object>();
 		int processedValuesFromDescriptor = 0;
 		final Method[] declaredMethods = ReflectionHelper.getMethods( annotationType );
@@ -82,7 +83,11 @@
 			}
 		}
 		if ( processedValuesFromDescriptor != descriptor.numberOfElements() ) {
-			throw new RuntimeException( "Trying to instantiate " + annotationType + " with unknown parameters." );
+
+			Set<String> unknownParameters = descriptor.getElements().keySet();
+			unknownParameters.removeAll( result.keySet() );
+
+			throw new RuntimeException( "Trying to instantiate " + annotationType + " with unknown parameter(s): " + unknownParameters );
 		}
 		return result;
 	}



More information about the hibernate-commits mailing list