Search in sources :

Example 16 with MethodDeclaration

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);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.Test)

Example 17 with MethodDeclaration

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)));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.Test)

Example 18 with MethodDeclaration

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());
}
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 19 with MethodDeclaration

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);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodContext) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 20 with MethodDeclaration

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)325 Test (org.junit.Test)166 CompilationUnit (com.github.javaparser.ast.CompilationUnit)143 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)107 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)100 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)84 Expression (com.github.javaparser.ast.expr.Expression)82 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)77 NameExpr (com.github.javaparser.ast.expr.NameExpr)58 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)57 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)47 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)46 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)41 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)40 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)38 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)35 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)35 NodeList (com.github.javaparser.ast.NodeList)32 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)27