Search in sources :

Example 1 with JavaExecutable

use of com.thoughtworks.qdox.model.JavaExecutable in project maven-plugins by apache.

the class AbstractFixJavadocMojo method addDefaultJavadocTags.

/**
 * @param sb           not null
 * @param entity       not null
 * @param indent       not null
 * @param isJavaExecutable
 * @throws MojoExecutionException if any
 */
private void addDefaultJavadocTags(final StringBuilder sb, final JavaAnnotatedElement entity, final String indent, final boolean isJavaExecutable) throws MojoExecutionException {
    boolean separatorAdded = false;
    if (isJavaExecutable) {
        JavaExecutable javaExecutable = (JavaExecutable) entity;
        if (fixTag(PARAM_TAG) && javaExecutable.getParameters() != null) {
            for (JavaParameter javaParameter : javaExecutable.getParameters()) {
                separatorAdded = appendDefaultParamTag(sb, indent, separatorAdded, javaParameter);
            }
        }
        if (javaExecutable instanceof JavaMethod && fixTag(RETURN_TAG)) {
            JavaMethod javaMethod = (JavaMethod) javaExecutable;
            if (javaMethod.getReturns() != null && !javaMethod.getReturns().isVoid()) {
                separatorAdded = appendDefaultReturnTag(sb, indent, separatorAdded, javaMethod);
            }
        }
        if (fixTag(THROWS_TAG) && javaExecutable.getExceptions() != null) {
            for (JavaType exception : javaExecutable.getExceptions()) {
                separatorAdded = appendDefaultThrowsTag(sb, indent, separatorAdded, exception);
            }
        }
    } else {
        separatorAdded = appendDefaultAuthorTag(sb, indent, separatorAdded);
        separatorAdded = appendDefaultVersionTag(sb, indent, separatorAdded);
    }
    if (fixTag(SINCE_TAG)) {
        if (!isJavaExecutable) {
            JavaClass javaClass = (JavaClass) entity;
            if (!ignoreClirr) {
                if (isNewClassFromLastVersion(javaClass)) {
                    separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                }
            } else {
                separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                addSinceClasses(javaClass);
            }
        } else {
            JavaExecutable javaExecutable = (JavaExecutable) entity;
            if (!ignoreClirr) {
                if (isNewMethodFromLastRevision(javaExecutable)) {
                    separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                }
            } else {
                if (sinceClasses != null && !sinceClassesContains(javaExecutable.getDeclaringClass())) {
                    separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
                }
            }
        }
    }
}
Also used : JavaType(com.thoughtworks.qdox.model.JavaType) JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaParameter(com.thoughtworks.qdox.model.JavaParameter) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaExecutable(com.thoughtworks.qdox.model.JavaExecutable)

Example 2 with JavaExecutable

use of com.thoughtworks.qdox.model.JavaExecutable in project maven-plugins by apache.

the class AbstractFixJavadocMojo method updateJavadocTags.

/**
 * Write tags according javaEntityTags.
 *
 * @param sb             not null
 * @param entity         not null
 * @param isJavaExecutable
 * @param javaEntityTags not null
 */
private void updateJavadocTags(final StringBuilder sb, final JavaAnnotatedElement entity, final boolean isJavaExecutable, final JavaEntityTags javaEntityTags) {
    for (DocletTag docletTag : entity.getTags()) {
        if (isJavaExecutable) {
            JavaExecutable javaExecutable = (JavaExecutable) entity;
            List<String> params = docletTag.getParameters();
            if (params.size() < 1) {
                continue;
            }
            if (docletTag.getName().equals(PARAM_TAG)) {
                writeParamTag(sb, javaExecutable, javaEntityTags, params);
            } else if (docletTag.getName().equals(RETURN_TAG) && javaExecutable instanceof JavaMethod) {
                writeReturnTag(sb, (JavaMethod) javaExecutable, javaEntityTags);
            } else if (docletTag.getName().equals(THROWS_TAG)) {
                writeThrowsTag(sb, javaExecutable, javaEntityTags, params);
            } else {
                // write unknown tags
                for (Iterator<String> it = javaEntityTags.getUnknownTags().iterator(); it.hasNext(); ) {
                    String originalJavadocTag = it.next();
                    String simplified = StringUtils.removeDuplicateWhitespace(originalJavadocTag).trim();
                    if (simplified.contains("@" + docletTag.getName())) {
                        it.remove();
                        sb.append(originalJavadocTag);
                        sb.append(EOL);
                    }
                }
            }
        } else {
            for (Iterator<String> it = javaEntityTags.getUnknownTags().iterator(); it.hasNext(); ) {
                String originalJavadocTag = it.next();
                String simplified = StringUtils.removeDuplicateWhitespace(originalJavadocTag).trim();
                if (simplified.contains("@" + docletTag.getName())) {
                    it.remove();
                    sb.append(originalJavadocTag);
                    sb.append(EOL);
                }
            }
        }
        if (sb.toString().endsWith(EOL)) {
            sb.delete(sb.toString().lastIndexOf(EOL), sb.toString().length());
        }
        sb.append(EOL);
    }
}
Also used : JavaMethod(com.thoughtworks.qdox.model.JavaMethod) DocletTag(com.thoughtworks.qdox.model.DocletTag) JavaExecutable(com.thoughtworks.qdox.model.JavaExecutable)

Example 3 with JavaExecutable

use of com.thoughtworks.qdox.model.JavaExecutable in project maven-plugins by apache.

the class AbstractFixJavadocMojo method addMissingJavadocTags.

/**
 * Add missing tags not already written.
 *
 * @param sb             not null
 * @param entity         not null
 * @param indent         not null
 * @param isJavaExecutable
 * @param javaEntityTags not null
 * @throws MojoExecutionException if any
 */
private void addMissingJavadocTags(final StringBuilder sb, final JavaAnnotatedElement entity, final String indent, final boolean isJavaExecutable, final JavaEntityTags javaEntityTags) throws MojoExecutionException {
    if (isJavaExecutable) {
        JavaExecutable javaExecutable = (JavaExecutable) entity;
        if (fixTag(PARAM_TAG)) {
            if (javaExecutable.getParameters() != null) {
                for (JavaParameter javaParameter : javaExecutable.getParameters()) {
                    if (javaEntityTags.getJavadocParamTag(javaParameter.getName(), true) == null) {
                        appendDefaultParamTag(sb, indent, javaParameter);
                    }
                }
            }
            // is generic?
            if (javaExecutable.getTypeParameters() != null) {
                for (JavaTypeVariable<JavaGenericDeclaration> typeParam : javaExecutable.getTypeParameters()) {
                    if (javaEntityTags.getJavadocParamTag("<" + typeParam.getName() + ">", true) == null) {
                        appendDefaultParamTag(sb, indent, typeParam);
                    }
                }
            }
        }
        if (javaExecutable instanceof JavaMethod) {
            JavaMethod javaMethod = (JavaMethod) javaExecutable;
            if (fixTag(RETURN_TAG) && StringUtils.isEmpty(javaEntityTags.getJavadocReturnTag()) && javaMethod.getReturns() != null && !javaMethod.getReturns().isVoid()) {
                appendDefaultReturnTag(sb, indent, javaMethod);
            }
        }
        if (fixTag(THROWS_TAG) && javaExecutable.getExceptions() != null) {
            for (JavaType exception : javaExecutable.getExceptions()) {
                if (javaEntityTags.getJavadocThrowsTag(exception.getValue(), true) == null) {
                    appendDefaultThrowsTag(sb, indent, exception);
                }
            }
        }
    } else {
        if (!javaEntityTags.getNamesTags().contains(AUTHOR_TAG)) {
            appendDefaultAuthorTag(sb, indent);
        }
        if (!javaEntityTags.getNamesTags().contains(VERSION_TAG)) {
            appendDefaultVersionTag(sb, indent);
        }
    }
    if (fixTag(SINCE_TAG) && !javaEntityTags.getNamesTags().contains(SINCE_TAG)) {
        if (!isJavaExecutable) {
            if (!ignoreClirr) {
                if (isNewClassFromLastVersion((JavaClass) entity)) {
                    appendDefaultSinceTag(sb, indent);
                }
            } else {
                appendDefaultSinceTag(sb, indent);
                addSinceClasses((JavaClass) entity);
            }
        } else {
            if (!ignoreClirr) {
                if (isNewMethodFromLastRevision((JavaExecutable) entity)) {
                    appendDefaultSinceTag(sb, indent);
                }
            } else if (sinceClasses != null) {
                if (entity instanceof JavaMember && !sinceClassesContains(((JavaMember) entity).getDeclaringClass())) {
                    appendDefaultSinceTag(sb, indent);
                } else if (entity instanceof JavaClass && !sinceClassesContains(((JavaClass) entity).getDeclaringClass())) {
                    appendDefaultSinceTag(sb, indent);
                }
            }
        }
    }
}
Also used : JavaType(com.thoughtworks.qdox.model.JavaType) JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaGenericDeclaration(com.thoughtworks.qdox.model.JavaGenericDeclaration) JavaParameter(com.thoughtworks.qdox.model.JavaParameter) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaMember(com.thoughtworks.qdox.model.JavaMember) JavaExecutable(com.thoughtworks.qdox.model.JavaExecutable)

Example 4 with JavaExecutable

use of com.thoughtworks.qdox.model.JavaExecutable in project maven-plugins by apache.

the class AbstractFixJavadocMojo method updateJavadocComment.

/**
 * @param stringWriter    not null
 * @param originalContent not null
 * @param entity          not null
 * @param indent          not null
 * @throws MojoExecutionException if any
 * @throws IOException            if any
 */
private void updateJavadocComment(final StringWriter stringWriter, final String originalContent, final JavaAnnotatedElement entity, final String indent) throws MojoExecutionException, IOException {
    if (entity.getComment() == null && (entity.getTags() == null || entity.getTags().isEmpty())) {
        return;
    }
    boolean isJavaExecutable = false;
    if (entity instanceof JavaExecutable) {
        isJavaExecutable = true;
    }
    StringBuilder sb = new StringBuilder();
    // special case for inherited method
    if (isJavaExecutable) {
        JavaExecutable javaMethod = (JavaExecutable) entity;
        if (isInherited(javaMethod)) {
            // QDOX-154 could be empty
            if (StringUtils.isEmpty(javaMethod.getComment())) {
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }
            String javadoc = getJavadocComment(originalContent, javaMethod);
            // case: /** {@inheritDoc} */ or no tags
            if (hasInheritedTag(javadoc) && (javaMethod.getTags() == null || javaMethod.getTags().isEmpty())) {
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }
            if (javadoc.contains(START_JAVADOC)) {
                javadoc = javadoc.substring(javadoc.indexOf(START_JAVADOC) + START_JAVADOC.length());
            }
            if (javadoc.contains(END_JAVADOC)) {
                javadoc = javadoc.substring(0, javadoc.indexOf(END_JAVADOC));
            }
            sb.append(indent).append(START_JAVADOC);
            sb.append(EOL);
            if (!javadoc.contains(INHERITED_TAG)) {
                sb.append(indent).append(SEPARATOR_JAVADOC).append(INHERITED_TAG);
                sb.append(EOL);
                appendSeparator(sb, indent);
            }
            javadoc = removeLastEmptyJavadocLines(javadoc);
            javadoc = alignIndentationJavadocLines(javadoc, indent);
            sb.append(javadoc);
            sb.append(EOL);
            if (javaMethod.getTags() != null) {
                for (DocletTag docletTag : javaMethod.getTags()) {
                    // Voluntary ignore these tags
                    if (JavadocUtil.equals(docletTag.getName(), PARAM_TAG, RETURN_TAG, THROWS_TAG)) {
                        continue;
                    }
                    String s = getJavadocComment(originalContent, entity, docletTag);
                    s = removeLastEmptyJavadocLines(s);
                    s = alignIndentationJavadocLines(s, indent);
                    sb.append(s);
                    sb.append(EOL);
                }
            }
            sb.append(indent).append(" ").append(END_JAVADOC);
            sb.append(EOL);
            if (hasInheritedTag(sb.toString().trim())) {
                sb = new StringBuilder();
                sb.append(indent).append(INHERITED_JAVADOC);
                sb.append(EOL);
                stringWriter.write(sb.toString());
                return;
            }
            stringWriter.write(sb.toString());
            return;
        }
    }
    sb.append(indent).append(START_JAVADOC);
    sb.append(EOL);
    // comment
    if (StringUtils.isNotEmpty(entity.getComment())) {
        updateJavadocComment(sb, originalContent, entity, indent);
    } else {
        addDefaultJavadocComment(sb, entity, indent, isJavaExecutable);
    }
    // tags
    if (entity.getTags() != null && !entity.getTags().isEmpty()) {
        updateJavadocTags(sb, originalContent, entity, indent, isJavaExecutable);
    } else {
        addDefaultJavadocTags(sb, entity, indent, isJavaExecutable);
    }
    sb = new StringBuilder(removeLastEmptyJavadocLines(sb.toString())).append(EOL);
    sb.append(indent).append(" ").append(END_JAVADOC);
    sb.append(EOL);
    stringWriter.write(sb.toString());
}
Also used : JavaExecutable(com.thoughtworks.qdox.model.JavaExecutable) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Aggregations

JavaExecutable (com.thoughtworks.qdox.model.JavaExecutable)4 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)3 DocletTag (com.thoughtworks.qdox.model.DocletTag)2 JavaClass (com.thoughtworks.qdox.model.JavaClass)2 JavaParameter (com.thoughtworks.qdox.model.JavaParameter)2 JavaType (com.thoughtworks.qdox.model.JavaType)2 JavaGenericDeclaration (com.thoughtworks.qdox.model.JavaGenericDeclaration)1 JavaMember (com.thoughtworks.qdox.model.JavaMember)1