package net.sf.mockcreator.codegeneration;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import net.sf.mockcreator.utils.SourceUtils;
import net.sf.mockcreator.utils.TemplateUtils;
public class MockedMethodGeneratorJava15 implements IMockedMethodGenerator {
private Method method;
String returnableClassName;
FormatterJava15 mf = new FormatterJava15();
TypeUtilsJava15 tu = new TypeUtilsJava15();
public String getMockedMethodBody()
{
boolean isVoid = method.getReturnType().equals(void.class);
return TemplateUtils.format("methodBody.template",
new Object[]{
mf.getReturnType(),
method.getName(),
mf.getParameters(),
mf.getExceptions(),
SourceUtils.getPackParametersSrc(method),
SourceUtils.getClassObjectMethodSignatureSrc(method),
getExceptionHandling(),
isVoid? "":getReturnClause(method.getGenericReturnType()),
Modifier.isStatic(method.getModifiers())?"static":"",
isVoid? "":"java.lang.Object ret = ",
new TypeUtilsJava15().getCanonicalMethodParameters(method)
});
}
public String getMockedMethodDeclaration()
{
return TemplateUtils.format("methodDeclaration.template",
new Object[]{
mf.getReturnType(), // typeUtils.getCanonicalTypeSpelling(method.getGenericReturnType()),
method.getName(),
mf.getParameters(), // typeUtils.getParametersSpecification(method,method.getGenericParameterTypes()),
mf.getExceptions(), // typeUtils.getThrowsSpecification(method.getGenericExceptionTypes()),
Modifier.isStatic(method.getModifiers())?"static":""
});
}
private String getReturnClause(Type returnType) {
if( returnType instanceof Class && ((Class)returnType).isPrimitive() ) {
String casted = SourceUtils.unwrap((Class)returnType, "ret");
return TemplateUtils.format("methodReturnPrimitive.template",new Object[]{casted});
} else {
String canonical = mf.getReturnType(); // typeUtils.getCanonicalTypeSpelling(returnType);
return TemplateUtils.format("methodReturnObject.template",new Object[]{canonical});
}
}
public String getExceptionHandling() {
String exTemplate = TemplateUtils.getTemplate("methodExceptionHandler.template");
String thTemplate = TemplateUtils.getTemplate("methodThrowableHandler.template");
StringBuffer exceptions = new StringBuffer();
Class[] exs = method.getExceptionTypes();
SourceUtils.getAllExceptionHandlers(exceptions, exs, exTemplate, thTemplate);
return exceptions.toString();
}
public void setMethod(Method method) {
this.method = method;
this.mf.setMethod(method);
}
public String getReturnables() {
StringBuffer methods = new StringBuffer();
String thTemplate = TemplateUtils.getTemplate("returnableThrowable.template");
Class[] exs = method.getExceptionTypes();
SourceUtils.getAllExceptionHandlers(methods, exs, thTemplate, thTemplate);
String genericType = new TypeUtilsJava15().getCanonicalMethodParameters(method);
Type ret = method.getGenericReturnType();
if( ret instanceof Class ) {
if( addReturnsMethods(methods,(Class)ret,char.class,Character.class) );
else if( addReturnsMethods(methods,(Class)ret,byte.class,Byte.class) );
else if( addReturnsMethods(methods,(Class)ret,short.class,Short.class) );
else if( addReturnsMethods(methods,(Class)ret,int.class,Integer.class) );
else if( addReturnsMethods(methods,(Class)ret,long.class,Long.class) );
else if( addReturnsMethods(methods,(Class)ret,float.class,Float.class) );
else if( addReturnsMethods(methods,(Class)ret,double.class,Double.class) );
else if( addReturnsMethods(methods,(Class)ret,boolean.class,Boolean.class) );
else if( !void.class.equals(ret) ) {
methods.append(TemplateUtils.format("returnableMethod.template", new Object[]{tu.getCanonicalTypeSpelling(ret),genericType}));
}
} else {
methods.append(TemplateUtils.format("returnableMethod.template", new Object[]{tu.getCanonicalTypeSpelling(ret),genericType}));
}
return methods.toString();
}
private boolean addReturnsMethods(StringBuffer clz, Class ret, Class one, Class two) {
if( ret.equals(one) || ret.equals(two) ) {
clz.append(TemplateUtils.format("returnablePrimitiveMethods.template", new Object[]{SourceUtils.getCanonicalTypeSpelling(one),SourceUtils.getCanonicalTypeSpelling(two)}));
return true;
}
return false;
}
public String getParametersSpecification() {
return mf.getParameters();
}
public String getAcceptableParametersSpecification() {
return mf.getObjectParameters();
}
}
|