001: package net.sf.mockcreator.codegeneration;
002:
003: import java.lang.reflect.Method;
004: import java.util.Iterator;
005: import java.util.Map;
006:
007: import net.sf.mockcreator.utils.SourceUtils;
008:
009: public class MockBuilder {
010:
011: public static Version version = new Version();
012: public static String BASENAME = "MOCK_CREATOR_BASE_TYPE";
013:
014: private static IFormatter mf = new FormatterJava15();
015:
016: public static String createMock(MockDescriptor md) throws Exception {
017: mf.setClass(md.getSourceClass());
018: collectSignatures(md);
019: return createClassHeader(md) + "\n" + createClass(md);
020: }
021:
022: private static String createClassHeader(MockDescriptor md) {
023: StringBuffer typeSource = new StringBuffer();
024:
025: if (!md.isTargetInDefaultPackage()) {
026: typeSource.append("package ");
027: typeSource.append(md.getTargetPackageName());
028: typeSource.append(";\n");
029: typeSource.append("\n");
030: }
031:
032: return typeSource.toString();
033: }
034:
035: private static String createClass(MockDescriptor md) {
036: StringBuffer typeSource = new StringBuffer();
037:
038: typeSource.append("public ");
039:
040: typeSource.append("class ");
041: typeSource.append("Mock" + mf.getClassName());
042:
043: addImplementExtends(md, typeSource);
044:
045: typeSource.append("{\n");
046:
047: typeSource.append(getMethodSources(md));
048:
049: typeSource
050: .append(" private String getClassObjectMethodSignature(String method){ return \""
051: + md.getTargetFullName()
052: + "\"+\"#\"+__id()+\"#\"+method; }\n");
053:
054: typeSource.append(" private String id;\n");
055: typeSource.append(" public String __id(){ return id; }\n");
056:
057: typeSource.append(" public static final String "
058: + MockBuilder.BASENAME + " = \"" + md.getSourceFQN()
059: + "\";\n");
060:
061: typeSource.append("}\n");
062:
063: return typeSource.toString();
064: }
065:
066: private static void addImplementExtends(MockDescriptor md,
067: StringBuffer typeSource) {
068: if (md.isInterface()) {
069: typeSource.append(" implements ");
070: } else {
071: typeSource.append(" extends ");
072: }
073:
074: if (!md.isSourceInDefaultPackage()) {
075: typeSource.append(md.getSourcePackageName());
076: typeSource.append('.');
077: }
078:
079: // typeSource.append(md.getName());
080: typeSource.append(mf.getMockedClassName());
081:
082: if (md.isInterface()) {
083: typeSource.append(", net.sf.mockcreator.MockBase ");
084: } else {
085: typeSource
086: .append(" implements net.sf.mockcreator.MockBase ");
087: }
088:
089: typeSource.append("\n");
090: }
091:
092: private static void collectSignatures(MockDescriptor md) {
093: Map signatures = md.getSignatures();
094:
095: Iterator methods = md.getMethods().iterator();
096: while (methods.hasNext()) {
097: Method method = (Method) methods.next();
098: MethodGenerator.addSignatures(signatures, method);
099: }
100: }
101:
102: private static String getMethodSources(MockDescriptor md) {
103: String ret = "";
104: String members = "";
105:
106: ret += MethodGenerator.getCtors(md);
107:
108: Iterator methods = md.getMethods().iterator();
109: while (methods.hasNext()) {
110: Method method = (Method) methods.next();
111: MethodGenerator mg = new MethodGenerator(md, method);
112:
113: if (SourceUtils.isMockableInClass(method)) {
114: ret += mg.getMethodMockMethods();
115: }
116: }
117:
118: return members + ret;
119: }
120: }
|