[hibernate-commits] Hibernate SVN: r19565 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validator/util and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu May 20 06:29:40 EDT 2010


Author: hardy.ferentschik
Date: 2010-05-20 06:29:39 -0400 (Thu, 20 May 2010)
New Revision: 19565

Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
   validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java
Log:
HV-274 Some error handling

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java	2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintDefinition.java	2010-05-20 10:29:39 UTC (rev 19565)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
 /*
  * JBoss, Home of Professional Open Source
  * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -25,6 +25,7 @@
 import javax.validation.ValidationException;
 
 import org.hibernate.validator.util.NewInstance;
+import org.hibernate.validator.util.ReflectionHelper;
 
 /**
  * @author Hardy Ferentschik
@@ -50,9 +51,17 @@
 			throw new ValidationException( "ConstraintMapping cannot be null" );
 		}
 
-		if ( ( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) )
-				&& ( property == null || property.length() == 0 ) ) {
-			throw new ValidationException( "A property level constraint cannot have a null or empty property name" );
+		if ( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) ) {
+			if ( property == null || property.length() == 0 ) {
+				throw new ValidationException( "A property level constraint cannot have a null or empty property name" );
+			}
+
+			if ( !ReflectionHelper.propertyExists( beanType, property, elementType ) ) {
+				throw new ValidationException(
+						"The class " + beanType + " does not have a property '"
+								+ property + "' with access " + elementType
+				);
+			}
 		}
 
 		this.beanType = beanType;

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java	2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/cfg/ConstraintMapping.java	2010-05-20 10:29:39 UTC (rev 19565)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
 /*
  * JBoss, Home of Professional Open Source
  * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -22,6 +22,8 @@
 import java.util.List;
 import java.util.Map;
 
+import org.hibernate.validator.util.ReflectionHelper;
+
 /**
  * @author Hardy Ferentschik
  */
@@ -38,6 +40,7 @@
 
 	protected void addConstraintConfig(ConstraintDefinition<?> definition) {
 		Class<?> beanClass = definition.getBeanType();
+
 		if ( configData.containsKey( beanClass ) ) {
 			configData.get( beanClass ).add( definition );
 		}

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java	2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/ReflectionHelper.java	2010-05-20 10:29:39 UTC (rev 19565)
@@ -19,6 +19,7 @@
 
 import java.beans.Introspector;
 import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
@@ -51,8 +52,6 @@
 	private ReflectionHelper() {
 	}
 
-	//run client in privileged block
-
 	@SuppressWarnings("unchecked")
 	public static <T> T getAnnotationParameter(Annotation annotation, String parameterName, Class<T> type) {
 		try {
@@ -112,6 +111,56 @@
 	}
 
 	/**
+	 * Checks whether the property with the specified name and type exists on the given class.
+	 *
+	 * @param clazz The class to check for the property. Cannot be {@code null}.
+	 * @param property The property name without 'is', 'get' or 'has'. Cannot be {@code null} or empty.
+	 * @param elementType The element type. Either {@code ElementType.FIELD} or {@code ElementType METHOD}.
+	 *
+	 * @return {@code true} is the property and can be access via the specified type, {@code false} otherwise.
+	 */
+	public static boolean propertyExists(Class<?> clazz, String property, ElementType elementType) {
+		if ( clazz == null ) {
+			throw new IllegalArgumentException( "The class cannot be null" );
+		}
+
+		if ( property == null || property.length() == 0 ) {
+			throw new IllegalArgumentException( "Property name cannot be null or empty" );
+		}
+
+		if ( !( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) ) ) {
+			throw new IllegalArgumentException( "Element type has to be FIELD or METHOD" );
+		}
+
+		boolean exists = false;
+		if ( ElementType.FIELD.equals( elementType ) ) {
+			try {
+				clazz.getDeclaredField( property );
+				exists = true;
+			}
+			catch ( NoSuchFieldException e ) {
+				exists = false;
+			}
+		}
+		else {
+			String methodName = property.substring( 0, 1 ).toUpperCase() + property.substring( 1 );
+			String[] prefixes = {"is", "get", "has"};
+			for(String prefix : prefixes) {
+				try {
+					clazz.getDeclaredMethod( prefix + methodName );
+					exists = true;
+					break;
+				} catch (NoSuchMethodException e) {
+
+				}
+			}
+
+		}
+		return exists;
+	}
+
+
+	/**
 	 * Returns the type of the field of return type of a method.
 	 *
 	 * @param member the member for which to get the type.

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java	2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/cfg/ConstraintMappingTest.java	2010-05-20 10:29:39 UTC (rev 19565)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
 /*
  * JBoss, Home of Professional Open Source
  * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -19,9 +19,11 @@
 
 import java.util.Set;
 import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
 import javax.validation.Validator;
 import javax.validation.ValidatorFactory;
 
+import org.slf4j.Logger;
 import org.testng.annotations.Test;
 
 import org.hibernate.validator.HibernateValidator;
@@ -30,17 +32,21 @@
 import org.hibernate.validator.cfg.MinDefinition;
 import org.hibernate.validator.cfg.NotNullDefinition;
 import org.hibernate.validator.test.util.TestUtil;
+import org.hibernate.validator.util.LoggerFactory;
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
 import static org.hibernate.validator.test.util.TestUtil.assertConstraintViolation;
 import static org.hibernate.validator.test.util.TestUtil.assertNumberOfViolations;
 import static org.testng.Assert.assertTrue;
+import static org.testng.FileAssert.fail;
 
 /**
  * @author Hardy Ferentschik
  */
 public class ConstraintMappingTest {
+	private static final Logger log = LoggerFactory.make();
+
 	@Test
 	public void testConstraintMapping() {
 		ConstraintMapping mapping = new ConstraintMapping();
@@ -83,24 +89,21 @@
 		assertConstraintViolation( violations.iterator().next(), "may not be null" );
 	}
 
-//	@Test
-//	public void testSingleConstraintWrongAccessType() {
-//		HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
-//
-//		ConstraintMapping mapping = new ConstraintMapping();
-//		mapping.type( Marathon.class )
-//				.property( "numberOfRunners", METHOD )
-//				.constraint( NotNullDefinition.class );
-//
-//		config.addMapping( mapping );
-//
-//		ValidatorFactory factory = config.buildValidatorFactory();
-//		Validator validator = factory.getValidator();
-//
-//		Set<ConstraintViolation<Marathon>> violations = validator.validate( new Marathon() );
-//		assertNumberOfViolations( violations, 1 );
-//		assertConstraintViolation( violations.iterator().next(), "may not be null" );
-//	}
+	@Test
+	public void testSingleConstraintWrongAccessType() {
+		HibernateValidatorConfiguration config = TestUtil.getConfiguration( HibernateValidator.class );
+
+		ConstraintMapping mapping = new ConstraintMapping();
+		try {
+			mapping.type( Marathon.class )
+					.property( "numberOfRunners", METHOD )
+					.constraint( NotNullDefinition.class );
+			fail();
+		}
+		catch ( ValidationException e ) {
+			log.debug( e.toString() );
+		}
+	}
 }
 
 

Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java	2010-05-20 05:45:33 UTC (rev 19564)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/util/ReflectionHelperTest.java	2010-05-20 10:29:39 UTC (rev 19565)
@@ -36,6 +36,9 @@
 
 import org.hibernate.validator.util.ReflectionHelper;
 
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNull;
@@ -172,9 +175,33 @@
 		}
 	}
 
+	@Test
+	public void testPropertyExists() {
+		assertTrue( ReflectionHelper.propertyExists( Foo.class, "foo", FIELD ) );
+		assertFalse( ReflectionHelper.propertyExists( Foo.class, "foo", METHOD ) );
+		assertFalse( ReflectionHelper.propertyExists( Foo.class, "bar", FIELD ) );
+		assertTrue( ReflectionHelper.propertyExists( Foo.class, "bar", METHOD ) );
+
+		try {
+			assertTrue( ReflectionHelper.propertyExists( Foo.class, "bar", TYPE ) );
+			fail();
+		}
+		catch ( IllegalArgumentException e ) {
+			// success
+		}
+	}
+
 	public class TestTypes {
 		public List<String> stringList;
 		public Map<String, Object> objectMap;
 		public String[] stringArray;
 	}
+
+	public class Foo {
+		String foo;
+
+		public String getBar() {
+			return "bar";
+		}
+	}
 }



More information about the hibernate-commits mailing list