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());
}
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;
}
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());
}
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());
}
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());
}
Aggregations