Search in sources :

Example 1 with ExpressionStmt

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));
}
Also used : ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Then(org.jbehave.core.annotations.Then)

Example 2 with ExpressionStmt

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");
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Then(org.jbehave.core.annotations.Then)

Example 3 with ExpressionStmt

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));
}
Also used : ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Then(org.jbehave.core.annotations.Then)

Example 4 with ExpressionStmt

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);
    }
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) TypeParameter(com.github.javaparser.ast.TypeParameter) Parameter(com.github.javaparser.ast.body.Parameter) MultiTypeParameter(com.github.javaparser.ast.body.MultiTypeParameter) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 5 with ExpressionStmt

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);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) Statement(com.github.javaparser.ast.stmt.Statement) File(java.io.File) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Aggregations

ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)27 Test (org.junit.Test)19 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)11 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)11 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)10 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)8 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)8 Expression (com.github.javaparser.ast.expr.Expression)7 Statement (com.github.javaparser.ast.stmt.Statement)6 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)5 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)5 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)5 Parameter (com.github.javaparser.ast.body.Parameter)3 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)3 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)3 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)3 Then (org.jbehave.core.annotations.Then)3 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)2