Search in sources :

Example 11 with VoidVisitorAdapter

use of japa.parser.ast.visitor.VoidVisitorAdapter in project Japid by branaway.

the class JavaSyntaxTool method parseArgs.

// XXX this method does not properly parse thingsl like A<t> a
// it does not detect the error
@SuppressWarnings("unchecked")
public static List<String> parseArgs(String line) {
    final List<String> ret = new ArrayList<String>();
    if (line == null || line.trim().length() == 0)
        return ret;
    @SuppressWarnings("rawtypes") VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

        @Override
        public void visit(MethodCallExpr n, Object arg) {
            List<Expression> args = n.getArgs();
            // api issue: args can be null in case of empty arg list
            if (args != null)
                for (Expression e : args) {
                    ret.add(e.toString());
                }
        }
    };
    line = line.trim();
    if (line.startsWith("(")) {
        // perhaps it's in the form of (...)
        String cl = String.format(classTempForArgsNoParenthesis, line);
        try {
            CompilationUnit cu = parse(cl);
            cu.accept(visitor, null);
            return ret;
        } catch (ParseException e) {
        // perhaps not really (...). fall through
        }
    }
    String cl = String.format(classTempForArgs, line);
    try {
        CompilationUnit cu = parse(cl);
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid arg list: " + line);
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) Expression(japa.parser.ast.expr.Expression) ArrayList(java.util.ArrayList) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 12 with VoidVisitorAdapter

use of japa.parser.ast.visitor.VoidVisitorAdapter in project Japid by branaway.

the class JavaSyntaxTool method hasMethodInvocatioin.

public static boolean hasMethodInvocatioin(CompilationUnit cu, final String string) {
    if (string == null || string.trim().length() == 0)
        return false;
    final StringBuilder re = new StringBuilder();
    VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

        @Override
        public void visit(MethodCallExpr n, Object arg) {
            if (string.equals(n.getName())) {
                re.append(1);
                return;
            } else {
                super.visit(n, arg);
            }
        }
    };
    cu.accept(visitor, null);
    if (re.length() == 0)
        return false;
    else
        return true;
}
Also used : VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 13 with VoidVisitorAdapter

use of japa.parser.ast.visitor.VoidVisitorAdapter in project Japid by branaway.

the class JavaSyntaxTool method parseNamedArgs.

/**
	 * 
	 * @param line
	 * @return list of named args if all the args are named; empty list if none
	 *         is named; or an exception is thrown if the arg list is not valid
	 *         or named and un-named are mixed
	 * 
	 */
public static List<NamedArg> parseNamedArgs(String line) {
    final List<NamedArg> ret = new ArrayList<NamedArg>();
    if (line == null || line.trim().length() == 0)
        return ret;
    line = line.trim();
    //		if (line.startsWith("(")) {
    //			if (line.endsWith(")"))
    //				line = line.substring(1, line.length() - 1);
    //			else
    //				throw new RuntimeException("no closing ')' in arg expression: "
    //						+ line);
    //		}
    String cl = String.format(classTempForArgs, line);
    final String finalLine = line;
    try {
        CompilationUnit cu = parse(cl);
        VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

            boolean hasNamed = false;

            boolean hasUnNamed = false;

            @Override
            public void visit(MethodCallExpr n, Object arg) {
                List<Expression> args = n.getArgs();
                // api issue: args can be null in case of empty arg list
                if (args != null)
                    for (Expression expr : args) {
                        if (expr instanceof AssignExpr) {
                            if (hasUnNamed)
                                throw new RuntimeException("the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: " + finalLine);
                            hasNamed = true;
                            AssignExpr ae = (AssignExpr) expr;
                            NamedArg na = new NamedArg(ae.getTarget(), ae.getValue());
                            ret.add(na);
                        } else {
                            if (hasNamed)
                                throw new RuntimeException("the line has mixed named and un-named arg list. It's not valid in Japid tag invocation. It must be all-or-none.: " + finalLine);
                            hasUnNamed = true;
                        }
                    }
            }
        };
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid arg list: " + line + ". ");
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ArrayList(java.util.ArrayList) AssignExpr(japa.parser.ast.expr.AssignExpr) Expression(japa.parser.ast.expr.Expression) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 14 with VoidVisitorAdapter

use of japa.parser.ast.visitor.VoidVisitorAdapter in project japid42 by branaway.

the class JavaSyntaxTool method hasMethod.

public static boolean hasMethod(CompilationUnit cu, final String string) {
    final StringBuilder sb = new StringBuilder();
    VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

        @Override
        public void visit(MethodDeclaration n, Object arg) {
            if (n.getName().equals(string)) {
                sb.append(1);
                return;
            }
        }
    };
    cu.accept(visitor, null);
    if (sb.length() == 0)
        return false;
    else
        return true;
}
Also used : MethodDeclaration(japa.parser.ast.body.MethodDeclaration) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter)

Example 15 with VoidVisitorAdapter

use of japa.parser.ast.visitor.VoidVisitorAdapter in project japid42 by branaway.

the class JavaSyntaxTool method parseParams.

/**
	 * parse a line of text that is supposed to be parameter list for a method
	 * declaration.
	 * 
	 * TODO: the parameter annotation, modifiers, etc ignored. should do it!
	 * 
	 * @param line
	 * @return
	 */
public static List<Parameter> parseParams(String line) {
    final List<Parameter> ret = new ArrayList<Parameter>();
    if (line == null || line.trim().length() == 0)
        return ret;
    // make it tolerant of lowercase default
    line = line.replace("@default(", "@Default(");
    String cl = String.format(classTempForParams, line);
    try {
        CompilationUnit cu = parse(cl);
        VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

            @Override
            public void visit(Parameter p, Object arg) {
                ret.add(p);
            }
        };
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid param list declaration: " + line);
    }
    return ret;
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ArrayList(java.util.ArrayList) Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter)

Aggregations

VoidVisitorAdapter (japa.parser.ast.visitor.VoidVisitorAdapter)16 ParseException (japa.parser.ParseException)10 CompilationUnit (japa.parser.ast.CompilationUnit)10 MethodCallExpr (japa.parser.ast.expr.MethodCallExpr)8 ArrayList (java.util.ArrayList)8 TypeParameter (japa.parser.ast.TypeParameter)6 MethodDeclaration (japa.parser.ast.body.MethodDeclaration)6 Parameter (japa.parser.ast.body.Parameter)6 AssignExpr (japa.parser.ast.expr.AssignExpr)4 Expression (japa.parser.ast.expr.Expression)4 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 BlockComment (japa.parser.ast.BlockComment)2 ImportDeclaration (japa.parser.ast.ImportDeclaration)2 LineComment (japa.parser.ast.LineComment)2 PackageDeclaration (japa.parser.ast.PackageDeclaration)2 AnnotationDeclaration (japa.parser.ast.body.AnnotationDeclaration)2