Search in sources :

Example 6 with JavaMethod

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

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

the class RequiredArrayIT method requiredAppearsInSetterJavadoc.

@Test
public void requiredAppearsInSetterJavadoc() throws IOException {
    JavaMethod javaMethod = classWithRequired.getMethodBySignature("setRequiredProperty", new Type[] { new Type("java.lang.String") });
    String javaDocComment = javaMethod.getComment();
    assertThat(javaDocComment, containsString("(Required)"));
}
Also used : Type(com.thoughtworks.qdox.model.Type) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with JavaMethod

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

the class RequiredIT method requiredAppearsInGetterJavadoc.

@Test
public void requiredAppearsInGetterJavadoc() throws IOException {
    JavaMethod javaMethod = classWithRequired.getMethodBySignature("getRequiredProperty", new Type[] {});
    String javaDocComment = javaMethod.getComment();
    assertThat(javaDocComment, containsString("(Required)"));
}
Also used : JavaMethod(com.thoughtworks.qdox.model.JavaMethod) Test(org.junit.Test)

Example 9 with JavaMethod

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

the class RequiredIT method requiredAppearsInSetterJavadoc.

@Test
public void requiredAppearsInSetterJavadoc() throws IOException {
    JavaMethod javaMethod = classWithRequired.getMethodBySignature("setRequiredProperty", new Type[] { new Type("java.lang.String") });
    String javaDocComment = javaMethod.getComment();
    assertThat(javaDocComment, containsString("(Required)"));
}
Also used : Type(com.thoughtworks.qdox.model.Type) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) Test(org.junit.Test)

Example 10 with JavaMethod

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

the class DescriptionIT method descriptionAppearsInSetterJavadoc.

@Test
public void descriptionAppearsInSetterJavadoc() throws IOException {
    JavaMethod javaMethod = classWithDescription.getMethodBySignature("setDescription", new Type[] { new Type("java.lang.String") });
    String javaDocComment = javaMethod.getComment();
    assertThat(javaDocComment, containsString("A description for this property"));
}
Also used : Type(com.thoughtworks.qdox.model.Type) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) Test(org.junit.Test)

Aggregations

JavaMethod (com.thoughtworks.qdox.model.JavaMethod)18 Test (org.junit.Test)8 Type (com.thoughtworks.qdox.model.Type)6 DocletTag (com.thoughtworks.qdox.model.DocletTag)5 JavaClass (com.thoughtworks.qdox.model.JavaClass)4 StringReader (java.io.StringReader)4 JavaDocBuilder (com.thoughtworks.qdox.JavaDocBuilder)3 AbstractInheritableJavaEntity (com.thoughtworks.qdox.model.AbstractInheritableJavaEntity)2 AbstractJavaEntity (com.thoughtworks.qdox.model.AbstractJavaEntity)2 JavaParameter (com.thoughtworks.qdox.model.JavaParameter)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 JavaField (com.thoughtworks.qdox.model.JavaField)1 TypeVariable (com.thoughtworks.qdox.model.TypeVariable)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 JavaEntityTags (org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.JavaEntityTags)1 MavenProjectStub (org.apache.maven.plugin.testing.stubs.MavenProjectStub)1