001: package net.sf.mockcreator.codegeneration;
002:
003: import java.lang.reflect.Method;
004: import java.lang.reflect.Modifier;
005: import java.lang.reflect.Type;
006:
007: import net.sf.mockcreator.utils.SourceUtils;
008: import net.sf.mockcreator.utils.TemplateUtils;
009:
010: public class MockedMethodGeneratorJava15 implements
011: IMockedMethodGenerator {
012: private Method method;
013: String returnableClassName;
014:
015: FormatterJava15 mf = new FormatterJava15();
016: TypeUtilsJava15 tu = new TypeUtilsJava15();
017:
018: public String getMockedMethodBody() {
019: boolean isVoid = method.getReturnType().equals(void.class);
020:
021: return TemplateUtils
022: .format(
023: "methodBody.template",
024: new Object[] {
025: mf.getReturnType(),
026: method.getName(),
027: mf.getParameters(),
028: mf.getExceptions(),
029: SourceUtils
030: .getPackParametersSrc(method),
031: SourceUtils
032: .getClassObjectMethodSignatureSrc(method),
033: getExceptionHandling(),
034: isVoid ? "" : getReturnClause(method
035: .getGenericReturnType()),
036: Modifier
037: .isStatic(method.getModifiers()) ? "static"
038: : "",
039: isVoid ? "" : "java.lang.Object ret = ",
040: new TypeUtilsJava15()
041: .getCanonicalMethodParameters(method) });
042: }
043:
044: public String getMockedMethodDeclaration() {
045: return TemplateUtils
046: .format(
047: "methodDeclaration.template",
048: new Object[] {
049: mf.getReturnType(), // typeUtils.getCanonicalTypeSpelling(method.getGenericReturnType()),
050: method.getName(),
051: mf.getParameters(), // typeUtils.getParametersSpecification(method,method.getGenericParameterTypes()),
052: mf.getExceptions(), // typeUtils.getThrowsSpecification(method.getGenericExceptionTypes()),
053: Modifier
054: .isStatic(method.getModifiers()) ? "static"
055: : "" });
056: }
057:
058: private String getReturnClause(Type returnType) {
059: if (returnType instanceof Class
060: && ((Class) returnType).isPrimitive()) {
061: String casted = SourceUtils.unwrap((Class) returnType,
062: "ret");
063: return TemplateUtils.format(
064: "methodReturnPrimitive.template",
065: new Object[] { casted });
066: } else {
067: String canonical = mf.getReturnType(); // typeUtils.getCanonicalTypeSpelling(returnType);
068: return TemplateUtils.format("methodReturnObject.template",
069: new Object[] { canonical });
070: }
071: }
072:
073: public String getExceptionHandling() {
074: String exTemplate = TemplateUtils
075: .getTemplate("methodExceptionHandler.template");
076: String thTemplate = TemplateUtils
077: .getTemplate("methodThrowableHandler.template");
078:
079: StringBuffer exceptions = new StringBuffer();
080: Class[] exs = method.getExceptionTypes();
081: SourceUtils.getAllExceptionHandlers(exceptions, exs,
082: exTemplate, thTemplate);
083: return exceptions.toString();
084: }
085:
086: public void setMethod(Method method) {
087: this .method = method;
088: this .mf.setMethod(method);
089: }
090:
091: public String getReturnables() {
092: StringBuffer methods = new StringBuffer();
093:
094: String thTemplate = TemplateUtils
095: .getTemplate("returnableThrowable.template");
096: Class[] exs = method.getExceptionTypes();
097: SourceUtils.getAllExceptionHandlers(methods, exs, thTemplate,
098: thTemplate);
099:
100: String genericType = new TypeUtilsJava15()
101: .getCanonicalMethodParameters(method);
102:
103: Type ret = method.getGenericReturnType();
104: if (ret instanceof Class) {
105: if (addReturnsMethods(methods, (Class) ret, char.class,
106: Character.class))
107: ;
108: else if (addReturnsMethods(methods, (Class) ret,
109: byte.class, Byte.class))
110: ;
111: else if (addReturnsMethods(methods, (Class) ret,
112: short.class, Short.class))
113: ;
114: else if (addReturnsMethods(methods, (Class) ret, int.class,
115: Integer.class))
116: ;
117: else if (addReturnsMethods(methods, (Class) ret,
118: long.class, Long.class))
119: ;
120: else if (addReturnsMethods(methods, (Class) ret,
121: float.class, Float.class))
122: ;
123: else if (addReturnsMethods(methods, (Class) ret,
124: double.class, Double.class))
125: ;
126: else if (addReturnsMethods(methods, (Class) ret,
127: boolean.class, Boolean.class))
128: ;
129: else if (!void.class.equals(ret)) {
130: methods.append(TemplateUtils.format(
131: "returnableMethod.template", new Object[] {
132: tu.getCanonicalTypeSpelling(ret),
133: genericType }));
134: }
135: } else {
136: methods.append(TemplateUtils.format(
137: "returnableMethod.template", new Object[] {
138: tu.getCanonicalTypeSpelling(ret),
139: genericType }));
140: }
141:
142: return methods.toString();
143: }
144:
145: private boolean addReturnsMethods(StringBuffer clz, Class ret,
146: Class one, Class two) {
147: if (ret.equals(one) || ret.equals(two)) {
148: clz
149: .append(TemplateUtils
150: .format(
151: "returnablePrimitiveMethods.template",
152: new Object[] {
153: SourceUtils
154: .getCanonicalTypeSpelling(one),
155: SourceUtils
156: .getCanonicalTypeSpelling(two) }));
157: return true;
158: }
159: return false;
160: }
161:
162: public String getParametersSpecification() {
163: return mf.getParameters();
164: }
165:
166: public String getAcceptableParametersSpecification() {
167: return mf.getObjectParameters();
168: }
169: }
|