01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2006-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.guard;
13:
14: import java.lang.reflect.AccessibleObject;
15: import java.lang.reflect.Constructor;
16: import java.lang.reflect.Method;
17: import java.util.WeakHashMap;
18:
19: import net.sf.oval.exception.ReflectionException;
20:
21: /**
22: * This implementation determines the names of constructor and method parameters by simply enumerating them based on there index:
23: * arg0,arg1,arg2,..
24: * @author Sebastian Thomschke
25: */
26: public class ParameterNameResolverEnumerationImpl implements
27: ParameterNameResolver {
28: private final WeakHashMap<AccessibleObject, String[]> parameterNamesCache = new WeakHashMap<AccessibleObject, String[]>();
29:
30: public String[] getParameterNames(final Method method)
31: throws ReflectionException {
32: /*
33: * intentionally the following code is not synchronized
34: */
35: String[] parameterNames = parameterNamesCache.get(method);
36: if (parameterNames == null) {
37: final int parameterCount = method.getParameterTypes().length;
38: parameterNames = new String[parameterCount];
39: for (int i = 0; i < parameterCount; i++) {
40: parameterNames[i] = "arg" + i;
41: }
42: parameterNamesCache.put(method, parameterNames);
43: }
44: return parameterNames;
45: }
46:
47: public String[] getParameterNames(final Constructor constructor)
48: throws ReflectionException {
49: /*
50: * intentionally the following code is not synchronized
51: */
52: String[] parameterNames = parameterNamesCache.get(constructor);
53: if (parameterNames == null) {
54: final int parameterCount = constructor.getParameterTypes().length;
55: parameterNames = new String[parameterCount];
56: for (int i = 0; i < parameterCount; i++) {
57: parameterNames[i] = "arg" + i;
58: }
59: parameterNamesCache.put(constructor, parameterNames);
60: }
61: return parameterNames;
62: }
63: }
|