Search in sources :

Example 11 with JavaDocBuilder

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

the class AbstractFixJavadocMojo method getQdoxClasses.

/**
     * Calling Qdox to find {@link JavaClass} objects from the Maven project sources.
     * Ignore java class if Qdox has parsing errors.
     *
     * @return an array of {@link JavaClass} found by QDox
     * @throws IOException            if any
     * @throws MojoExecutionException if any
     */
private JavaClass[] getQdoxClasses() throws IOException, MojoExecutionException {
    if ("pom".equalsIgnoreCase(project.getPackaging())) {
        getLog().warn("This project has 'pom' packaging, no Java sources is available.");
        return null;
    }
    List<File> javaFiles = new LinkedList<File>();
    for (String sourceRoot : getProjectSourceRoots(project)) {
        File f = new File(sourceRoot);
        if (f.isDirectory()) {
            javaFiles.addAll(FileUtils.getFiles(f, includes, excludes, true));
        } else {
            if (getLog().isWarnEnabled()) {
                getLog().warn(f + " doesn't exist. Ignored it.");
            }
        }
    }
    JavaDocBuilder builder = new JavaDocBuilder();
    builder.getClassLibrary().addClassLoader(getProjectClassLoader());
    builder.setEncoding(encoding);
    for (File f : javaFiles) {
        if (!f.getAbsolutePath().toLowerCase(Locale.ENGLISH).endsWith(".java") && getLog().isWarnEnabled()) {
            getLog().warn("'" + f + "' is not a Java file. Ignored it.");
            continue;
        }
        try {
            builder.addSource(f);
        } catch (ParseException e) {
            if (getLog().isWarnEnabled()) {
                getLog().warn("QDOX ParseException: " + e.getMessage() + ". Can't fix it.");
            }
        }
    }
    return builder.getClasses();
}
Also used : ParseException(com.thoughtworks.qdox.parser.ParseException) File(java.io.File) LinkedList(java.util.LinkedList) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder)

Example 12 with JavaDocBuilder

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

the class FixJavadocMojoTest method testJavadocCommentJdk5.

/**
     * @throws Throwable if any
     */
public void testJavadocCommentJdk5() throws Throwable {
    String content = "/**" + EOL + " * Dummy Class." + EOL + " */" + EOL + "public class DummyClass" + EOL + "{" + EOL + "    /**" + EOL + "     * Dummy method." + EOL + "     *" + EOL + "     * @param <K>  The Key type for the method" + EOL + "     * @param <V>  The Value type for the method" + EOL + "     * @param name The name." + EOL + "     * @return A map configured." + EOL + "     */" + EOL + "    public <K, V> java.util.Map<K, V> dummyMethod( String name )" + EOL + "    {" + EOL + "        return null;" + EOL + "    }" + EOL + "}";
    JavaDocBuilder builder = new JavaDocBuilder();
    builder.setEncoding("UTF-8");
    builder.addSource(new StringReader(content));
    JavaClass[] classes = builder.getClasses();
    JavaClass clazz = classes[0];
    JavaMethod javaMethod = clazz.getMethods()[0];
    String methodJavadoc = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] { String.class, AbstractJavaEntity.class }, new Object[] { content, javaMethod });
    assertEquals("     * Dummy method." + EOL + "     *", methodJavadoc);
    assertEquals(4, javaMethod.getTags().length);
    AbstractFixJavadocMojo mojoInstance = new FixJavadocMojo();
    setVariableValueToObject(mojoInstance, "fixTagsSplitted", new String[] { "all" });
    DocletTag tag = javaMethod.getTags()[0];
    String tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @param <K>  The Key type for the method", tagJavadoc);
    tag = javaMethod.getTags()[1];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @param <V>  The Value type for the method", tagJavadoc);
    tag = javaMethod.getTags()[2];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @param name The name.", tagJavadoc);
    tag = javaMethod.getTags()[3];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { content, javaMethod, tag });
    assertEquals("     * @return A map configured.", tagJavadoc);
}
Also used : AbstractInheritableJavaEntity(com.thoughtworks.qdox.model.AbstractInheritableJavaEntity) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder) AbstractJavaEntity(com.thoughtworks.qdox.model.AbstractJavaEntity) JavaClass(com.thoughtworks.qdox.model.JavaClass) StringReader(java.io.StringReader) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaClass(com.thoughtworks.qdox.model.JavaClass) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Aggregations

JavaDocBuilder (com.thoughtworks.qdox.JavaDocBuilder)12 File (java.io.File)9 JavaClass (com.thoughtworks.qdox.model.JavaClass)6 BeforeClass (org.junit.BeforeClass)4 DocletTag (com.thoughtworks.qdox.model.DocletTag)3 JavaField (com.thoughtworks.qdox.model.JavaField)3 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)3 StringReader (java.io.StringReader)3 Test (org.junit.Test)3 AbstractInheritableJavaEntity (com.thoughtworks.qdox.model.AbstractInheritableJavaEntity)2 AbstractJavaEntity (com.thoughtworks.qdox.model.AbstractJavaEntity)2 JavaSource (com.thoughtworks.qdox.model.JavaSource)1 ParseException (com.thoughtworks.qdox.parser.ParseException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Resource (org.apache.maven.model.Resource)1