Search in sources :

Example 1 with VoidVisitorAdapter

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

the class JavaSyntaxTool method parseCode.

public static List<CodeNode> parseCode(String code) {
    try {
        final List<CodeNode> nodes = new LinkedList();
        CompilationUnit cu = parse(code);
        VoidVisitorAdapter visitor = new VoidVisitorAdapter() {

            int nested = 0;

            @Override
            public void visit(AnnotationDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(AnnotationMemberDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ArrayAccessExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ArrayCreationExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ArrayInitializerExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(AssertStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(AssignExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(BinaryExpr n, Object arg) {
                if (n.getOperator() == Operator.binOr) {
                    nodes.add(new CodeNode(nested++, new BinaryOrExpr(n)));
                } else {
                    nodes.add(new CodeNode(nested++, n));
                }
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(BlockComment n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(BlockStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(BooleanLiteralExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(BreakStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(CastExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(CatchClause n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(CharLiteralExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ClassExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ClassOrInterfaceDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ClassOrInterfaceType n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(CompilationUnit n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ConditionalExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ConstructorDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ContinueStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(DoStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(DoubleLiteralExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(EmptyMemberDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(EmptyStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(EmptyTypeDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(EnclosedExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(EnumConstantDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(EnumDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ExplicitConstructorInvocationStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ExpressionStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(FieldAccessExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(FieldDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ForeachStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ForStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(IfStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ImportDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(InitializerDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(InstanceOfExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(IntegerLiteralExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(IntegerLiteralMinValueExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(JavadocComment n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(LabeledStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(LineComment n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(LongLiteralExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(LongLiteralMinValueExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(MarkerAnnotationExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(MemberValuePair n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(MethodCallExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(MethodDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(NameExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(NormalAnnotationExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(NullLiteralExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ObjectCreationExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(PackageDeclaration n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(Parameter n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(PrimitiveType n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(QualifiedNameExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ReferenceType n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ReturnStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(SingleMemberAnnotationExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(StringLiteralExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(SuperExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(SwitchEntryStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(SwitchStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(SynchronizedStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ThisExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(ThrowStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(TryStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(TypeDeclarationStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(TypeParameter n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(UnaryExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(VariableDeclarationExpr n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(VariableDeclarator n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(VariableDeclaratorId n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(VoidType n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(WhileStmt n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }

            @Override
            public void visit(WildcardType n, Object arg) {
                nodes.add(new CodeNode(nested++, n));
                super.visit(n, arg);
                nested--;
            }
        };
        cu.accept(visitor, null);
        return nodes;
    } catch (ParseException e) {
        throw new RuntimeException("invalid Java code: " + code + ". " + e);
    }
}
Also used : IntegerLiteralExpr(japa.parser.ast.expr.IntegerLiteralExpr) ObjectCreationExpr(japa.parser.ast.expr.ObjectCreationExpr) TypeParameter(japa.parser.ast.TypeParameter) ExplicitConstructorInvocationStmt(japa.parser.ast.stmt.ExplicitConstructorInvocationStmt) LineComment(japa.parser.ast.LineComment) ExpressionStmt(japa.parser.ast.stmt.ExpressionStmt) ReferenceType(japa.parser.ast.type.ReferenceType) VariableDeclarator(japa.parser.ast.body.VariableDeclarator) EnumConstantDeclaration(japa.parser.ast.body.EnumConstantDeclaration) QualifiedNameExpr(japa.parser.ast.expr.QualifiedNameExpr) BreakStmt(japa.parser.ast.stmt.BreakStmt) BooleanLiteralExpr(japa.parser.ast.expr.BooleanLiteralExpr) CastExpr(japa.parser.ast.expr.CastExpr) LongLiteralExpr(japa.parser.ast.expr.LongLiteralExpr) PrimitiveType(japa.parser.ast.type.PrimitiveType) NormalAnnotationExpr(japa.parser.ast.expr.NormalAnnotationExpr) PackageDeclaration(japa.parser.ast.PackageDeclaration) CompilationUnit(japa.parser.ast.CompilationUnit) BlockComment(japa.parser.ast.BlockComment) SwitchStmt(japa.parser.ast.stmt.SwitchStmt) BlockStmt(japa.parser.ast.stmt.BlockStmt) CharLiteralExpr(japa.parser.ast.expr.CharLiteralExpr) CatchClause(japa.parser.ast.stmt.CatchClause) ForeachStmt(japa.parser.ast.stmt.ForeachStmt) InstanceOfExpr(japa.parser.ast.expr.InstanceOfExpr) UnaryExpr(japa.parser.ast.expr.UnaryExpr) LinkedList(java.util.LinkedList) WildcardType(japa.parser.ast.type.WildcardType) EmptyMemberDeclaration(japa.parser.ast.body.EmptyMemberDeclaration) AnnotationDeclaration(japa.parser.ast.body.AnnotationDeclaration) ClassExpr(japa.parser.ast.expr.ClassExpr) AssertStmt(japa.parser.ast.stmt.AssertStmt) ReturnStmt(japa.parser.ast.stmt.ReturnStmt) ThrowStmt(japa.parser.ast.stmt.ThrowStmt) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter) LabeledStmt(japa.parser.ast.stmt.LabeledStmt) VoidType(japa.parser.ast.type.VoidType) ClassOrInterfaceDeclaration(japa.parser.ast.body.ClassOrInterfaceDeclaration) QualifiedNameExpr(japa.parser.ast.expr.QualifiedNameExpr) NameExpr(japa.parser.ast.expr.NameExpr) StringLiteralExpr(japa.parser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(japa.parser.ast.type.ClassOrInterfaceType) SynchronizedStmt(japa.parser.ast.stmt.SynchronizedStmt) FieldDeclaration(japa.parser.ast.body.FieldDeclaration) AssignExpr(japa.parser.ast.expr.AssignExpr) SingleMemberAnnotationExpr(japa.parser.ast.expr.SingleMemberAnnotationExpr) JavadocComment(japa.parser.ast.body.JavadocComment) VariableDeclaratorId(japa.parser.ast.body.VariableDeclaratorId) MemberValuePair(japa.parser.ast.expr.MemberValuePair) ArrayInitializerExpr(japa.parser.ast.expr.ArrayInitializerExpr) WhileStmt(japa.parser.ast.stmt.WhileStmt) ConstructorDeclaration(japa.parser.ast.body.ConstructorDeclaration) EmptyTypeDeclaration(japa.parser.ast.body.EmptyTypeDeclaration) TypeDeclarationStmt(japa.parser.ast.stmt.TypeDeclarationStmt) FieldAccessExpr(japa.parser.ast.expr.FieldAccessExpr) EmptyStmt(japa.parser.ast.stmt.EmptyStmt) ArrayCreationExpr(japa.parser.ast.expr.ArrayCreationExpr) ForStmt(japa.parser.ast.stmt.ForStmt) DoStmt(japa.parser.ast.stmt.DoStmt) ThisExpr(japa.parser.ast.expr.ThisExpr) VariableDeclarationExpr(japa.parser.ast.expr.VariableDeclarationExpr) SuperExpr(japa.parser.ast.expr.SuperExpr) IntegerLiteralMinValueExpr(japa.parser.ast.expr.IntegerLiteralMinValueExpr) ArrayAccessExpr(japa.parser.ast.expr.ArrayAccessExpr) MethodDeclaration(japa.parser.ast.body.MethodDeclaration) BinaryExpr(japa.parser.ast.expr.BinaryExpr) TryStmt(japa.parser.ast.stmt.TryStmt) MarkerAnnotationExpr(japa.parser.ast.expr.MarkerAnnotationExpr) ConditionalExpr(japa.parser.ast.expr.ConditionalExpr) EnumDeclaration(japa.parser.ast.body.EnumDeclaration) ContinueStmt(japa.parser.ast.stmt.ContinueStmt) NullLiteralExpr(japa.parser.ast.expr.NullLiteralExpr) IfStmt(japa.parser.ast.stmt.IfStmt) InitializerDeclaration(japa.parser.ast.body.InitializerDeclaration) LongLiteralMinValueExpr(japa.parser.ast.expr.LongLiteralMinValueExpr) DoubleLiteralExpr(japa.parser.ast.expr.DoubleLiteralExpr) ImportDeclaration(japa.parser.ast.ImportDeclaration) SwitchEntryStmt(japa.parser.ast.stmt.SwitchEntryStmt) Parameter(japa.parser.ast.body.Parameter) TypeParameter(japa.parser.ast.TypeParameter) ParseException(japa.parser.ParseException) EnclosedExpr(japa.parser.ast.expr.EnclosedExpr) AnnotationMemberDeclaration(japa.parser.ast.body.AnnotationMemberDeclaration) MethodCallExpr(japa.parser.ast.expr.MethodCallExpr)

Example 2 with VoidVisitorAdapter

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

the class JavaSyntaxTool method isValidMethDecl.

/**
	 * verify that line is a valid method declaration part, excluding method body and the {} 
	 * @param line: something like foo(int a, String b)
	 */
public static void isValidMethDecl(String line) {
    final String classTempForMeth = "class T {  %s{} }";
    String classString = String.format(classTempForMeth, line);
    try {
        CompilationUnit cu = parse(classString);
        VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
        };
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid method declaration: " + line + ". Was expecting something like foo(int a, String b).");
    }
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) ParseException(japa.parser.ParseException) VoidVisitorAdapter(japa.parser.ast.visitor.VoidVisitorAdapter)

Example 3 with VoidVisitorAdapter

use of japa.parser.ast.visitor.VoidVisitorAdapter in project Japid 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)

Example 4 with VoidVisitorAdapter

use of japa.parser.ast.visitor.VoidVisitorAdapter 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 5 with VoidVisitorAdapter

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

the class JavaSyntaxTool method isValidMethDecl.

/**
	 * verify that line is a valid method declaration part, excluding method body and the {} 
	 * @param line: something like foo(int a, String b)
	 */
public static void isValidMethDecl(String line) {
    final String classTempForMeth = "class T {  %s{} }";
    String classString = String.format(classTempForMeth, line);
    try {
        CompilationUnit cu = parse(classString);
        VoidVisitorAdapter visitor = new VoidVisitorAdapter() {
        };
        cu.accept(visitor, null);
    } catch (ParseException e) {
        throw new RuntimeException("the line does not seem to be a valid method declaration: " + line + ". Was expecting something like foo(int a, String b).");
    }
}
Also used : CompilationUnit(japa.parser.ast.CompilationUnit) 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