Search in sources :

Example 16 with Expression

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

the class Issue144 method issue144WithReflectionTypeSolver.

@Test
public void issue144WithReflectionTypeSolver() {
    CompilationUnit cu = parseSampleWithStandardExtension("issue144/HelloWorld");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "HelloWorld");
    ExpressionStmt expressionStmt = (ExpressionStmt) clazz.getMethodsByName("main").get(0).getBody().get().getStatement(0);
    MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
    Expression firstParameter = methodCallExpr.getArgument(0);
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver(true));
    assertEquals(true, javaParserFacade.solve(firstParameter).isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 17 with Expression

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

the class JavaParser method parseExpression.

/**
 * Parses the Java expression contained in a {@link String} and returns a
 * {@link Expression} that represents it.
 *
 * @param expression
 *            {@link String} containing Java expression
 * @return Expression representing the Java expression
 * @throws ParseException
 *             if the source code has parser errors
 */
public static Expression parseExpression(final String expression) {
    StringReader sr = new StringReader(expression);
    Expression e = new ASTParser(sr).Expression();
    sr.close();
    return e;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) StringReader(java.io.StringReader) ASTParser(com.github.javaparser.ASTParser)

Example 18 with Expression

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

the class NodeTest method typeOnlyFindAllMatchesSubclasses.

@Test
public void typeOnlyFindAllMatchesSubclasses() {
    Expression e = parseExpression("1+2+3");
    List<Node> ints = e.findAll(Node.class);
    assertEquals("[1 + 2 + 3, 1 + 2, 1, 2, 3]", ints.toString());
}
Also used : JavaParser.parseExpression(com.github.javaparser.JavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) Test(org.junit.Test)

Example 19 with Expression

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

the class NodeTest method stream.

@Test
public void stream() {
    Expression e = parseExpression("1+2+3");
    List<IntegerLiteralExpr> ints = e.stream().filter(n -> n instanceof IntegerLiteralExpr).map(IntegerLiteralExpr.class::cast).filter(i -> i.asInt() > 1).collect(Collectors.toList());
    assertEquals("[2, 3]", ints.toString());
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SimpleName(com.github.javaparser.ast.expr.SimpleName) java.util(java.util) AstObserver(com.github.javaparser.ast.observer.AstObserver) EOL(com.github.javaparser.utils.Utils.EOL) Test(org.junit.Test) JavaParser.parse(com.github.javaparser.JavaParser.parse) Collectors(java.util.stream.Collectors) ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) LineComment(com.github.javaparser.ast.comments.LineComment) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) JavaParser.parseExpression(com.github.javaparser.JavaParser.parseExpression) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) Expression(com.github.javaparser.ast.expr.Expression) Comment(com.github.javaparser.ast.comments.Comment) Assert(org.junit.Assert) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BlockComment(com.github.javaparser.ast.comments.BlockComment) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) JavaParser.parseExpression(com.github.javaparser.JavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) Test(org.junit.Test)

Example 20 with Expression

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

the class NodeTest method classSpecificWalk.

@Test
public void classSpecificWalk() {
    Expression e = parseExpression("1+1");
    StringBuilder b = new StringBuilder();
    e.walk(IntegerLiteralExpr.class, n -> b.append(n.toString()));
    assertEquals("11", b.toString());
}
Also used : JavaParser.parseExpression(com.github.javaparser.JavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) 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