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.guard;
014    
015    import java.lang.reflect.Constructor;
016    import java.lang.reflect.Method;
017    
018    import net.sf.oval.exception.ReflectionException;
019    
020    import com.thoughtworks.paranamer.BytecodeReadingParanamer;
021    import com.thoughtworks.paranamer.CachingParanamer;
022    import com.thoughtworks.paranamer.Paranamer;
023    
024    /**
025     * This implementation that uses com.thoughtworks.paranamer.Paranamer (http://paranamer.codehaus.org/)
026     * to determine the names of parameter names as a fallback the results of a 
027     * ParameterNameResolverEnumerationImpl are returned
028     *  
029     * @author Sebastian Thomschke
030     */
031    public class ParameterNameResolverParanamerImpl implements ParameterNameResolver
032    {
033            private static final ParameterNameResolver FALLBACK = new ParameterNameResolverEnumerationImpl();
034    
035            private final Paranamer paranamer;
036    
037            public ParameterNameResolverParanamerImpl()
038            {
039                    paranamer = new CachingParanamer(new BytecodeReadingParanamer());
040            }
041    
042            public ParameterNameResolverParanamerImpl(final Paranamer paranamer)
043            {
044                    this.paranamer = paranamer;
045            }
046    
047            /**
048             * {@inheritDoc}
049             */
050            public String[] getParameterNames(final Constructor< ? > constructor) throws ReflectionException
051            {
052                    return FALLBACK.getParameterNames(constructor);
053            }
054    
055            /**
056             * {@inheritDoc}
057             */
058            public String[] getParameterNames(final Method method) throws ReflectionException
059            {
060                    final String[] parameterNames = paranamer.lookupParameterNames(method);
061    
062                    if (parameterNames == null) return FALLBACK.getParameterNames(method);
063    
064                    return parameterNames;
065            }
066    }