use of japa.parser.ast.body.Parameter in project Japid by branaway.
the class JavaSyntaxTool method hasMethod.
public static boolean hasMethod(CompilationUnit cu, final String name, final int modis, final String returnType, String paramList) {
final StringBuilder sb = new StringBuilder();
if (paramList == null)
paramList = "";
String formalParamList = addParamNamesPlaceHolder(paramList);
final List<Parameter> params = parseParams(formalParamList);
VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
@Override
public void visit(MethodDeclaration n, Object arg) {
if (n.getName().equals(name)) {
int modifiers2 = n.getModifiers();
if (modifiers2 == modis) {
Type type = n.getType();
if (type.toString().equals(returnType)) {
List<Parameter> ps = n.getParameters();
if (ps == null)
ps = new ArrayList<Parameter>();
if (paramsMatch(params, ps)) {
sb.append(1);
return;
}
}
}
}
}
};
cu.accept(visitor, null);
if (sb.length() == 0)
return false;
else
return true;
}
use of japa.parser.ast.body.Parameter in project Japid by branaway.
the class JavaSyntaxTool method paramsMatch.
protected static boolean paramsMatch(List<Parameter> params, List<Parameter> ps) {
if (params == ps)
return true;
if ((params == null && ps != null) || (params != null && ps == null))
return false;
if (params.size() != ps.size()) {
return false;
}
for (int i = 0; i < params.size(); i++) {
Parameter p1 = params.get(i);
Parameter p2 = ps.get(i);
if (!matchParams(p1, p2)) {
return false;
}
}
return true;
}
use of japa.parser.ast.body.Parameter in project Japid by branaway.
the class TemplateClassMetaData method renderMethod.
/**
* the entry point of the template: render(...). Concrete views have this
* method while the layouts do not.
*/
protected void renderMethod() {
String resultType = useWithPlay ? RENDER_RESULT : "String";
String paramNameArray = "";
String paramTypeArray = "";
String paramDefaultsArray = "";
String currentClassFQN = (this.packageName == null ? "" : this.packageName + ".") + this.className;
List<Parameter> params = JavaSyntaxTool.parseParams(this.renderArgs);
String renderArgsWithoutAnnos = "";
// / named param stuff
for (Parameter p : params) {
paramNameArray += "\"" + p.getId() + "\", ";
paramTypeArray += "\"" + p.getType() + "\", ";
String defa = JavaSyntaxTool.getDefault(p);
paramDefaultsArray += defa + ",";
renderArgsWithoutAnnos += p.getType() + " " + p.getId() + ",";
}
if (renderArgsWithoutAnnos.endsWith(",")) {
renderArgsWithoutAnnos = renderArgsWithoutAnnos.substring(0, renderArgsWithoutAnnos.length() - 1);
}
String nameParamCode = String.format(NAMED_PARAM_CODE, paramNameArray, paramTypeArray, paramDefaultsArray, currentClassFQN);
pln(nameParamCode);
if (doBodyArgsString != null)
pln(" { setHasDoBody(); }");
if (renderArgs != null) {
for (Parameter p : params) {
addField(p);
}
// set the render(xxx)
if (doBodyArgsString != null) {
pln(String.format(NAMED_PARAM_WITH_BODY, getLineMarker()));
// the template can be called with a callback body
// the field
pln(TAB + "private DoBody body;");
doBodyInterface();
// now the render(...)
pln("\tpublic " + resultType + " render(" + renderArgsWithoutAnnos + ", DoBody body) {");
pln("\t\t" + "this.body = body;");
// assign the params to fields
for (Parameter p : params) {
pln("\t\tthis." + p.getId() + " = " + p.getId() + ";");
}
restOfRenderBody(resultType);
}
// a version without the body part to allow optional body
pln("\tpublic " + resultType + " render(" + renderArgsWithoutAnnos + ") {");
// assign the params to fields
for (Parameter p : params) {
pln("\t\tthis." + p.getId() + " = " + p.getId() + ";");
}
restOfRenderBody(resultType);
} else {
if (doBodyArgsString != null) {
pln(String.format(NAMED_PARAM_WITH_BODY, getLineMarker()));
// the field
pln(TAB + "DoBody body;");
doBodyInterface();
// now the render(...)
pln("\tpublic " + resultType + " render(DoBody body) {");
pln("\t\t" + "this.body = body;");
restOfRenderBody(resultType);
}
// the parameter-less render()
pln("\tpublic " + resultType + " render() {");
restOfRenderBody(resultType);
}
// the static apply method
String args = "";
for (Parameter p : params) {
args += p.getId() + COMMA;
}
if (args.endsWith(COMMA)) {
args = args.substring(0, args.lastIndexOf(COMMA));
}
String applyMethod = isAbstract ? String.format(APPLY_METHOD_ABSTRACT, resultType, renderArgsWithoutAnnos, this.className, args) : String.format(APPLY_METHOD, resultType, renderArgsWithoutAnnos, this.className, args);
pln("\n" + applyMethod);
}
use of japa.parser.ast.body.Parameter in project japid42 by branaway.
the class JavaSyntaxTool method addFinalToAllParams.
/**
*
* @param params
* Type1 p1, Type2 p2...
* @return Final Type1 p1, final Type2 p2...
*/
public static String addFinalToAllParams(String paramline) {
if (paramline == null)
return null;
paramline = paramline.trim();
if (paramline.length() == 0)
return "";
List<Parameter> params = parseParams(paramline);
String s = "";
for (Parameter p : params) {
s += "final " + p.getType() + " " + p.getId().getName() + ", ";
}
return s.substring(0, s.lastIndexOf(", "));
}
use of japa.parser.ast.body.Parameter in project japid42 by branaway.
the class JavaSyntaxTool method hasMethod.
public static boolean hasMethod(CompilationUnit cu, final String name, final int modis, final String returnType, String paramList) {
final StringBuilder sb = new StringBuilder();
if (paramList == null)
paramList = "";
String formalParamList = addParamNamesPlaceHolder(paramList);
final List<Parameter> params = parseParams(formalParamList);
VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
@Override
public void visit(MethodDeclaration n, Object arg) {
if (n.getName().equals(name)) {
int modifiers2 = n.getModifiers();
if (modifiers2 == modis) {
Type type = n.getType();
if (type.toString().equals(returnType)) {
List<Parameter> ps = n.getParameters();
if (ps == null)
ps = new ArrayList<Parameter>();
if (paramsMatch(params, ps)) {
sb.append(1);
return;
}
}
}
}
}
};
cu.accept(visitor, null);
if (sb.length() == 0)
return false;
else
return true;
}
Aggregations