Search in sources :

Example 6 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class DifferenceTest method addingStatementToEmptyMethodBody.

@Test
public void addingStatementToEmptyMethodBody() {
    String code = "class A { void foo(char p1, int p2) {} }";
    considerCode(code);
    Statement s = new ExpressionStmt(new BinaryExpr(new IntegerLiteralExpr("10"), new IntegerLiteralExpr("2"), BinaryExpr.Operator.PLUS));
    MethodDeclaration m = cu.getClassByName("A").get().getMethodsByName("foo").get(0);
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(m.getBody().get());
    LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterListAddition(m.getBody().get(), ObservableProperty.STATEMENTS, 0, s);
    Difference diff = Difference.calculate(csmOriginal, csmChanged);
    int i = 0;
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmIndent()), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmChild(s)), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.added(new CsmUnindent()), diff.getElements().get(i++));
    assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
    assertEquals(i, diff.getElements().size());
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CsmIndent(com.github.javaparser.printer.concretesyntaxmodel.CsmIndent) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) CsmUnindent(com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent) Test(org.junit.Test)

Example 7 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class LexicalDifferenceCalculatorTest method differenceAfterddingStatementToEmptyBlock.

@Test
public void differenceAfterddingStatementToEmptyBlock() throws IOException {
    LexicalDifferenceCalculator ldc = new LexicalDifferenceCalculator();
    considerExample("ASimpleClassWithMoreFormatting_step3");
    MethodDeclaration setter = cu.getClassByName("MyRenamedClass").get().getMethodsByName("setAField").get(0);
    Statement assignStatement = new ExpressionStmt(new AssignExpr(new FieldAccessExpr(new ThisExpr(), "aField"), new NameExpr("aField"), AssignExpr.Operator.ASSIGN));
    Difference diff = ldc.calculateListAdditionDifference(ObservableProperty.STATEMENTS, setter.getBody().get().getStatements(), 0, assignStatement);
    int index = 0;
    assertEquals(Difference.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.kept(CsmElement.newline()), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.added(CsmElement.indent()), diff.getElements().get(index++));
    assertTrue(isAddedChild(diff.getElements().get(index++), ExpressionStmt.class));
    assertEquals(Difference.DifferenceElement.added(CsmElement.newline()), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.added(CsmElement.unindent()), diff.getElements().get(index++));
    assertEquals(Difference.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(index++));
    assertEquals(index, diff.getElements().size());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Test(org.junit.Test)

Example 8 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class Issue144 method issue144.

@Test(expected = UnsolvedSymbolException.class)
public void issue144() {
    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(typeSolver);
    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) 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 9 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt 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 10 with ExpressionStmt

use of com.github.javaparser.ast.stmt.ExpressionStmt in project javaparser by javaparser.

the class Issue128 method loopOnStaticallyImportedType.

@Test
public void loopOnStaticallyImportedType() {
    CompilationUnit cu = parseSampleWithStandardExtension("issue128/foo/Issue128");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
    ExpressionStmt expressionStmt = (ExpressionStmt) clazz.getMethodsByName("test").get(0).getBody().get().getStatement(0);
    MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    assertEquals(false, javaParserFacade.solve(methodCallExpr).isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) 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)

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