use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class TransformationsTest method exampleParam4.
@Test
public void exampleParam4() throws IOException {
considerExample("Example_param3_original");
MethodDeclaration md = (MethodDeclaration) cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
md.getParameters().remove(1);
assertTransformed("Example_param4", cu);
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class PrettyPrintVisitorTest method printLambdaIntersectionTypeReturn.
@Test
public void printLambdaIntersectionTypeReturn() {
String code = "class A {" + EOL + " Object f() {" + EOL + " return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
CompilationUnit cu = JavaParser.parse(code);
MethodDeclaration methodDeclaration = (MethodDeclaration) cu.getType(0).getMember(0);
assertEquals("return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey());", print(methodDeclaration.getBody().get().getStatements().get(0)));
}
use of com.github.javaparser.ast.body.MethodDeclaration 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.body.MethodDeclaration in project javaparser by javaparser.
the class Issue276 method testSolveStaticallyImportedMemberType.
@Test
public void testSolveStaticallyImportedMemberType() throws FileNotFoundException {
CompilationUnit cu = JavaParser.parse(new File(adaptPath("src/test/resources/issue276/foo/C.java")));
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "C");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(adaptPath(new File("src/test/resources/issue276"))));
List<MethodDeclaration> methods = cls.findAll(MethodDeclaration.class);
boolean isSolved = false;
for (MethodDeclaration method : methods) {
if (method.getNameAsString().equals("overrideMe")) {
MethodContext context = new MethodContext(method, typeSolver);
isSolved = context.solveType("FindMeIfYouCan", typeSolver).isSolved();
}
}
Assert.assertTrue(isSolved);
}
use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.
the class Issue186 method lambdaPrimitivesIssue.
@Test
public void lambdaPrimitivesIssue() {
CompilationUnit cu = parseSample("Issue186");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "bar");
List<LambdaExpr> lambdas = methodDeclaration.findAll(LambdaExpr.class);
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
assertEquals("java.util.function.Predicate<? super java.lang.String>", javaParserFacade.getType(lambdas.get(0)).describe());
assertEquals("java.util.function.Function<? super java.lang.String, ? extends java.lang.Integer>", javaParserFacade.getType(lambdas.get(1)).describe());
assertEquals("java.util.function.Predicate<? super java.lang.Integer>", javaParserFacade.getType(lambdas.get(2)).describe());
}
Aggregations