Search in sources :

Example 26 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class GetMetaModelGenerator method generateNode.

@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
    final MethodDeclaration getMetaModelMethod = (MethodDeclaration) parseBodyDeclaration(f("%s public %s getMetaModel() { return JavaParserMetaModel.%s; }", nodeMetaModel.isRootNode() ? "" : "@Override", nodeMetaModel.getClass().getSimpleName(), nodeMetaModel.getMetaModelFieldName()));
    addOrReplaceWhenSameSignature(nodeCoid, getMetaModelMethod);
    nodeCu.addImport(nodeMetaModel.getClass().getName());
    nodeCu.addImport(JavaParserMetaModel.class);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration)

Example 27 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class PropertyGenerator method generateGetter.

private void generateGetter(BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
    final MethodDeclaration getter = new MethodDeclaration(EnumSet.of(PUBLIC), parseType(property.getTypeNameForGetter()), property.getGetterMethodName());
    final BlockStmt body = getter.getBody().get();
    body.getStatements().clear();
    if (property.isOptional()) {
        body.addStatement(f("return Optional.ofNullable(%s);", property.getName()));
    } else {
        body.addStatement(f("return %s;", property.getName()));
    }
    replaceWhenSameSignature(nodeCoid, getter);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 28 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class JavaParserTest method rangeOfLambdaBody.

@Test
public void rangeOfLambdaBody() {
    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 = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
    CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
    LambdaExpr lambdaExpr = castExpr.getExpression().asLambdaExpr();
    Statement lambdaBody = lambdaExpr.getBody();
    assertEquals(range(3, 68, 3, 101), lambdaBody.getRange().get());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Example 29 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class JavaParserTest method rangeOfCastNonIntersection.

@Test
public void rangeOfCastNonIntersection() {
    String code = "class A {" + EOL + "  Object f() {" + EOL + "    return (Comparator<Map.Entry<K, V>>               )(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
    CompilationUnit cu = JavaParser.parse(code);
    MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
    CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
    assertEquals(range(3, 12, 3, 101), castExpr.getRange().get());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CastExpr(com.github.javaparser.ast.expr.CastExpr) Test(org.junit.Test)

Example 30 with MethodDeclaration

use of com.github.javaparser.ast.body.MethodDeclaration in project javaparser by javaparser.

the class JavaParserTest method rangeOfCast.

@Test
public void rangeOfCast() {
    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 = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
    CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
    assertEquals(range(3, 12, 3, 101), castExpr.getRange().get());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CastExpr(com.github.javaparser.ast.expr.CastExpr) 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