Search in sources :

Example 1 with JavaConstructor

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

the class AbstractFixJavadocMojo method processFix.

/**
 * Process the given {@link JavaClass}, ie add missing javadoc tags depending user parameters.
 *
 * @param javaClass not null
 * @throws IOException            if any
 * @throws MojoExecutionException if any
 */
private void processFix(JavaClass javaClass) throws IOException, MojoExecutionException {
    // Skipping inner classes
    if (javaClass.isInner()) {
        return;
    }
    File javaFile = new File(javaClass.getSource().getURL().getFile());
    // the original java content in memory
    final String originalContent = StringUtils.unifyLineSeparators(FileUtils.fileRead(javaFile, encoding));
    if (getLog().isDebugEnabled()) {
        getLog().debug("Analyzing " + javaClass.getFullyQualifiedName());
    }
    final StringWriter stringWriter = new StringWriter();
    BufferedReader reader = null;
    boolean changeDetected = false;
    try {
        reader = new BufferedReader(new StringReader(originalContent));
        int lineNumber = 0;
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            lineNumber++;
            final String indent = autodetectIndentation(line);
            // fixing classes
            if (javaClass.getComment() == null && javaClass.getAnnotations() != null && !javaClass.getAnnotations().isEmpty()) {
                if (lineNumber == javaClass.getAnnotations().get(0).getLineNumber()) {
                    changeDetected |= fixClassComment(stringWriter, originalContent, javaClass, indent);
                    takeCareSingleComment(stringWriter, originalContent, javaClass);
                }
            } else if (lineNumber == javaClass.getLineNumber()) {
                changeDetected |= fixClassComment(stringWriter, originalContent, javaClass, indent);
                takeCareSingleComment(stringWriter, originalContent, javaClass);
            }
            // fixing fields
            if (javaClass.getFields() != null) {
                for (JavaField field : javaClass.getFields()) {
                    if (lineNumber == field.getLineNumber()) {
                        changeDetected |= fixFieldComment(stringWriter, javaClass, field, indent);
                    }
                }
            }
            // fixing methods
            if (javaClass.getConstructors() != null) {
                for (JavaConstructor method : javaClass.getConstructors()) {
                    if (lineNumber == method.getLineNumber()) {
                        changeDetected |= fixMethodComment(stringWriter, originalContent, method, indent);
                        takeCareSingleComment(stringWriter, originalContent, method);
                    }
                }
            }
            // fixing methods
            for (JavaMethod method : javaClass.getMethods()) {
                int methodLineNumber;
                if (method.getComment() == null && !method.getAnnotations().isEmpty()) {
                    methodLineNumber = method.getAnnotations().get(0).getLineNumber();
                } else {
                    methodLineNumber = method.getLineNumber();
                }
                if (lineNumber == methodLineNumber) {
                    changeDetected |= fixMethodComment(stringWriter, originalContent, method, indent);
                    takeCareSingleComment(stringWriter, originalContent, method);
                }
            }
            stringWriter.write(line);
            stringWriter.write(EOL);
        }
        reader.close();
        reader = null;
    } finally {
        IOUtil.close(reader);
    }
    if (changeDetected) {
        if (getLog().isInfoEnabled()) {
            getLog().info("Saving changes to " + javaClass.getFullyQualifiedName());
        }
        if (outputDirectory != null && !outputDirectory.getAbsolutePath().equals(getProjectSourceDirectory().getAbsolutePath())) {
            String path = StringUtils.replace(javaFile.getAbsolutePath().replaceAll("\\\\", "/"), project.getBuild().getSourceDirectory().replaceAll("\\\\", "/"), "");
            javaFile = new File(outputDirectory, path);
            javaFile.getParentFile().mkdirs();
        }
        writeFile(javaFile, encoding, stringWriter.toString());
    } else {
        if (getLog().isDebugEnabled()) {
            getLog().debug("No changes made to " + javaClass.getFullyQualifiedName());
        }
    }
}
Also used : JavaField(com.thoughtworks.qdox.model.JavaField) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) File(java.io.File) JavaConstructor(com.thoughtworks.qdox.model.JavaConstructor)

Example 2 with JavaConstructor

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

the class AbstractFixJavadocMojo method getDefaultMethodJavadocComment.

/**
 * Default comment for method with taking care of getter/setter in the javaMethod name.
 *
 * @param javaExecutable not null
 * @return a default comment for method.
 */
private static String getDefaultMethodJavadocComment(final JavaExecutable javaExecutable) {
    if (javaExecutable instanceof JavaConstructor) {
        return "<p>Constructor for " + javaExecutable.getName() + ".</p>";
    }
    if (javaExecutable.getName().length() > 3 && (javaExecutable.getName().startsWith("get") || javaExecutable.getName().startsWith("set"))) {
        String field = StringUtils.lowercaseFirstLetter(javaExecutable.getName().substring(3));
        JavaClass clazz = javaExecutable.getDeclaringClass();
        if (clazz.getFieldByName(field) == null) {
            return "<p>" + javaExecutable.getName() + ".</p>";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<p>");
        if (javaExecutable.getName().startsWith("get")) {
            sb.append("Getter ");
        } else if (javaExecutable.getName().startsWith("set")) {
            sb.append("Setter ");
        }
        sb.append("for the field <code>").append(field).append("</code>.</p>");
        return sb.toString();
    }
    return "<p>" + javaExecutable.getName() + ".</p>";
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) JavaConstructor(com.thoughtworks.qdox.model.JavaConstructor)

Aggregations

JavaConstructor (com.thoughtworks.qdox.model.JavaConstructor)2 JavaClass (com.thoughtworks.qdox.model.JavaClass)1 JavaField (com.thoughtworks.qdox.model.JavaField)1 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1