001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2006-2007 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: *******************************************************************************/package net.sf.oval.guard;
013:
014: import java.lang.reflect.AccessibleObject;
015: import java.lang.reflect.Constructor;
016: import java.lang.reflect.Field;
017: import java.lang.reflect.Method;
018: import java.util.WeakHashMap;
019:
020: import net.sf.oval.exception.ReflectionException;
021: import net.sf.oval.internal.util.ReflectionUtils;
022:
023: import org.aspectj.lang.JoinPoint;
024: import org.aspectj.lang.reflect.ConstructorSignature;
025: import org.aspectj.lang.reflect.MethodSignature;
026:
027: /**
028: * This class determines the names of constructor and method parameters based on the static JoinPoint fields added to the classes by the AspectJ compiler
029: *
030: * @author Sebastian Thomschke
031: */
032: public class ParameterNameResolverAspectJImpl implements
033: ParameterNameResolver {
034: private final WeakHashMap<AccessibleObject, String[]> parameterNamesCache = new WeakHashMap<AccessibleObject, String[]>();
035:
036: public String[] getParameterNames(final Method method)
037: throws ReflectionException {
038: /*
039: * intentionally the following code is not synchronized
040: */
041: String[] parameterNames = parameterNamesCache.get(method);
042: if (parameterNames == null) {
043: try {
044: determineParamterNames(method.getDeclaringClass());
045: parameterNames = parameterNamesCache.get(method);
046: } catch (IllegalArgumentException e) {
047: throw new ReflectionException(
048: "Cannot detemine parameter names for method "
049: + method, e);
050: } catch (IllegalAccessException e) {
051: throw new ReflectionException(
052: "Cannot detemine parameter names for method "
053: + method, e);
054: }
055: }
056:
057: if (parameterNames == null) {
058: final int parameterCount = method.getParameterTypes().length;
059: parameterNames = new String[parameterCount];
060: for (int i = 0; i < parameterCount; i++) {
061: parameterNames[i] = "parameter" + i;
062: }
063: parameterNamesCache.put(method, parameterNames);
064: }
065: return parameterNames;
066: }
067:
068: public String[] getParameterNames(final Constructor constructor)
069: throws ReflectionException {
070: /*
071: * intentionally the following code is not synchronized
072: */
073: String[] parameterNames = parameterNamesCache.get(constructor);
074: if (parameterNames == null) {
075: try {
076: determineParamterNames(constructor.getDeclaringClass());
077: parameterNames = parameterNamesCache.get(constructor);
078: } catch (IllegalArgumentException e) {
079: throw new ReflectionException(
080: "Cannot detemine parameter names for constructor "
081: + constructor, e);
082: } catch (IllegalAccessException e) {
083: throw new ReflectionException(
084: "Cannot detemine parameter names for constructor "
085: + constructor, e);
086: }
087: }
088:
089: if (parameterNames == null) {
090: final int parameterCount = constructor.getParameterTypes().length;
091: parameterNames = new String[parameterCount];
092: for (int i = 0; i < parameterCount; i++) {
093: parameterNames[i] = "parameter" + i;
094: }
095: parameterNamesCache.put(constructor, parameterNames);
096: }
097: return parameterNames;
098: }
099:
100: private void determineParamterNames(final Class clazz)
101: throws IllegalArgumentException, IllegalAccessException {
102: assert clazz != null;
103:
104: for (final Field field : clazz.getDeclaredFields()) {
105: // search for static fields of type JoinPoint.StaticPart
106: if (ReflectionUtils.isStatic(field)
107: && field.getType() == JoinPoint.StaticPart.class) {
108: // access the StaticPart object
109: field.setAccessible(true);
110: final JoinPoint.StaticPart staticPart = (JoinPoint.StaticPart) field
111: .get(null);
112: if (staticPart == null)
113: break;
114:
115: if (staticPart.getSignature() instanceof ConstructorSignature) {
116: final ConstructorSignature sig = (ConstructorSignature) staticPart
117: .getSignature();
118: final String[] parameterNames = sig
119: .getParameterNames();
120:
121: final Constructor constr = sig.getConstructor();
122:
123: if (parameterNames.length > 0)
124: parameterNamesCache.put(constr, parameterNames);
125: } else if (staticPart.getSignature() instanceof MethodSignature) {
126: final MethodSignature sig = (MethodSignature) staticPart
127: .getSignature();
128: final String[] parameterNames = sig
129: .getParameterNames();
130:
131: final Method method = sig.getMethod();
132:
133: if (parameterNames.length > 0)
134: parameterNamesCache.put(method, parameterNames);
135: }
136: }
137: }
138: }
139: }
|