Search in sources :

Example 11 with ConstructorMetadata

use of org.springframework.roo.classpath.details.ConstructorMetadata in project spring-roo by spring-projects.

the class ItdSourceFileComposer method appendConstructors.

private void appendConstructors() {
    final List<? extends ConstructorMetadata> constructors = itdTypeDetails.getDeclaredConstructors();
    if (constructors == null || constructors.isEmpty()) {
        return;
    }
    content = true;
    for (final ConstructorMetadata constructor : constructors) {
        Validate.isTrue(constructor.getParameterTypes().size() == constructor.getParameterNames().size(), "Mismatched parameter names against parameter types");
        // ROO-3447: Append comments if exists
        CommentStructure commentStructure = constructor.getCommentStructure();
        if (commentStructure != null && commentStructure.getBeginComments() != null) {
            List<AbstractComment> constructorComments = commentStructure.getBeginComments();
            String comment = "";
            boolean missingComponentsAdded = false;
            for (AbstractComment constructorComment : constructorComments) {
                // Join all JavadocComment's
                if (constructorComment instanceof JavadocComment) {
                    // Add JavaDoc missing components
                    if (!missingComponentsAdded) {
                        if (!constructor.getParameterNames().isEmpty() && ((JavadocComment) constructorComment).getParamsInfo() == null) {
                            List<String> paramsInfo = new ArrayList<String>();
                            for (JavaSymbolName name : constructor.getParameterNames()) {
                                paramsInfo.add(name.getSymbolName());
                            }
                            ((JavadocComment) constructorComment).setParamsInfo(paramsInfo);
                        }
                    }
                    missingComponentsAdded = true;
                    comment = comment.concat(constructorComment.getComment()).concat(IOUtils.LINE_SEPARATOR);
                } else {
                    // Not JavadocComment, write comment as it is, unchanged
                    appendFormalLine(constructorComment.getComment());
                }
            }
            // Write JavadocComment's to ITD, in a single JavadocComment instance
            String[] commentLines = comment.split(IOUtils.LINE_SEPARATOR);
            for (String commentLine : commentLines) {
                appendFormalLine(commentLine);
            }
        } else {
            // ROO-3834: Append default Javadoc if not exists a comment structure,
            // including constructor params
            CommentStructure defaultCommentStructure = new CommentStructure();
            List<String> parameterNames = new ArrayList<String>();
            for (JavaSymbolName name : constructor.getParameterNames()) {
                parameterNames.add(name.getSymbolName());
            }
            JavadocComment javadocComment = new JavadocComment("TODO Auto-generated constructor documentation", parameterNames, null, null);
            defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
            constructor.setCommentStructure(defaultCommentStructure);
            // Now lines should be formatted, so write them
            String[] comment = javadocComment.getComment().split(IOUtils.LINE_SEPARATOR);
            for (String line : comment) {
                appendFormalLine(line);
            }
        }
        // Append annotations
        for (final AnnotationMetadata annotation : constructor.getAnnotations()) {
            appendIndent();
            outputAnnotation(annotation);
            this.newLine(false);
        }
        // Append "<modifier> <TargetOfIntroduction>.new" portion
        appendIndent();
        if (constructor.getModifier() != 0) {
            append(Modifier.toString(constructor.getModifier()));
            append(" ");
        }
        append(introductionTo.getSimpleTypeName());
        append(".");
        append("new");
        // Append parameter types and names
        append("(");
        final List<AnnotatedJavaType> parameterTypes = constructor.getParameterTypes();
        final List<JavaSymbolName> parameterNames = constructor.getParameterNames();
        for (int i = 0; i < parameterTypes.size(); i++) {
            final AnnotatedJavaType paramType = parameterTypes.get(i);
            final JavaSymbolName paramName = parameterNames.get(i);
            for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
                append(AnnotationMetadataUtils.toSourceForm(methodParameterAnnotation, resolver));
                append(" ");
            }
            append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
            append(" ");
            append(paramName.getSymbolName());
            if (i < parameterTypes.size() - 1) {
                append(", ");
            }
        }
        append(") {");
        this.newLine(false);
        indent();
        // Add body
        append(constructor.getBody());
        indentRemove();
        appendFormalLine("}");
        this.newLine(false);
    }
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AbstractComment(org.springframework.roo.classpath.details.comments.AbstractComment)

Example 12 with ConstructorMetadata

use of org.springframework.roo.classpath.details.ConstructorMetadata in project spring-roo by spring-projects.

the class ClasspathOperationsImpl method createConstructor.

@Override
public void createConstructor(final JavaType name, final Set<String> fields) {
    final ClassOrInterfaceTypeDetails javaTypeDetails = typeLocationService.getTypeDetails(name);
    Validate.notNull(javaTypeDetails, "The type specified, '%s', doesn't exist", name.getFullyQualifiedTypeName());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, pathResolver.getFocusedPath(Path.SRC_MAIN_JAVA));
    final List<FieldMetadata> constructorFields = new ArrayList<FieldMetadata>();
    final List<? extends FieldMetadata> declaredFields = javaTypeDetails.getDeclaredFields();
    if (fields != null) {
        for (final String field : fields) {
            declared: for (final FieldMetadata declaredField : declaredFields) {
                if (field.equals(declaredField.getFieldName().getSymbolName())) {
                    constructorFields.add(declaredField);
                    break declared;
                }
            }
        }
        if (constructorFields.isEmpty()) {
            // class, so return without creating any constructor
            throw new IllegalArgumentException(String.format("The set of fields provided for the constructor does not exist in the class '%s'", name));
        }
    }
    // Search for an existing constructor
    final List<JavaType> parameterTypes = new ArrayList<JavaType>();
    for (final FieldMetadata fieldMetadata : constructorFields) {
        parameterTypes.add(fieldMetadata.getFieldType());
    }
    final ConstructorMetadata result = javaTypeDetails.getDeclaredConstructor(parameterTypes);
    if (result != null) {
        // Found an existing constructor on this class
        throw new IllegalArgumentException(String.format("The class '%s' already has a constructor method with the same arguments and it cannot " + "be created. Use '--force' parameter to overrite it.", name));
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("super();");
    for (final FieldMetadata field : constructorFields) {
        final String fieldName = field.getFieldName().getSymbolName();
        bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
        parameterNames.add(field.getFieldName());
    }
    // Create the constructor
    final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(declaredByMetadataId);
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
    constructorBuilder.setParameterNames(parameterNames);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(javaTypeDetails);
    cidBuilder.addConstructor(constructorBuilder);
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Aggregations

ConstructorMetadata (org.springframework.roo.classpath.details.ConstructorMetadata)12 JavaType (org.springframework.roo.model.JavaType)9 ArrayList (java.util.ArrayList)8 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)8 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)8 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)6 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)6 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)5 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)5 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)4 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)4 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)3 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)3 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)3 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)3 ConstructorMetadataBuilder (org.springframework.roo.classpath.details.ConstructorMetadataBuilder)3 MemberHoldingTypeDetails (org.springframework.roo.classpath.details.MemberHoldingTypeDetails)3 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)3 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)3 ImportDeclaration (com.github.antlrjavaparser.api.ImportDeclaration)2