Search in sources :

Example 1 with AbstractComment

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

the class ItdSourceFileComposer method writeMethods.

private void writeMethods(final List<? extends MethodMetadata> methods, final boolean defineTarget, final boolean isInterfaceMethod) {
    for (final MethodMetadata method : methods) {
        Validate.isTrue(method.getParameterTypes().size() == method.getParameterNames().size(), "Method %s has mismatched parameter names against parameter types", method.getMethodName().getSymbolName());
        // ROO-3447: Append comments if exists
        CommentStructure commentStructure = method.getCommentStructure();
        if (commentStructure != null && commentStructure.getBeginComments() != null) {
            List<AbstractComment> comments = commentStructure.getBeginComments();
            String commentString = "";
            boolean missingComponentsAdded = false;
            for (AbstractComment comment : comments) {
                // Join all JavadocComment's
                if (comment instanceof JavadocComment) {
                    // Add JavaDoc missing components
                    if (!missingComponentsAdded) {
                        // Check params info
                        if (!method.getParameterNames().isEmpty() && ((JavadocComment) comment).getParamsInfo() == null) {
                            List<String> paramsInfo = new ArrayList<String>();
                            for (JavaSymbolName name : method.getParameterNames()) {
                                paramsInfo.add(name.getSymbolName());
                            }
                            ((JavadocComment) comment).setParamsInfo(paramsInfo);
                        }
                        // Check return info
                        if (!method.getReturnType().equals(JavaType.VOID_OBJECT) && !method.getReturnType().equals(JavaType.VOID_PRIMITIVE) && ((JavadocComment) comment).getReturnInfo() == null) {
                            ((JavadocComment) comment).setReturnInfo(method.getReturnType().getSimpleTypeName());
                        }
                        // Check throws info
                        if (!method.getThrowsTypes().isEmpty() && ((JavadocComment) comment).getThrowsInfo() == null) {
                            List<String> throwsInfo = new ArrayList<String>();
                            for (JavaType throwsType : method.getThrowsTypes()) {
                                throwsInfo.add(throwsType.getSimpleTypeName());
                            }
                            ((JavadocComment) comment).setThrowsInfo(throwsInfo);
                        }
                        missingComponentsAdded = true;
                    }
                    commentString = commentString.concat(comment.getComment()).concat(IOUtils.LINE_SEPARATOR);
                } else {
                    // Not JavadocComment, write comment as it is, unchanged
                    appendFormalLine(comment.getComment());
                }
            }
            // Write JavadocComment's to ITD, in a single JavadocComment instance
            String[] commentLines = commentString.split(IOUtils.LINE_SEPARATOR);
            for (String commentLine : commentLines) {
                appendFormalLine(commentLine);
            }
        } else {
            // ROO-3834: Append default Javadoc if not exists a comment structure,
            // including params, return and throws
            CommentStructure defaultCommentStructure = new CommentStructure();
            List<String> parameterNames = new ArrayList<String>();
            for (JavaSymbolName name : method.getParameterNames()) {
                parameterNames.add(name.getSymbolName());
            }
            List<String> throwsTypesNames = new ArrayList<String>();
            for (JavaType type : method.getThrowsTypes()) {
                throwsTypesNames.add(type.getSimpleTypeName());
            }
            String returnInfo = null;
            JavaType returnType = method.getReturnType();
            if (!returnType.equals(JavaType.VOID_OBJECT) && !returnType.equals(JavaType.VOID_PRIMITIVE)) {
                returnInfo = returnType.getSimpleTypeName();
            }
            JavadocComment javadocComment = new JavadocComment("TODO Auto-generated method documentation", parameterNames, returnInfo, throwsTypesNames);
            defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
            method.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 : method.getAnnotations()) {
            appendIndent();
            outputAnnotation(annotation);
            this.newLine(false);
        }
        // Append "<modifier> <genericDefinition> <returnType> <methodName>" portion
        appendIndent();
        // modifier
        if (method.getModifier() != 0) {
            append(Modifier.toString(method.getModifier()));
            append(" ");
        }
        // ROO-3648: genericDefinition
        if (method.getGenericDefinition() != null && !method.getGenericDefinition().trim().equals("")) {
            append("<".concat(method.getGenericDefinition()).concat(">"));
            append(" ");
        }
        // return type
        append(method.getReturnType().getNameIncludingTypeParameters(false, resolver));
        append(" ");
        if (defineTarget) {
            append(introductionTo.getSimpleTypeName());
            append(".");
        }
        append(method.getMethodName().getSymbolName());
        // Append parameter types and names
        append("(");
        final List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
        final List<JavaSymbolName> parameterNames = method.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()) {
                outputAnnotation(methodParameterAnnotation);
                append(" ");
            }
            append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
            append(" ");
            append(paramName.getSymbolName());
            if (i < parameterTypes.size() - 1) {
                append(", ");
            }
        }
        // Add exceptions to be thrown
        final List<JavaType> throwsTypes = method.getThrowsTypes();
        if (throwsTypes.size() > 0) {
            append(") throws ");
            for (int i = 0; i < throwsTypes.size(); i++) {
                append(throwsTypes.get(i).getNameIncludingTypeParameters(false, resolver));
                if (throwsTypes.size() > i + 1) {
                    append(", ");
                }
            }
        } else {
            append(")");
        }
        if (isInterfaceMethod) {
            append(";");
            this.newLine(false);
        } else {
            append(" {");
            this.newLine(false);
            // Add body
            indent();
            append(method.getBody());
            indentRemove();
            appendFormalLine("}");
        }
        this.newLine();
    }
}
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) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) AbstractComment(org.springframework.roo.classpath.details.comments.AbstractComment)

Example 2 with AbstractComment

use of org.springframework.roo.classpath.details.comments.AbstractComment 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 3 with AbstractComment

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

the class ItdSourceFileComposer method appendFields.

private void appendFields() {
    final List<? extends FieldMetadata> fields = itdTypeDetails.getDeclaredFields();
    if (fields == null || fields.isEmpty()) {
        return;
    }
    content = true;
    for (final FieldMetadata field : fields) {
        // ROO-3447: Append comments if exists
        CommentStructure commentStructure = field.getCommentStructure();
        if (commentStructure != null && commentStructure.getBeginComments() != null) {
            List<AbstractComment> comments = commentStructure.getBeginComments();
            String commentString = "";
            for (AbstractComment comment : comments) {
                // Join all JavadocComment's
                if (comment instanceof JavadocComment) {
                    commentString = commentString.concat(comment.getComment()).concat(IOUtils.LINE_SEPARATOR);
                } else {
                    // Not JavadocComment, write comment as it is, unchanged
                    appendFormalLine(comment.getComment());
                }
            }
            // Write JavadocComment's to ITD, in a single JavadocComment instance
            String[] commentLines = commentString.split(IOUtils.LINE_SEPARATOR);
            for (String commentLine : commentLines) {
                appendFormalLine(commentLine);
            }
        } else {
            // ROO-3834: Append default Javadoc if not exists a comment structure
            JavadocComment javadocComment = new JavadocComment("TODO Auto-generated attribute documentation");
            // 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 : field.getAnnotations()) {
            appendIndent();
            outputAnnotation(annotation);
            this.newLine(false);
        }
        // Append "<modifier> <fieldType> <fieldName>" portion
        appendIndent();
        if (field.getModifier() != 0) {
            append(Modifier.toString(field.getModifier()));
            append(" ");
        }
        append(field.getFieldType().getNameIncludingTypeParameters(false, resolver));
        append(" ");
        append(introductionTo.getSimpleTypeName());
        append(".");
        append(field.getFieldName().getSymbolName());
        // Append initializer, if present
        if (field.getFieldInitializer() != null) {
            append(" = ");
            append(field.getFieldInitializer());
        }
        // Complete the field declaration
        append(";");
        this.newLine(false);
        this.newLine();
    }
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AbstractComment(org.springframework.roo.classpath.details.comments.AbstractComment) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)3 AbstractComment (org.springframework.roo.classpath.details.comments.AbstractComment)3 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)3 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)3 ArrayList (java.util.ArrayList)2 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)2 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)2 ConstructorMetadata (org.springframework.roo.classpath.details.ConstructorMetadata)1 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)1 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)1 JavaType (org.springframework.roo.model.JavaType)1