Search in sources :

Example 1 with JavaField

use of com.thoughtworks.qdox.model.JavaField 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().length != 0) {
                if (lineNumber == javaClass.getAnnotations()[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.getMethods() != null) {
                for (JavaMethod method : javaClass.getMethods()) {
                    if (lineNumber == method.getLineNumber()) {
                        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)

Example 2 with JavaField

use of com.thoughtworks.qdox.model.JavaField 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 3 with JavaField

use of com.thoughtworks.qdox.model.JavaField in project jsonschema2pojo by joelittlejohn.

the class DollarCommentIT method descriptionAndCommentCanCoexist.

@Test
public void descriptionAndCommentCanCoexist() {
    JavaField javaField = classWithDescription.getFieldByName("descriptionAndComment");
    String javaDocComment = javaField.getComment();
    assertThat(javaDocComment, containsString("JavaDoc buddies!"));
    assertThat(javaDocComment.indexOf("JavaDoc buddies!"), is(greaterThan(javaDocComment.indexOf("This should co-exist with the value from $comment"))));
}
Also used : JavaField(com.thoughtworks.qdox.model.JavaField) Test(org.junit.Test)

Example 4 with JavaField

use of com.thoughtworks.qdox.model.JavaField in project jsonschema2pojo by joelittlejohn.

the class JavaNameIT method originalPropertyNamesAppearInJavaDoc.

@Test
public void originalPropertyNamesAppearInJavaDoc() throws IOException {
    schemaRule.generateAndCompile("/schema/javaName/javaName.json", "com.example.javaname");
    File generatedJavaFile = schemaRule.generated("com/example/javaname/JavaName.java");
    JavaDocBuilder javaDocBuilder = new JavaDocBuilder();
    javaDocBuilder.addSource(generatedJavaFile);
    JavaClass classWithDescription = javaDocBuilder.getClassByName("com.example.javaname.JavaName");
    JavaField javaPropertyField = classWithDescription.getFieldByName("javaProperty");
    assertThat(javaPropertyField.getComment(), containsString("Corresponds to the \"propertyWithJavaName\" property."));
    JavaField javaEnumField = classWithDescription.getFieldByName("javaEnum");
    assertThat(javaEnumField.getComment(), containsString("Corresponds to the \"enumWithJavaName\" property."));
    JavaField javaObjectField = classWithDescription.getFieldByName("javaObject");
    assertThat(javaObjectField.getComment(), containsString("Corresponds to the \"objectWithJavaName\" property."));
}
Also used : JavaField(com.thoughtworks.qdox.model.JavaField) JavaClass(com.thoughtworks.qdox.model.JavaClass) File(java.io.File) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder) Test(org.junit.Test)

Example 5 with JavaField

use of com.thoughtworks.qdox.model.JavaField in project jsonschema2pojo by joelittlejohn.

the class RequiredIT method notRequiredIsTheDefault.

@Test
public void notRequiredIsTheDefault() {
    JavaField javaField = classWithRequired.getFieldByName("defaultNotRequiredProperty");
    String javaDocComment = javaField.getComment();
    assertThat(javaDocComment, not(containsString("(Required)")));
}
Also used : JavaField(com.thoughtworks.qdox.model.JavaField) Test(org.junit.Test)

Aggregations

JavaField (com.thoughtworks.qdox.model.JavaField)17 Test (org.junit.Test)15 File (java.io.File)5 JavaDocBuilder (com.thoughtworks.qdox.JavaDocBuilder)3 JavaClass (com.thoughtworks.qdox.model.JavaClass)3 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)2 BufferedReader (java.io.BufferedReader)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 JavaConstructor (com.thoughtworks.qdox.model.JavaConstructor)1