use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class ParsingSteps method thenMethodReferenceInStatementInMethodInClassIsScope.
@Then("method reference in statement $statementPosition in method $methodPosition in class $classPosition scope is $expectedName")
public void thenMethodReferenceInStatementInMethodInClassIsScope(int statementPosition, int methodPosition, int classPosition, String expectedName) {
ExpressionStmt statementUnderTest = getStatementInMethodInClass(statementPosition, methodPosition, classPosition).asExpressionStmt();
assertEquals(1, statementUnderTest.findAll(MethodReferenceExpr.class).size());
MethodReferenceExpr methodReferenceUnderTest = statementUnderTest.findFirst(MethodReferenceExpr.class).get();
assertThat(methodReferenceUnderTest.getScope().toString(), is(expectedName));
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class ParsingSteps method thenTheAssignExprProducedDoesntHaveANullTarget.
@Then("the assignExpr produced doesn't have a null target")
public void thenTheAssignExprProducedDoesntHaveANullTarget() {
CompilationUnit compilationUnit = (CompilationUnit) state.get("cu1");
ClassOrInterfaceDeclaration classDeclaration = compilationUnit.getType(0).asClassOrInterfaceDeclaration();
ConstructorDeclaration ctor = classDeclaration.getMember(1).asConstructorDeclaration();
ExpressionStmt assignStmt = ctor.getBody().getStatement(0).asExpressionStmt();
AssignExpr assignExpr = assignStmt.getExpression().asAssignExpr();
assertNotNull(assignExpr.getTarget());
assertEquals(NameExpr.class, assignExpr.getTarget().getClass());
assertEquals(assignExpr.getTarget().asNameExpr().getNameAsString(), "mString");
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class ParsingSteps method thenLambdaInMethodCallInStatementInMethodInClassBody.
@Then("lambda in method call in statement $statementPosition in method $methodPosition in class $classPosition body is \"$expectedBody\"")
public void thenLambdaInMethodCallInStatementInMethodInClassBody(int statementPosition, int methodPosition, int classPosition, String expectedBody) {
ExpressionStmt statement = getStatementInMethodInClass(statementPosition, methodPosition, classPosition).asExpressionStmt();
VariableDeclarationExpr variableDeclarationExpr = statement.getExpression().asVariableDeclarationExpr();
VariableDeclarator variableDeclarator = variableDeclarationExpr.getVariable(0);
MethodCallExpr methodCallExpr = (MethodCallExpr) variableDeclarator.getInitializer().orElse(null);
CastExpr castExpr = methodCallExpr.getArgument(0).asCastExpr();
LambdaExpr lambdaExpr = castExpr.getExpression().asLambdaExpr();
assertThat(lambdaExpr.getBody().toString(), is(expectedBody));
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class DumpVisitor method visit.
@Override
public void visit(LambdaExpr n, Object arg) {
printJavaComment(n.getComment(), arg);
List<Parameter> parameters = n.getParameters();
boolean printPar = false;
printPar = n.isParametersEnclosed();
if (printPar) {
printer.print("(");
}
if (parameters != null) {
for (Iterator<Parameter> i = parameters.iterator(); i.hasNext(); ) {
Parameter p = i.next();
p.accept(this, arg);
if (i.hasNext()) {
printer.print(", ");
}
}
}
if (printPar) {
printer.print(")");
}
printer.print(" -> ");
Statement body = n.getBody();
if (body instanceof ExpressionStmt) {
// Print the expression directly
((ExpressionStmt) body).getExpression().accept(this, arg);
} else {
body.accept(this, arg);
}
}
use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.
the class Issue257 method issue257.
@Test
public void issue257() throws FileNotFoundException {
String pathToSourceFile = adaptPath("src/test/resources/issue257/A.java.txt");
CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile));
Statement statement = cu.getClassByName("A").get().getMethodsByName("run").get(0).getBody().get().getStatement(0);
ExpressionStmt expressionStmt = (ExpressionStmt) statement;
Expression expression = expressionStmt.getExpression();
JavaParserFacade.get(typeSolver).getType(expression);
}
Aggregations