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.Constructor;
16  import java.lang.reflect.Method;
17  
18  import net.sf.oval.exception.ReflectionException;
19  
20  import com.thoughtworks.paranamer.BytecodeReadingParanamer;
21  import com.thoughtworks.paranamer.CachingParanamer;
22  import com.thoughtworks.paranamer.Paranamer;
23  
24  /**
25   * This implementation that uses com.thoughtworks.paranamer.Paranamer (http://paranamer.codehaus.org/)
26   * to determine the names of parameter names as a fallback the results of a 
27   * ParameterNameResolverEnumerationImpl are returned
28   *  
29   * @author Sebastian Thomschke
30   */
31  public class ParameterNameResolverParanamerImpl implements ParameterNameResolver
32  {
33  	private static final ParameterNameResolver FALLBACK = new ParameterNameResolverEnumerationImpl();
34  
35  	private final Paranamer paranamer;
36  
37  	public ParameterNameResolverParanamerImpl()
38  	{
39  		paranamer = new CachingParanamer(new BytecodeReadingParanamer());
40  	}
41  
42  	public ParameterNameResolverParanamerImpl(final Paranamer paranamer)
43  	{
44  		this.paranamer = paranamer;
45  	}
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	public String[] getParameterNames(final Constructor< ? > constructor) throws ReflectionException
51  	{
52  		return FALLBACK.getParameterNames(constructor);
53  	}
54  
55  	/**
56  	 * {@inheritDoc}
57  	 */
58  	public String[] getParameterNames(final Method method) throws ReflectionException
59  	{
60  		final String[] parameterNames = paranamer.lookupParameterNames(method);
61  
62  		if (parameterNames == null) return FALLBACK.getParameterNames(method);
63  
64  		return parameterNames;
65  	}
66  }