001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.context;
014    
015    import java.lang.reflect.Method;
016    
017    import net.sf.oval.Validator;
018    import net.sf.oval.internal.util.SerializableMethod;
019    import net.sf.oval.internal.util.StringUtils;
020    
021    /**
022     * @author Sebastian Thomschke
023     */
024    public class MethodParameterContext extends OValContext
025    {
026            private static final long serialVersionUID = 1L;
027    
028            private final SerializableMethod method;
029            private final int parameterIndex;
030            private final String parameterName;
031    
032            public MethodParameterContext(final Method method, final int parameterIndex, final String parameterName)
033            {
034                    this.method = SerializableMethod.getInstance(method);
035                    this.parameterIndex = parameterIndex;
036                    this.parameterName = parameterName == null ? "param" + parameterIndex : parameterName;
037                    this.compileTimeType = method.getParameterTypes()[parameterIndex];
038            }
039    
040            /**
041             * @return Returns the method.
042             */
043            public Method getMethod()
044            {
045                    return method.getMethod();
046            }
047    
048            /**
049             * @return Returns the parameterIndex.
050             */
051            public int getParameterIndex()
052            {
053                    return parameterIndex;
054            }
055    
056            /**
057             * @return the parameterName
058             */
059            public String getParameterName()
060            {
061                    return parameterName;
062            }
063    
064            /**
065             * {@inheritDoc}
066             */
067            @Override
068            public String toString()
069            {
070                    return method.getDeclaringClass().getName() + "." + method.getName() + "("
071                                    + StringUtils.implode(method.getParameterTypes(), ",") + ") "
072                                    + Validator.getMessageResolver().getMessage("net.sf.oval.context.MethodParameterContext.parameter")
073                                    + " " + parameterIndex
074                                    + (parameterName == null || parameterName.length() == 0 ? "" : " (" + parameterName + ")");
075            }
076    }