View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2013 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import static net.sf.oval.Validator.getCollectionFactory;
16  
17  import java.lang.reflect.Method;
18  import java.util.Map;
19  
20  import net.sf.oval.ConstraintTarget;
21  import net.sf.oval.Validator;
22  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
23  import net.sf.oval.context.OValContext;
24  import net.sf.oval.exception.InvalidConfigurationException;
25  import net.sf.oval.exception.ReflectionException;
26  import net.sf.oval.internal.util.ReflectionUtils;
27  
28  /**
29   * @author Sebastian Thomschke
30   */
31  public class ValidateWithMethodCheck extends AbstractAnnotationCheck<ValidateWithMethod>
32  {
33  	private static final long serialVersionUID = 1L;
34  
35  	private boolean ignoreIfNull;
36  	private String methodName;
37  	private Class< ? > parameterType;
38  
39  	/**
40  	 * {@inheritDoc}
41  	 */
42  	@Override
43  	public void configure(final ValidateWithMethod constraintAnnotation)
44  	{
45  		super.configure(constraintAnnotation);
46  		setMethodName(constraintAnnotation.methodName());
47  		setParameterType(constraintAnnotation.parameterType());
48  		setIgnoreIfNull(constraintAnnotation.ignoreIfNull());
49  	}
50  
51  	/**
52  	 * {@inheritDoc}
53  	 */
54  	@Override
55  	protected Map<String, String> createMessageVariables()
56  	{
57  		final Map<String, String> messageVariables = getCollectionFactory().createMap(4);
58  		messageVariables.put("ignoreIfNull", Boolean.toString(ignoreIfNull));
59  		messageVariables.put("methodName", methodName);
60  		messageVariables.put("parameterType", parameterType.getName());
61  		return messageVariables;
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	@Override
68  	protected ConstraintTarget[] getAppliesToDefault()
69  	{
70  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
71  	}
72  
73  	/**
74  	 * @return the methodName
75  	 */
76  	public String getMethodName()
77  	{
78  		return methodName;
79  	}
80  
81  	/**
82  	 * @return the parameterType
83  	 */
84  	public Class< ? > getParameterType()
85  	{
86  		return parameterType;
87  	}
88  
89  	/**
90  	 * @return the ignoreIfNull
91  	 */
92  	public boolean isIgnoreIfNull()
93  	{
94  		return ignoreIfNull;
95  	}
96  
97  	/**
98  	 * {@inheritDoc}
99  	 */
100 	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
101 			final Validator validator) throws ReflectionException
102 	{
103 		if (valueToValidate == null && ignoreIfNull) return true;
104 
105 		final Method method = ReflectionUtils.getMethodRecursive(validatedObject.getClass(), methodName, parameterType);
106 		if (method == null)
107 			throw new InvalidConfigurationException("Method " + validatedObject.getClass().getName() + "." + methodName + "("
108 					+ parameterType + ") not found. Is [" + parameterType + "] the correct value for [parameterType]?");
109 		//explicit cast to avoid: type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds boolean,java.lang.Object
110 		return (Boolean) ReflectionUtils.invokeMethod(method, validatedObject, valueToValidate);
111 	}
112 
113 	/**
114 	 * @param ignoreIfNull the ignoreIfNull to set
115 	 */
116 	public void setIgnoreIfNull(final boolean ignoreIfNull)
117 	{
118 		this.ignoreIfNull = ignoreIfNull;
119 		requireMessageVariablesRecreation();
120 	}
121 
122 	/**
123 	 * @param methodName the methodName to set
124 	 */
125 	public void setMethodName(final String methodName)
126 	{
127 		this.methodName = methodName;
128 		requireMessageVariablesRecreation();
129 	}
130 
131 	/**
132 	 * @param parameterType the parameterType to set
133 	 */
134 	public void setParameterType(final Class< ? > parameterType)
135 	{
136 		this.parameterType = parameterType;
137 		requireMessageVariablesRecreation();
138 	}
139 }