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