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.AccessibleObject;
016    import java.lang.reflect.Constructor;
017    import java.lang.reflect.Method;
018    import java.util.WeakHashMap;
019    
020    import net.sf.oval.exception.ReflectionException;
021    
022    /**
023     * This implementation determines the names of constructor and method parameters by simply enumerating them based on there index:
024     * arg0,arg1,arg2,..
025     * @author Sebastian Thomschke
026     */
027    public class ParameterNameResolverEnumerationImpl implements ParameterNameResolver
028    {
029            private final WeakHashMap<AccessibleObject, String[]> parameterNamesCache = new WeakHashMap<AccessibleObject, String[]>();
030    
031            /**
032             * {@inheritDoc}
033             */
034            public String[] getParameterNames(final Constructor< ? > constructor) throws ReflectionException
035            {
036                    /*
037                     * intentionally the following code is not synchronized
038                     */
039                    String[] parameterNames = parameterNamesCache.get(constructor);
040                    if (parameterNames == null)
041                    {
042                            final int parameterCount = constructor.getParameterTypes().length;
043                            parameterNames = new String[parameterCount];
044                            for (int i = 0; i < parameterCount; i++)
045                            {
046                                    parameterNames[i] = "arg" + i;
047                            }
048                            parameterNamesCache.put(constructor, parameterNames);
049                    }
050                    return parameterNames;
051            }
052    
053            /**
054             * {@inheritDoc}
055             */
056            public String[] getParameterNames(final Method method) throws ReflectionException
057            {
058                    /*
059                     * intentionally the following code is not synchronized
060                     */
061                    String[] parameterNames = parameterNamesCache.get(method);
062                    if (parameterNames == null)
063                    {
064                            final int parameterCount = method.getParameterTypes().length;
065                            parameterNames = new String[parameterCount];
066                            for (int i = 0; i < parameterCount; i++)
067                            {
068                                    parameterNames[i] = "arg" + i;
069                            }
070                            parameterNamesCache.put(method, parameterNames);
071                    }
072                    return parameterNames;
073            }
074    }