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.Constructor;
16  
17  import net.sf.oval.Validator;
18  import net.sf.oval.internal.util.SerializableConstructor;
19  import net.sf.oval.internal.util.StringUtils;
20  
21  /**
22   * @author Sebastian Thomschke
23   */
24  public class ConstructorParameterContext extends OValContext
25  {
26  	private static final long serialVersionUID = 1L;
27  
28  	private final SerializableConstructor constructor;
29  	private final int parameterIndex;
30  	private final String parameterName;
31  
32  	/**
33  	 * 
34  	 * @param constructor
35  	 * @param parameterIndex
36  	 */
37  	public ConstructorParameterContext(final Constructor< ? > constructor, final int parameterIndex,
38  			final String parameterName)
39  	{
40  		this.constructor = SerializableConstructor.getInstance(constructor);
41  		this.parameterIndex = parameterIndex;
42  		this.parameterName = parameterName;
43  		this.compileTimeType = constructor.getParameterTypes()[parameterIndex];
44  	}
45  
46  	/**
47  	 * @return Returns the constructor.
48  	 */
49  	public Constructor< ? > getConstructor()
50  	{
51  		return constructor.getConstructor();
52  	}
53  
54  	/**
55  	 * @return Returns the parameterIndex.
56  	 */
57  	public int getParameterIndex()
58  	{
59  		return parameterIndex;
60  	}
61  
62  	/**
63  	 * @return the parameterName
64  	 */
65  	public String getParameterName()
66  	{
67  		return parameterName;
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 */
73  	@Override
74  	public String toString()
75  	{
76  		return constructor.getDeclaringClass().getName()
77  				+ "("
78  				+ StringUtils.implode(constructor.getParameterTypes(), ",")
79  				+ ") "
80  				+ Validator.getMessageResolver()
81  						.getMessage("net.sf.oval.context.ConstructorParameterContext.parameter") + " " + parameterIndex
82  				+ (parameterName == null || parameterName.length() == 0 ? "" : " (" + parameterName + ")");
83  	}
84  }