You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by si...@apache.org on 2010/07/02 10:06:53 UTC
svn commit: r959908 -
/incubator/bval/trunk/bval-guice/src/main/java/org/apache/bval/guice/ValidateMethodInterceptor.java
Author: simonetripodi
Date: Fri Jul 2 08:06:52 2010
New Revision: 959908
URL: http://svn.apache.org/viewvc?rev=959908&view=rev
Log:
added policy to re-throw the ConstraintViolationException with a custom exception wrapper defined in the @Validate annotation
Modified:
incubator/bval/trunk/bval-guice/src/main/java/org/apache/bval/guice/ValidateMethodInterceptor.java
Modified: incubator/bval/trunk/bval-guice/src/main/java/org/apache/bval/guice/ValidateMethodInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-guice/src/main/java/org/apache/bval/guice/ValidateMethodInterceptor.java?rev=959908&r1=959907&r2=959908&view=diff
==============================================================================
--- incubator/bval/trunk/bval-guice/src/main/java/org/apache/bval/guice/ValidateMethodInterceptor.java (original)
+++ incubator/bval/trunk/bval-guice/src/main/java/org/apache/bval/guice/ValidateMethodInterceptor.java Fri Jul 2 08:06:52 2010
@@ -15,6 +15,7 @@
*/
package org.apache.bval.guice;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
@@ -71,10 +72,10 @@ public final class ValidateMethodInterce
groups));
if (!constraintViolations.isEmpty()) {
- throw new ConstraintViolationException("Validation error when calling method '"
+ throw getException(new ConstraintViolationException("Validation error when calling method '"
+ method
+ "' with arguments "
- + Arrays.deepToString(arguments), constraintViolations);
+ + Arrays.deepToString(arguments), constraintViolations), validate.rethrowExceptionsAs());
}
Object returnedValue = invocation.proceed();
@@ -82,14 +83,36 @@ public final class ValidateMethodInterce
if (validate.validateReturnedValue()) {
constraintViolations.addAll(methodValidator.validateReturnedValue(clazz, method, returnedValue, groups));
if (!constraintViolations.isEmpty()) {
- throw new ConstraintViolationException("Method '"
+ throw getException(new ConstraintViolationException("Method '"
+ method
+ "' returned a not valid value "
- + returnedValue, constraintViolations);
+ + returnedValue, constraintViolations), validate.rethrowExceptionsAs());
}
}
return returnedValue;
}
+ private static Throwable getException(ConstraintViolationException exception,
+ Class<? extends Throwable> exceptionWrapperClass) {
+ // check the thrown exception is of same re-throw type
+ if (exceptionWrapperClass == ConstraintViolationException.class) {
+ return exception;
+ }
+
+ // re-throw the exception as new exception
+ Throwable rethrowEx = null;
+
+ try {
+ Constructor<? extends Throwable> constructor = exceptionWrapperClass.getConstructor(Throwable.class);
+ rethrowEx = constructor.newInstance(exception);
+ } catch (Exception e) {
+ rethrowEx = new RuntimeException("Impossible to re-throw '"
+ + exceptionWrapperClass
+ + "', it needs the constructor with <Throwable> argument.", e);
+ }
+
+ return rethrowEx;
+ }
+
}