View Javadoc

1   package org.springmodules.validation.bean.rule;
2   
3   import java.lang.reflect.InvocationTargetException;
4   import java.lang.reflect.Method;
5   
6   import org.springmodules.validation.util.condition.AbstractCondition;
7   import org.springmodules.validation.util.condition.Condition;
8   import org.springmodules.validation.util.condition.ConditionException;
9   
10  /**
11   * @author Uri Boness
12   */
13  public class ValidationMethodValidationRule extends AbstractValidationRule {
14  
15      private MethodCondition condition;
16  
17      public ValidationMethodValidationRule(Method method) {
18          super(method.getName() + "()");
19          condition = new MethodCondition(method);
20      }
21  
22      public Condition getCondition() {
23          return condition;
24      }
25  
26  
27      private class MethodCondition extends AbstractCondition {
28  
29          private Method method;
30  
31          public MethodCondition(Method method) {
32              this.method = method;
33          }
34  
35          public boolean doCheck(Object object) {
36              try {
37                  boolean originalAccessiblity = method.isAccessible();
38                  method.setAccessible(true);
39                  Boolean valid = (Boolean) method.invoke(object, new Object[0]);
40                  method.setAccessible(originalAccessiblity);
41                  return valid.booleanValue();
42              } catch (IllegalAccessException iae) {
43                  throw new ConditionException("Could not validate object using validation method '" +
44                      method.getName() + "'", iae);
45              } catch (InvocationTargetException ite) {
46                  throw new ConditionException("Could not validate object using validation method '" +
47                      method.getName() + "'", ite);
48              }
49          }
50      }
51  
52  }