001: package net.sf.mockcreator.codegeneration;
002:
003: import java.lang.reflect.Constructor;
004: import java.lang.reflect.Method;
005: import java.lang.reflect.Modifier;
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import net.sf.mockcreator.utils.SourceUtils;
010: import net.sf.mockcreator.utils.TemplateUtils;
011:
012: public class MethodGenerator {
013: private MockDescriptor md;
014: private Method method;
015: String returnableClassName;
016:
017: IMockedMethodGenerator mockedMethodGenerator;
018:
019: public MethodGenerator(MockDescriptor md,
020: java.lang.reflect.Method method) {
021: this .md = md;
022: this .method = method;
023:
024: returnableClassName = "ReturnableFor_"
025: + SourceUtils.getMethodFQN(method.getName(), method
026: .getParameterTypes(), method.getReturnType());
027: returnableClassName = ("Returnable" + returnableClassName
028: .hashCode()).replace('-', 'x');
029:
030: mockedMethodGenerator = new MockedMethodGeneratorJava15();
031: mockedMethodGenerator.setMethod(method);
032: }
033:
034: public String getMethodMockMethods() {
035: String methods = mockedMethodGenerator.getReturnables();
036: String returnables = TemplateUtils.format(
037: "returnable.template", new Object[] {
038: returnableClassName, methods });
039:
040: String body = mockedMethodGenerator.getMockedMethodBody();
041:
042: return returnables + body + getMethodConfigurationMethods();
043: }
044:
045: public String getMethodMockDeclaration() {
046: return mockedMethodGenerator.getMockedMethodDeclaration();
047: }
048:
049: public String getMethodConfigurationMethods() {
050: return getExpectMethodSource() + "\n" + getAcceptMethodSource()
051: + "\n" + getExpectZeroOrMoreMethodSource() + "\n"
052: + getExpectZeroOrMoreWithParamsMethodSource() + "\n"
053: + getAcceptZeroOrMoreMethodSource() + "\n" + "\n\n";
054: }
055:
056: private String getExpectMethodSource() {
057: String methodName = uniqueMethodName("expect", method, method
058: .getParameterTypes());
059: // String params = SourceUtils.getParametersSpecification(method.getParameterTypes());
060: String params = mockedMethodGenerator
061: .getParametersSpecification();
062: String paramsList = SourceUtils.getPackParametersSrc(method);
063: String id = SourceUtils
064: .getClassObjectMethodSignatureSrc(method);
065:
066: return TemplateUtils
067: .format("expectMethod.template", new Object[] {
068: returnableClassName,
069: methodName,
070: params,
071: paramsList,
072: id,
073: new TypeUtilsJava15()
074: .getCanonicalMethodParameters(method) });
075: }
076:
077: private String getAcceptMethodSource() {
078: if (method.getParameterTypes().length == 0) {
079: return "";
080: }
081:
082: String methodName = uniqueMethodName("accept", method,
083: SourceUtils.getObjects(method));
084: // String params = getObjectsSpecification();
085: String params = mockedMethodGenerator
086: .getAcceptableParametersSpecification();
087: String paramsList = SourceUtils.getObjectsListSrc(method);
088: String id = SourceUtils
089: .getClassObjectMethodSignatureSrc(method);
090:
091: return TemplateUtils.format("acceptMethod.template",
092: new Object[] { returnableClassName, methodName, params,
093: paramsList, id });
094: }
095:
096: private String getExpectZeroOrMoreMethodSource() {
097: String methodName = uniqueMethodName("expectZeroOrMore",
098: method, null);
099: String id = SourceUtils
100: .getClassObjectMethodSignatureSrc(method);
101:
102: return TemplateUtils
103: .format(
104: "expectZeroOrMoreMethod.template",
105: new Object[] {
106: returnableClassName,
107: methodName,
108: id,
109: new TypeUtilsJava15()
110: .getCanonicalMethodParameters(method) });
111: }
112:
113: private String getExpectZeroOrMoreWithParamsMethodSource() {
114: if (method.getParameterTypes().length == 0) {
115: return "";
116: }
117:
118: String methodName = uniqueMethodName("expectZeroOrMore",
119: method, method.getParameterTypes());
120: // String params = SourceUtils.getParametersSpecification(method.getParameterTypes());
121: String params = mockedMethodGenerator
122: .getParametersSpecification();
123: String paramsList = SourceUtils.getPackParametersSrc(method);
124: String id = SourceUtils
125: .getClassObjectMethodSignatureSrc(method);
126:
127: return TemplateUtils
128: .format(
129: "expectZeroOrMoreMethod.WithParameters.template",
130: new Object[] {
131: returnableClassName,
132: methodName,
133: params,
134: paramsList,
135: id,
136: new TypeUtilsJava15()
137: .getCanonicalMethodParameters(method) });
138: }
139:
140: private String getAcceptZeroOrMoreMethodSource() {
141: if (method.getReturnType().getName().equals("void")
142: || method.getParameterTypes().length == 0) {
143: return "";
144: }
145:
146: String methodName = uniqueMethodName("acceptZeroOrMore",
147: method, SourceUtils.getObjects(method));
148: // String params = getObjectsSpecification();
149: String params = mockedMethodGenerator
150: .getAcceptableParametersSpecification();
151: String paramsList = SourceUtils.getObjectsListSrc(method);
152: String id = SourceUtils
153: .getClassObjectMethodSignatureSrc(method);
154:
155: return TemplateUtils.format(
156: "acceptZeroOrMoreMethod.WithParameters.template",
157: new Object[] { returnableClassName, methodName, params,
158: paramsList, id });
159: }
160:
161: private String uniqueMethodName(String prefix, Method method,
162: Class[] actualParams) {
163: String proposedName = method.getName();
164: Class[] params = method.getParameterTypes();
165: Class returnValue = method.getReturnType();
166:
167: String norm = SourceUtils.capitalizeFirstLetter(proposedName);
168: String name = prefix + norm;
169:
170: String signature = SourceUtils.getMethodFQN(name, actualParams,
171: null);
172: if (signatureCount(signature) == 1) {
173: return name;
174: }
175:
176: return SourceUtils.getMethodFQN(name, params, returnValue);
177: }
178:
179: private int signatureCount(String signature) {
180: Integer cnt = (Integer) md.getSignatures().get(signature);
181:
182: if (cnt == null) {
183: return 0;
184: }
185:
186: return cnt.intValue();
187: }
188:
189: public static void addSignatures(Map signatures, Method mth) {
190: String nn = SourceUtils.capitalizeFirstLetter(mth.getName());
191:
192: incrementSignatureCount(signatures, SourceUtils.getMethodFQN(
193: "expect" + nn, mth.getParameterTypes(), null));
194:
195: if (mth.getParameterTypes().length != 0) {
196: incrementSignatureCount(signatures, SourceUtils
197: .getMethodFQN("accept" + nn, SourceUtils
198: .getObjects(mth), null));
199: incrementSignatureCount(signatures, SourceUtils
200: .getMethodFQN("acceptZeroOrMore" + nn, SourceUtils
201: .getObjects(mth), null));
202: }
203:
204: incrementSignatureCount(signatures, SourceUtils.getMethodFQN(
205: "expectZeroOrMore" + nn, null, null));
206: if (mth.getParameterTypes().length != 0) {
207: incrementSignatureCount(signatures, SourceUtils
208: .getMethodFQN("expectZeroOrMore" + nn, mth
209: .getParameterTypes(), null));
210: }
211: }
212:
213: private static void incrementSignatureCount(Map signatures,
214: String signature) {
215: Integer cnt = (Integer) signatures.get(signature);
216: if (cnt == null) {
217: cnt = new Integer(0);
218: }
219: cnt = new Integer(cnt.intValue() + 1);
220: signatures.put(signature, cnt);
221: }
222:
223: public static String getCtors(MockDescriptor md) {
224: String ret = "";
225: boolean thereWasDefaultCtor = false;
226: Constructor lastCtor = null;
227: int lastCtorParamsLength = 0;
228:
229: Iterator ci = md.getCtors().iterator();
230:
231: while (ci.hasNext()) {
232: Constructor ctor = (Constructor) ci.next();
233:
234: if ((ctor.getModifiers() & Modifier.PRIVATE) != 0) {
235: continue;
236: }
237:
238: if (ctor.getParameterTypes().length == 0) {
239: thereWasDefaultCtor = true;
240: }
241:
242: Class[] params = ctor.getParameterTypes();
243: ret += getCtorSrc(md, ctor, params);
244:
245: if ((lastCtor == null)
246: || (lastCtorParamsLength > params.length)) {
247: lastCtor = ctor;
248: lastCtorParamsLength = params.length;
249: }
250: }
251:
252: if (!thereWasDefaultCtor) {
253: ret += getCtorSrc(md, lastCtor, null);
254: }
255:
256: return ret;
257: }
258:
259: public static String getCtorSrc(MockDescriptor md,
260: Constructor ctor, Class[] params) {
261: String ps = params != null ? SourceUtils
262: .getParametersSpecification(params) : "";
263: String ts = ctor != null ? SourceUtils
264: .getThrowsSpecification(ctor.getExceptionTypes()) : "";
265:
266: return TemplateUtils.format("ctor.template", new Object[] {
267: md.getName(), ps, ts,
268: SourceUtils.getSuperCtorCallSrc(ctor, params) });
269: }
270: }
|