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.guard;
14  
15  import java.lang.reflect.AccessibleObject;
16  import java.lang.reflect.Constructor;
17  import java.lang.reflect.Method;
18  import java.util.WeakHashMap;
19  
20  import net.sf.oval.exception.ReflectionException;
21  
22  /**
23   * This implementation determines the names of constructor and method parameters by simply enumerating them based on there index:
24   * arg0,arg1,arg2,..
25   * @author Sebastian Thomschke
26   */
27  public class ParameterNameResolverEnumerationImpl implements ParameterNameResolver
28  {
29  	private final WeakHashMap<AccessibleObject, String[]> parameterNamesCache = new WeakHashMap<AccessibleObject, String[]>();
30  
31  	/**
32  	 * {@inheritDoc}
33  	 */
34  	public String[] getParameterNames(final Constructor< ? > constructor) throws ReflectionException
35  	{
36  		/*
37  		 * intentionally the following code is not synchronized
38  		 */
39  		String[] parameterNames = parameterNamesCache.get(constructor);
40  		if (parameterNames == null)
41  		{
42  			final int parameterCount = constructor.getParameterTypes().length;
43  			parameterNames = new String[parameterCount];
44  			for (int i = 0; i < parameterCount; i++)
45  			{
46  				parameterNames[i] = "arg" + i;
47  			}
48  			parameterNamesCache.put(constructor, parameterNames);
49  		}
50  		return parameterNames;
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	public String[] getParameterNames(final Method method) throws ReflectionException
57  	{
58  		/*
59  		 * intentionally the following code is not synchronized
60  		 */
61  		String[] parameterNames = parameterNamesCache.get(method);
62  		if (parameterNames == null)
63  		{
64  			final int parameterCount = method.getParameterTypes().length;
65  			parameterNames = new String[parameterCount];
66  			for (int i = 0; i < parameterCount; i++)
67  			{
68  				parameterNames[i] = "arg" + i;
69  			}
70  			parameterNamesCache.put(method, parameterNames);
71  		}
72  		return parameterNames;
73  	}
74  }