View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 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.context;
14  
15  import java.lang.reflect.Method;
16  
17  import net.sf.oval.Validator;
18  import net.sf.oval.internal.util.SerializableMethod;
19  import net.sf.oval.internal.util.StringUtils;
20  
21  /**
22   * @author Sebastian Thomschke
23   */
24  public class MethodParameterContext extends OValContext
25  {
26  	private static final long serialVersionUID = 1L;
27  
28  	private final SerializableMethod method;
29  	private final int parameterIndex;
30  	private final String parameterName;
31  
32  	public MethodParameterContext(final Method method, final int parameterIndex, final String parameterName)
33  	{
34  		this.method = SerializableMethod.getInstance(method);
35  		this.parameterIndex = parameterIndex;
36  		this.parameterName = parameterName == null ? "param" + parameterIndex : parameterName;
37  		this.compileTimeType = method.getParameterTypes()[parameterIndex];
38  	}
39  
40  	/**
41  	 * @return Returns the method.
42  	 */
43  	public Method getMethod()
44  	{
45  		return method.getMethod();
46  	}
47  
48  	/**
49  	 * @return Returns the parameterIndex.
50  	 */
51  	public int getParameterIndex()
52  	{
53  		return parameterIndex;
54  	}
55  
56  	/**
57  	 * @return the parameterName
58  	 */
59  	public String getParameterName()
60  	{
61  		return parameterName;
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	@Override
68  	public String toString()
69  	{
70  		return method.getDeclaringClass().getName() + "." + method.getName() + "("
71  				+ StringUtils.implode(method.getParameterTypes(), ",") + ") "
72  				+ Validator.getMessageResolver().getMessage("net.sf.oval.context.MethodParameterContext.parameter")
73  				+ " " + parameterIndex
74  				+ (parameterName == null || parameterName.length() == 0 ? "" : " (" + parameterName + ")");
75  	}
76  }