Search in sources :

Example 11 with JavaClass

use of com.thoughtworks.qdox.model.JavaClass in project sling by apache.

the class JcrOcmMojo method execute.

public void execute() throws MojoFailureException {
    boolean hasFailures = false;
    // prepare QDox and prime with the compile class path of the project
    JavaDocBuilder builder = new JavaDocBuilder();
    try {
        builder.getClassLibrary().addClassLoader(this.getCompileClassLoader());
    } catch (IOException ioe) {
        throw new MojoFailureException("Cannot prepare QDox");
    }
    // add the sources from the project
    for (Iterator i = this.project.getCompileSourceRoots().iterator(); i.hasNext(); ) {
        try {
            builder.addSourceTree(new File((String) i.next()));
        } catch (OutOfMemoryError oome) {
            // this may be the case for big sources and not enough VM mem
            // drop the builder to help GC now
            builder = null;
            // fail with some explanation
            throw new MojoFailureException("Failed analyzing source due to not enough memory, try setting Max Heap Size higher, e.g. using MAVEN_OPTS=-Xmx128m");
        }
    }
    // parse the sources and get them
    JavaSource[] javaSources = builder.getSources();
    List descriptors = new ArrayList();
    for (int i = 0; i < javaSources.length; i++) {
        JavaClass[] javaClasses = javaSources[i].getClasses();
        for (int j = 0; javaClasses != null && j < javaClasses.length; j++) {
            DocletTag tag = javaClasses[j].getTagByName(ClassDescriptor.TAG_CLASS_DESCRIPTOR);
            if (tag != null) {
                ClassDescriptor descriptor = this.createClassDescriptor(javaClasses[j]);
                if (descriptor != null) {
                    descriptors.add(descriptor);
                } else {
                    hasFailures = true;
                }
            }
        }
    }
    // after checking all classes, throw if there were any failures
    if (hasFailures) {
        throw new MojoFailureException("Jackrabbit OCM Descriptor parsing had failures (see log)");
    }
    // terminate if there is nothing to write
    if (descriptors.isEmpty()) {
        this.getLog().info("No Jackrabbit OCM Descriptors found in project");
        return;
    }
    // finally the descriptors have to be written ....
    if (StringUtils.isEmpty(this.finalName)) {
        this.getLog().error("Descriptor file name must not be empty");
        return;
    }
    // prepare the descriptor output file
    File descriptorFile = new File(new File(this.outputDirectory, "SLING-INF"), this.finalName);
    // ensure parent dir
    descriptorFile.getParentFile().mkdirs();
    this.getLog().info("Generating " + descriptors.size() + " OCM Mapping Descriptors to " + descriptorFile);
    // write out all the class descriptors in parse order
    FileOutputStream descriptorStream = null;
    XMLWriter xw = null;
    try {
        descriptorStream = new FileOutputStream(descriptorFile);
        xw = new XMLWriter(descriptorStream, false);
        xw.printElementStart("jackrabbit-ocm", false);
        for (Iterator di = descriptors.iterator(); di.hasNext(); ) {
            ClassDescriptor sd = (ClassDescriptor) di.next();
            sd.generate(xw);
        }
        xw.printElementEnd("jackrabbit-ocm");
    } catch (IOException ioe) {
        hasFailures = true;
        this.getLog().error("Cannot write descriptor to " + descriptorFile, ioe);
        throw new MojoFailureException("Failed to write descriptor to " + descriptorFile);
    } finally {
        IOUtil.close(xw);
        IOUtil.close(descriptorStream);
        // remove the descriptor file in case of write failure
        if (hasFailures) {
            descriptorFile.delete();
        }
    }
    // now add the descriptor file to the maven resources
    final String ourRsrcPath = this.outputDirectory.getAbsolutePath();
    boolean found = false;
    final Iterator rsrcIterator = this.project.getResources().iterator();
    while (!found && rsrcIterator.hasNext()) {
        final Resource rsrc = (Resource) rsrcIterator.next();
        found = rsrc.getDirectory().equals(ourRsrcPath);
    }
    if (!found) {
        final Resource resource = new Resource();
        resource.setDirectory(this.outputDirectory.getAbsolutePath());
        this.project.addResource(resource);
    }
    // and set include accordingly
    this.project.getProperties().setProperty("Sling-Mappings", "SLING-INF/" + this.finalName);
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) IOException(java.io.IOException) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder) JavaClass(com.thoughtworks.qdox.model.JavaClass) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) JavaSource(com.thoughtworks.qdox.model.JavaSource) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Example 12 with JavaClass

use of com.thoughtworks.qdox.model.JavaClass 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)

Example 13 with JavaClass

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

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

the class FixJavadocMojoTest method testJavadocComment.

/**
 * @throws Throwable if any
 */
public void testJavadocComment() throws Throwable {
    String content = "/**" + EOL + " * Dummy Class." + EOL + " */" + EOL + "public class DummyClass" + EOL + "{" + EOL + "    /**" + EOL + "     *" + EOL + "     * Dummy" + EOL + "     *" + EOL + "     *      Method." + EOL + "     *" + EOL + "     * @param args not" + EOL + "     *" + EOL + "     * null" + EOL + "     * @param i non negative" + EOL + "     * @param object could" + EOL + "     * be" + EOL + "     *      null" + EOL + "     * @return a" + EOL + "     * String" + EOL + "     *" + EOL + "     * @throws Exception if" + EOL + "     * any" + EOL + "     *" + EOL + "     */" + EOL + "    public static String dummyMethod( String[] args, int i, Object object )" + EOL + "        throws Exception" + EOL + "    {" + EOL + "        return null;" + EOL + "    }" + EOL + "}";
    JavaProjectBuilder builder = new JavaProjectBuilder();
    builder.setEncoding("UTF-8");
    builder.addSource(new StringReader(content));
    JavaClass clazz = builder.addSource(new StringReader(content)).getClassByName("DummyClass");
    JavaMethod javaMethod = clazz.getMethods().get(0);
    String javadoc = AbstractFixJavadocMojo.extractOriginalJavadoc(content, javaMethod);
    assertEquals("    /**" + EOL + "     *" + EOL + "     * Dummy" + EOL + "     *" + EOL + "     *      Method." + EOL + "     *" + EOL + "     * @param args not" + EOL + "     *" + EOL + "     * null" + EOL + "     * @param i non negative" + EOL + "     * @param object could" + EOL + "     * be" + EOL + "     *      null" + EOL + "     * @return a" + EOL + "     * String" + EOL + "     *" + EOL + "     * @throws Exception if" + EOL + "     * any" + EOL + "     *" + EOL + "     */", javadoc);
    String javadocContent = AbstractFixJavadocMojo.extractOriginalJavadocContent(content, javaMethod);
    assertEquals("     *" + EOL + "     * Dummy" + EOL + "     *" + EOL + "     *      Method." + EOL + "     *" + EOL + "     * @param args not" + EOL + "     *" + EOL + "     * null" + EOL + "     * @param i non negative" + EOL + "     * @param object could" + EOL + "     * be" + EOL + "     *      null" + EOL + "     * @return a" + EOL + "     * String" + EOL + "     *" + EOL + "     * @throws Exception if" + EOL + "     * any" + EOL + "     *", javadocContent);
    String withoutEmptyJavadocLines = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines", new Class[] { String.class }, new Object[] { javadocContent });
    assertTrue(withoutEmptyJavadocLines.endsWith("any"));
    String methodJavadoc = AbstractFixJavadocMojo.getJavadocComment(content, javaMethod);
    assertEquals("     *" + EOL + "     * Dummy" + EOL + "     *" + EOL + "     *      Method." + EOL + "     *", methodJavadoc);
    withoutEmptyJavadocLines = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines", new Class[] { String.class }, new Object[] { methodJavadoc });
    assertTrue(withoutEmptyJavadocLines.endsWith("Method."));
    assertEquals(5, javaMethod.getTags().size());
    AbstractFixJavadocMojo mojoInstance = new FixJavadocMojo();
    setVariableValueToObject(mojoInstance, "fixTagsSplitted", new String[] { "all" });
    DocletTag tag = javaMethod.getTags().get(0);
    String tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @param args not" + EOL + "     *" + EOL + "     * null", tagJavadoc);
    withoutEmptyJavadocLines = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines", new Class[] { String.class }, new Object[] { tagJavadoc });
    assertTrue(withoutEmptyJavadocLines.endsWith("null"));
    tag = javaMethod.getTags().get(1);
    tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @param i non negative", tagJavadoc);
    withoutEmptyJavadocLines = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines", new Class[] { String.class }, new Object[] { tagJavadoc });
    assertTrue(withoutEmptyJavadocLines.endsWith("negative"));
    tag = javaMethod.getTags().get(2);
    tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @param object could" + EOL + "     * be" + EOL + "     *      null", tagJavadoc);
    withoutEmptyJavadocLines = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines", new Class[] { String.class }, new Object[] { tagJavadoc });
    assertTrue(withoutEmptyJavadocLines.endsWith("null"));
    tag = javaMethod.getTags().get(3);
    tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @return a" + EOL + "     * String" + EOL + "     *", tagJavadoc);
    withoutEmptyJavadocLines = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines", new Class[] { String.class }, new Object[] { tagJavadoc });
    assertTrue(withoutEmptyJavadocLines.endsWith("String"));
    tag = javaMethod.getTags().get(4);
    tagJavadoc = mojoInstance.getJavadocComment(content, javaMethod, tag);
    assertEquals("     * @throws Exception if" + EOL + "     * any" + EOL + "     *", tagJavadoc);
    withoutEmptyJavadocLines = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "removeLastEmptyJavadocLines", new Class[] { String.class }, new Object[] { tagJavadoc });
    assertTrue(withoutEmptyJavadocLines.endsWith("any"));
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass) StringReader(java.io.StringReader) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaProjectBuilder(com.thoughtworks.qdox.JavaProjectBuilder) JavaClass(com.thoughtworks.qdox.model.JavaClass) DocletTag(com.thoughtworks.qdox.model.DocletTag)

Example 15 with JavaClass

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

the class AbstractFixJavadocMojoTest method testReplaceLinkTags_missingEndBrace.

public void testReplaceLinkTags_missingEndBrace() throws Throwable {
    String comment = "/** {@link ConnectException */";
    String source = "import java.net.ConnectException;\n" + comment + "\n" + "public class MissingEndBrace {}";
    JavaClass clazz = getJavaSource(source).getClassByName("MissingEndBrace");
    String newComment = AbstractFixJavadocMojo.replaceLinkTags(comment, clazz);
    assertEquals("/** {@link ConnectException */", newComment);
}
Also used : JavaClass(com.thoughtworks.qdox.model.JavaClass)

Aggregations

JavaClass (com.thoughtworks.qdox.model.JavaClass)48 AbstractInheritableJavaEntity (com.thoughtworks.qdox.model.AbstractInheritableJavaEntity)12 JavaMethod (com.thoughtworks.qdox.model.JavaMethod)12 DocletTag (com.thoughtworks.qdox.model.DocletTag)10 JavaDocBuilder (com.thoughtworks.qdox.JavaDocBuilder)6 File (java.io.File)6 StringReader (java.io.StringReader)6 JavaParameter (com.thoughtworks.qdox.model.JavaParameter)5 JavaSource (com.thoughtworks.qdox.model.JavaSource)5 ArrayList (java.util.ArrayList)5 JavaProjectBuilder (com.thoughtworks.qdox.JavaProjectBuilder)4 BeanProperty (com.thoughtworks.qdox.model.BeanProperty)4 JavaField (com.thoughtworks.qdox.model.JavaField)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Test (org.junit.Test)3 AbstractJavaEntity (com.thoughtworks.qdox.model.AbstractJavaEntity)2 JavaExecutable (com.thoughtworks.qdox.model.JavaExecutable)2 JavaMember (com.thoughtworks.qdox.model.JavaMember)2 JavaType (com.thoughtworks.qdox.model.JavaType)2