Search in sources :

Example 26 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class TreeVisitorTest method isValidBreadthFirstTraversal.

@Test
public void isValidBreadthFirstTraversal() {
    Expression expression = JavaParser.parseExpression("(2+3)+(4+5)");
    StringBuilder result = new StringBuilder();
    TreeVisitor visitor = new TreeVisitor() {

        @Override
        public void process(Node node) {
            result.append("<").append(node).append("> ");
        }
    };
    visitor.visitBreadthFirst(expression);
    assertEquals("<(2 + 3) + (4 + 5)> <(2 + 3)> <(4 + 5)> <2 + 3> <4 + 5> <2> <3> <4> <5> ", result.toString());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Example 27 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class TreeVisitorTest method isValidDirectChildrenTraversal.

@Test
public void isValidDirectChildrenTraversal() {
    Expression expression = JavaParser.parseExpression("(2+3)+(4+5)");
    StringBuilder result = new StringBuilder();
    TreeVisitor visitor = new TreeVisitor() {

        @Override
        public void process(Node node) {
            result.append("<").append(node).append("> ");
        }
    };
    visitor.visitDirectChildren(expression);
    assertEquals("<(2 + 3)> <(4 + 5)> ", result.toString());
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) Node(com.github.javaparser.ast.Node) Test(org.junit.Test)

Example 28 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class ContextTest method resolveReferenceToLambdaParam.

@Test
public void resolveReferenceToLambdaParam() throws ParseException, IOException {
    CompilationUnit cu = parseSample("Navigator");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
    MethodCallExpr callToGetName = Navigator.findMethodCall(method, "getName").get();
    Expression referenceToT = callToGetName.getScope().get();
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
    ResolvedType typeOfT = JavaParserFacade.get(typeSolver).getType(referenceToT);
    assertEquals("? super com.github.javaparser.ast.body.TypeDeclaration", typeOfT.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Expression(com.github.javaparser.ast.expr.Expression) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 29 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class ContextTest method resolveLambdaType.

@Test
public void resolveLambdaType() throws ParseException, IOException {
    CompilationUnit cu = parseSample("Navigator");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
    MethodCallExpr callToFilter = Navigator.findMethodCall(method, "filter").get();
    Expression lambdaExpr = callToFilter.getArguments().get(0);
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
    ResolvedType typeOfLambdaExpr = JavaParserFacade.get(typeSolver).getType(lambdaExpr);
    assertEquals("java.util.function.Predicate<? super com.github.javaparser.ast.body.TypeDeclaration>", typeOfLambdaExpr.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Expression(com.github.javaparser.ast.expr.Expression) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 30 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class EnumResolutionTest method switchOnEnum.

@Test
public void switchOnEnum() {
    CompilationUnit cu = parseSample("SwitchOnEnum");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SwitchOnEnum");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
    SwitchStmt switchStmt = Navigator.findSwitch(method);
    Expression expression = switchStmt.getEntries().get(0).getLabel().get();
    SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
    assertTrue(ref.isSolved());
    assertEquals("SwitchOnEnum.MyEnum", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43