001: package net.sf.mockcreator.codegeneration;
002:
003: import java.lang.reflect.GenericArrayType;
004: import java.lang.reflect.Method;
005: import java.lang.reflect.Type;
006:
007: public class FormatterJava15 implements IFormatter {
008:
009: private Class clz;
010: private Method method;
011: private TypeUtilsJava15 typeUtils = new TypeUtilsJava15();
012:
013: public void setClass(Class clz) {
014: this .clz = clz;
015: }
016:
017: public void setMethod(Method method) {
018: this .method = method;
019: }
020:
021: public String getClassName() {
022: StringBuffer sb = new StringBuffer();
023:
024: String clzName = clz.getSimpleName();
025: int dot = clzName.lastIndexOf('.');
026: if (dot >= 0) {
027: clzName = clzName.substring(dot + 1);
028: }
029: sb.append(clzName);
030:
031: Type[] types = clz.getTypeParameters();
032: if (types.length > 0) {
033: sb.append('<');
034: for (int i = 0; i < types.length; ++i) {
035: if (i != 0)
036: sb.append(", ");
037: sb
038: .append(typeUtils
039: .getFullCanonicalTypeSpellingForTypeVariable(types[i]));
040: }
041: sb.append('>');
042: }
043:
044: return sb.toString();
045: }
046:
047: public String getMockedClassName() {
048: StringBuffer sb = new StringBuffer();
049:
050: String clzName = clz.getSimpleName();
051: int dot = clzName.lastIndexOf('.');
052: if (dot >= 0) {
053: clzName = clzName.substring(dot + 1);
054: }
055: sb.append(clzName);
056:
057: Type[] types = clz.getTypeParameters();
058: if (types.length > 0) {
059: sb.append('<');
060: for (int i = 0; i < types.length; ++i) {
061: if (i != 0)
062: sb.append(", ");
063: sb.append(typeUtils.getCanonicalTypeSpelling(types[i]));
064: }
065: sb.append('>');
066: }
067:
068: return sb.toString();
069: }
070:
071: public String getReturnType() {
072: return typeUtils.getCanonicalTypeSpelling(method
073: .getGenericReturnType());
074: }
075:
076: public String getMethodName() {
077: return method.getName();
078: }
079:
080: public String getParameters() {
081: Type[] params = method.getGenericParameterTypes();
082:
083: StringBuffer sb = new StringBuffer();
084: for (int i = 0; i < params.length; ++i) {
085: if (i > 0)
086: sb.append(", ");
087:
088: if (i == params.length - 1 && method.isVarArgs()) {
089: if (params[i] instanceof GenericArrayType) {
090: GenericArrayType gat = (GenericArrayType) params[i];
091: String p = typeUtils.getCanonicalTypeSpelling(gat
092: .getGenericComponentType());
093: sb.append(p + " ... p" + i);
094: } else {
095: Class clz = (Class) params[i];
096: String p = typeUtils.getCanonicalTypeSpelling(clz
097: .getComponentType());
098: sb.append(p + " ... p" + i);
099: }
100: } else {
101: sb
102: .append(typeUtils
103: .getCanonicalTypeSpelling(params[i]));
104: sb.append(" ");
105: sb.append("p" + i);
106: }
107: }
108:
109: return sb.toString();
110: }
111:
112: public String getObjectParameters() {
113: Type[] params = method.getGenericParameterTypes();
114:
115: StringBuffer sb = new StringBuffer();
116: for (int i = 0; i < params.length; ++i) {
117: if (i > 0)
118: sb.append(", ");
119:
120: if (i == params.length - 1 && method.isVarArgs()) {
121: if (params[i] instanceof GenericArrayType) {
122: GenericArrayType gat = (GenericArrayType) params[i];
123: String p = typeUtils.getCanonicalTypeSpelling(gat
124: .getGenericComponentType());
125: sb.append(p + " ... p" + i);
126: } else {
127: Class clz = (Class) params[i];
128: String p = typeUtils.getCanonicalTypeSpelling(clz
129: .getComponentType());
130: sb.append(p + " ... p" + i);
131: }
132: } else {
133: sb.append(typeUtils
134: .getCanonicalTypeSpelling(Object.class));
135: sb.append(" ");
136: sb.append("p" + i);
137: }
138: }
139:
140: return sb.toString();
141: }
142:
143: public String getExceptions() {
144: Type[] exs = method.getGenericExceptionTypes();
145: if (exs.length == 0)
146: return "";
147:
148: StringBuffer sb = new StringBuffer();
149: sb.append(" throws ");
150: for (int i = 0; i < exs.length; ++i) {
151: if (i != 0)
152: sb.append(", ");
153: sb.append(typeUtils.getCanonicalTypeSpelling(exs[i]));
154: }
155:
156: return sb.toString();
157: }
158:
159: public String getEnum() {
160: if (!clz.isEnum())
161: return null;
162: StringBuffer sb = new StringBuffer();
163: sb.append("public static enum ");
164: sb.append(clz.getSimpleName());
165: sb.append(" {");
166:
167: Object[] constants = clz.getEnumConstants();
168: for (int i = 0; i < constants.length; ++i) {
169: if (i != 0)
170: sb.append(", ");
171: sb.append(constants[i]);
172: }
173:
174: sb.append(" };");
175: return sb.toString();
176: }
177:
178: }
|