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.Constructor; 016 017 import net.sf.oval.Validator; 018 import net.sf.oval.internal.util.SerializableConstructor; 019 import net.sf.oval.internal.util.StringUtils; 020 021 /** 022 * @author Sebastian Thomschke 023 */ 024 public class ConstructorParameterContext extends OValContext 025 { 026 private static final long serialVersionUID = 1L; 027 028 private final SerializableConstructor constructor; 029 private final int parameterIndex; 030 private final String parameterName; 031 032 /** 033 * 034 * @param constructor 035 * @param parameterIndex 036 */ 037 public ConstructorParameterContext(final Constructor< ? > constructor, final int parameterIndex, 038 final String parameterName) 039 { 040 this.constructor = SerializableConstructor.getInstance(constructor); 041 this.parameterIndex = parameterIndex; 042 this.parameterName = parameterName; 043 this.compileTimeType = constructor.getParameterTypes()[parameterIndex]; 044 } 045 046 /** 047 * @return Returns the constructor. 048 */ 049 public Constructor< ? > getConstructor() 050 { 051 return constructor.getConstructor(); 052 } 053 054 /** 055 * @return Returns the parameterIndex. 056 */ 057 public int getParameterIndex() 058 { 059 return parameterIndex; 060 } 061 062 /** 063 * @return the parameterName 064 */ 065 public String getParameterName() 066 { 067 return parameterName; 068 } 069 070 /** 071 * {@inheritDoc} 072 */ 073 @Override 074 public String toString() 075 { 076 return constructor.getDeclaringClass().getName() 077 + "(" 078 + StringUtils.implode(constructor.getParameterTypes(), ",") 079 + ") " 080 + Validator.getMessageResolver() 081 .getMessage("net.sf.oval.context.ConstructorParameterContext.parameter") + " " + parameterIndex 082 + (parameterName == null || parameterName.length() == 0 ? "" : " (" + parameterName + ")"); 083 } 084 }