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

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed May 19 11:50:05 EDT 2010


Author: hardy.ferentschik
Date: 2010-05-19 11:50:04 -0400 (Wed, 19 May 2010)
New Revision: 19556

Modified:
   validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java
Log:
HV-274 changed the way new instances are created. Now you can also pass a vararg argument of constructor parameters and the matching constructor will be located and executed

Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java	2010-05-19 15:48:49 UTC (rev 19555)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/util/NewInstance.java	2010-05-19 15:50:04 UTC (rev 19556)
@@ -17,28 +17,41 @@
 */
 package org.hibernate.validator.util;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.security.PrivilegedAction;
 import javax.validation.ValidationException;
 
 /**
+ * Execute instance creation as privileged action.
+ *
  * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
  */
 public class NewInstance<T> implements PrivilegedAction<T> {
 	private final Class<T> clazz;
 	private final String message;
+	private final Class<?>[] parameterTypes;
+	private final Object[] initArgs;
 
-	public static <T> NewInstance<T> action(Class<T> clazz, String message) {
-		return new NewInstance<T>( clazz, message );
+	public static <T> NewInstance<T> action(Class<T> clazz, String message, Object... initArgs) {
+		return new NewInstance<T>( clazz, message, initArgs );
 	}
 
-	private NewInstance(Class<T> clazz, String message) {
+	private NewInstance(Class<T> clazz, String message, Object... initArgs) {
 		this.clazz = clazz;
 		this.message = message;
+		this.initArgs = initArgs;
+		this.parameterTypes = new Class<?>[initArgs.length];
+		for ( int i = 0; i < initArgs.length; i++ ) {
+			this.parameterTypes[i] = initArgs[i].getClass();
+		}
 	}
 
 	public T run() {
 		try {
-			return clazz.newInstance();
+			Constructor<T> constructor = clazz.getConstructor( parameterTypes );
+			return constructor.newInstance( initArgs );
 		}
 		catch ( InstantiationException e ) {
 			throw new ValidationException( "Unable to instantiate " + message + ": " + clazz, e );
@@ -46,6 +59,12 @@
 		catch ( IllegalAccessException e ) {
 			throw new ValidationException( "Unable to instantiate " + clazz, e );
 		}
+		catch ( NoSuchMethodException e ) {
+			throw new ValidationException( "Unable to instantiate " + clazz, e );
+		}
+		catch ( InvocationTargetException e ) {
+			throw new ValidationException( "Unable to instantiate " + clazz, e );
+		}
 		catch ( RuntimeException e ) {
 			throw new ValidationException( "Unable to instantiate " + clazz, e );
 		}



More information about the hibernate-commits mailing list