Search in sources :

Example 36 with MethodDeclaration

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

the class ArrayTypeTest method setMethodDeclarationWithArrays.

@Test
public void setMethodDeclarationWithArrays() {
    MethodDeclaration method = parseBodyDeclaration("int[][] a()[][] {}").asMethodDeclaration();
    method.setType(new ArrayType(new ArrayType(parseClassOrInterfaceType("Blob"))));
    assertEquals("Blob[][] a() {" + EOL + "}", method.toString());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.Test)

Example 37 with MethodDeclaration

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

the class ArrayTypeTest method setParameterWithArrays.

@Test
public void setParameterWithArrays() {
    MethodDeclaration method = parseBodyDeclaration("void a(int[][] a[][]) {}").asMethodDeclaration();
    method.getParameter(0).setType(new ArrayType(new ArrayType(parseClassOrInterfaceType("Blob"))));
    assertEquals("void a(Blob[][] a) {" + EOL + "}", method.toString());
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.Test)

Example 38 with MethodDeclaration

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

the class ArrayTypeTest method getMethodDeclarationWithArrays.

@Test
public void getMethodDeclarationWithArrays() {
    MethodDeclaration methodDeclaration = parseBodyDeclaration("@C int @A[] a() @B[] {}").asMethodDeclaration();
    ArrayType arrayType1 = methodDeclaration.getType().asArrayType();
    ArrayType arrayType2 = arrayType1.getComponentType().asArrayType();
    Type elementType = arrayType2.getComponentType();
    assertThat(elementType).isInstanceOf(PrimitiveType.class);
    assertThat(arrayType1.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("A")));
    assertThat(arrayType2.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("B")));
    assertThat(methodDeclaration.getAnnotations()).containsExactly(new MarkerAnnotationExpr(parseName("C")));
    assertThat(methodDeclaration.getType().getParentNode().get()).isSameAs(methodDeclaration);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Test(org.junit.Test)

Example 39 with MethodDeclaration

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

the class RemoveMethodGenerator method generateNode.

@Override
protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
    MethodDeclaration removeNodeMethod = (MethodDeclaration) parseBodyDeclaration("public boolean remove(Node node) {}");
    nodeCu.addImport(Node.class);
    nodeMetaModel.getSuperNodeMetaModel().ifPresent(s -> annotateOverridden(removeNodeMethod));
    final BlockStmt body = removeNodeMethod.getBody().get();
    body.addStatement("if (node == null) return false;");
    for (PropertyMetaModel property : nodeMetaModel.getDeclaredPropertyMetaModels()) {
        if (!property.isNode()) {
            continue;
        }
        String check;
        if (property.isNodeList()) {
            check = nodeListCheck(property);
        } else {
            if (property.isRequired()) {
                continue;
            }
            String removeAttributeMethodName = generateRemoveMethodForAttribute(nodeCoid, nodeMetaModel, property);
            check = attributeCheck(property, removeAttributeMethodName);
        }
        if (property.isOptional()) {
            check = f("if (%s != null) { %s }", property.getName(), check);
        }
        body.addStatement(check);
    }
    if (nodeMetaModel.getSuperNodeMetaModel().isPresent()) {
        body.addStatement("return super.remove(node);");
    } else {
        body.addStatement("return false;");
    }
    addOrReplaceWhenSameSignature(nodeCoid, removeNodeMethod);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PropertyMetaModel(com.github.javaparser.metamodel.PropertyMetaModel)

Example 40 with MethodDeclaration

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

the class RemoveMethodGenerator method generateRemoveMethodForAttribute.

private String generateRemoveMethodForAttribute(ClassOrInterfaceDeclaration nodeCoid, BaseNodeMetaModel nodeMetaModel, PropertyMetaModel property) {
    final String methodName = "remove" + capitalize(property.getName());
    final MethodDeclaration removeMethod = (MethodDeclaration) parseBodyDeclaration(f("public %s %s() {}", nodeMetaModel.getTypeName(), methodName));
    final BlockStmt block = removeMethod.getBody().get();
    block.addStatement(f("return %s((%s) null);", property.getSetterMethodName(), property.getTypeNameForSetter()));
    addOrReplaceWhenSameSignature(nodeCoid, removeMethod);
    return methodName;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

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