Search in sources :

Example 6 with Parameter

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;
}
Also used : VoidType(japa.parser.ast.type.VoidType) ReferenceType(japa.parser.ast.type.ReferenceType) WildcardType(japa.parser.ast.type.WildcardType) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) Type(japa.parser.ast.type.Type) PrimitiveType(japa.parser.ast.type.PrimitiveType) MethodDeclaration(japa.parser.ast.body.MethodDeclaration) ArrayList(java.util.ArrayList) Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter)

Example 7 with Parameter

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;
}
Also used : Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter)

Example 8 with Parameter

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);
}
Also used : Parameter(japa.parser.ast.body.Parameter)

Example 9 with Parameter

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(", "));
}
Also used : Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter)

Example 10 with Parameter

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;
}
Also used : VoidType(japa.parser.ast.type.VoidType) ReferenceType(japa.parser.ast.type.ReferenceType) WildcardType(japa.parser.ast.type.WildcardType) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) Type(japa.parser.ast.type.Type) PrimitiveType(japa.parser.ast.type.PrimitiveType) MethodDeclaration(japa.parser.ast.body.MethodDeclaration) ArrayList(java.util.ArrayList) Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter)

Aggregations

Parameter (japa.parser.ast.body.Parameter)18 TypeParameter (japa.parser.ast.TypeParameter)12 VoidVisitorAdapter (japa.parser.ast.visitor.VoidVisitorAdapter)6 CompilationUnit (japa.parser.ast.CompilationUnit)5 MethodDeclaration (japa.parser.ast.body.MethodDeclaration)5 ParseException (japa.parser.ParseException)4 PackageDeclaration (japa.parser.ast.PackageDeclaration)3 ClassOrInterfaceDeclaration (japa.parser.ast.body.ClassOrInterfaceDeclaration)3 ClassOrInterfaceType (japa.parser.ast.type.ClassOrInterfaceType)3 PrimitiveType (japa.parser.ast.type.PrimitiveType)3 ReferenceType (japa.parser.ast.type.ReferenceType)3 VoidType (japa.parser.ast.type.VoidType)3 WildcardType (japa.parser.ast.type.WildcardType)3 ArrayList (java.util.ArrayList)3 BlockComment (japa.parser.ast.BlockComment)2 ImportDeclaration (japa.parser.ast.ImportDeclaration)2 LineComment (japa.parser.ast.LineComment)2 AnnotationDeclaration (japa.parser.ast.body.AnnotationDeclaration)2 AnnotationMemberDeclaration (japa.parser.ast.body.AnnotationMemberDeclaration)2 ConstructorDeclaration (japa.parser.ast.body.ConstructorDeclaration)2