1
2
3 package org.virtualmock.aspect;
4
5 import java.util.Hashtable;
6 import org.codehaus.aspectwerkz.regexp.ClassPattern;
7 import org.codehaus.aspectwerkz.regexp.Pattern;
8 import org.virtualmock.call.InvokedCall;
9 import org.virtualmock.call.RecordedCall;
10 import org.virtualmock.util.Toolbox;
11
12
13 /***
14 * This class consists of utility methods which have been abstracted out of the
15 * Advice class. This makes them easier to debug and unit test. It also acts
16 * as an encapsulation layer between the advices and the rest of the class.
17 * This is the only class which deals with aspect-specific classes, such as
18 * JoinPoints.
19 *
20 * @author Chad Woolley
21 * @version $Revision: 1.15 $
22 */
23 public class AspectUtils {
24 /***
25 * Returns true if the specified class should be excluded from VirtualMock
26 * processing.
27 *
28 * @param targetClass the class which will be checked to see if it should
29 * be excluded
30 *
31 * @return true if the specified class should be excluded
32 *
33 * @todo hardcoded to exclude VMTest class as an example
34 */
35 public boolean isClassExcluded(Class targetClass) {
36 String targetClassName = targetClass.getName();
37
38 String classExclusionPattern = "org.virtualmock.VMTest";
39 ClassPattern classPattern =
40 Pattern.compileClassPattern(classExclusionPattern);
41 boolean isExcluded = classPattern.matches(targetClassName);
42
43 return isExcluded;
44 }
45
46 /***
47 * Log4j logger
48 *
49 * @param argumentValues DOCUMENT ME! (Constructor Parameter)
50 *
51 * @return DOCUMENT ME! (Method Return)
52 */
53
54
55
56
57
58 /***
59 * Create dummy argument names.
60 *
61 * @param argumentValues the argument values for which the dummy argument
62 * names will be created.
63 *
64 * @return an array of dummy argument names.
65 */
66 public String[] createArgumentNames(Object[] argumentValues) {
67
68
69
70
71
72 String[] argumentNames = new String[argumentValues.length];
73
74 for (int j = 0; j < argumentValues.length; j++) {
75 StringBuffer parameterName = new StringBuffer();
76 parameterName.append("arg");
77 parameterName.append(j);
78 argumentNames[j] = parameterName.toString();
79 }
80
81 return argumentNames;
82 }
83
84 /***
85 * Transform two arrays of matching argument types and argument values into
86 * a hashtable of corresponding key/value pairs, with the argumentType as
87 * the key.
88 *
89 * @param argumentTypes an array of argument types
90 * @param argumentValues an array of argument values
91 *
92 * @return the argument types and values represented as a hashtable
93 */
94 public Hashtable hashArguments(Class[] argumentTypes,
95 Object[] argumentValues) {
96 Hashtable arguments = null;
97
98 for (int i = 0; i < argumentValues.length; i++) {
99 if (argumentValues[i] != null) {
100 if (arguments == null) {
101 arguments = new Hashtable();
102 }
103
104 arguments.put(argumentTypes[i], argumentValues[i]);
105 }
106 }
107
108 return arguments;
109 }
110
111 /***
112 * Performs the logic of playing back a recorded mock call.
113 *
114 * @param invokedCall the call that was invoked
115 * @param recordedCall the recorded call that matches this invoked call
116 *
117 * @return the recorded mock return type (which may be null if one was not
118 * recorded)
119 *
120 * @throws Throwable the recorded mock exception (if one was recorded)
121 */
122 public Object playbackMockCall(InvokedCall invokedCall,
123 RecordedCall recordedCall) throws Throwable {
124
125
126 Toolbox.getCallManager().indicateCalled(invokedCall);
127
128
129 Throwable throwable = recordedCall.getThrowable();
130
131
132 if (throwable != null) {
133
134
135
136
137 throw throwable;
138 }
139
140
141 Object returnValue = recordedCall.getReturnValue();
142
143
144 if (returnValue != null) {
145
146
147
148
149 return returnValue;
150 }
151
152
153
154
155
156
157
158 return null;
159 }
160 }