Search in sources :

Example 1 with JavaDocBuilder

use of com.thoughtworks.qdox.JavaDocBuilder in project jsonschema2pojo by joelittlejohn.

the class JavaNameIT method originalPropertyNamesAppearInJavaDoc.

@Test
public void originalPropertyNamesAppearInJavaDoc() throws NoSuchFieldException, 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 2 with JavaDocBuilder

use of com.thoughtworks.qdox.JavaDocBuilder in project jsonschema2pojo by joelittlejohn.

the class TitleIT method generateClasses.

@BeforeClass
public static void generateClasses() throws ClassNotFoundException, IOException {
    classSchemaRule.generateAndCompile("/schema/title/title.json", "com.example");
    File generatedJavaFile = classSchemaRule.generated("com/example/Title.java");
    JavaDocBuilder javaDocBuilder = new JavaDocBuilder();
    javaDocBuilder.addSource(generatedJavaFile);
    classWithTitle = javaDocBuilder.getClassByName("com.example.Title");
}
Also used : File(java.io.File) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder) BeforeClass(org.junit.BeforeClass)

Example 3 with JavaDocBuilder

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

the class FixJavadocMojoTest method testRemoveUnknownExceptions.

public void testRemoveUnknownExceptions() throws Exception {
    AbstractFixJavadocMojo mojoInstance = new FixJavadocMojo();
    setVariableValueToObject(mojoInstance, "fixTagsSplitted", new String[] { "all" });
    setVariableValueToObject(mojoInstance, "project", new MavenProjectStub());
    String source = "package a.b.c;" + EOL + "public class Clazz {" + EOL + " /**" + EOL + " * @throws java.lang.RuntimeException" + EOL + " * @throws NumberFormatException" + EOL + " * @throws java.lang.Exception" + // not thrown and no RTE -> remove
    EOL + " * @throws com.foo.FatalException" + // not on classpath (?!) -> see removeUnknownThrows
    EOL + " */" + EOL + " public void method() {}" + EOL + "}";
    JavaDocBuilder builder = new JavaDocBuilder();
    JavaMethod javaMethod = builder.addSource(new StringReader(source)).getClasses()[0].getMethods()[0];
    JavaEntityTags javaEntityTags = mojoInstance.parseJavadocTags(source, javaMethod, "", true);
    StringBuilder sb = new StringBuilder();
    mojoInstance.writeThrowsTag(sb, javaMethod, javaEntityTags, new String[] { "java.lang.RuntimeException" });
    assertEquals(" * @throws java.lang.RuntimeException", sb.toString());
    sb = new StringBuilder();
    mojoInstance.writeThrowsTag(sb, javaMethod, javaEntityTags, new String[] { "NumberFormatException" });
    assertEquals(" * @throws java.lang.NumberFormatException", sb.toString());
    sb = new StringBuilder();
    mojoInstance.writeThrowsTag(sb, javaMethod, javaEntityTags, new String[] { "java.lang.Exception" });
    assertEquals("", sb.toString());
    setVariableValueToObject(mojoInstance, "removeUnknownThrows", true);
    sb = new StringBuilder();
    mojoInstance.writeThrowsTag(sb, javaMethod, javaEntityTags, new String[] { "com.foo.FatalException" });
    assertEquals("", sb.toString());
    setVariableValueToObject(mojoInstance, "removeUnknownThrows", false);
    sb = new StringBuilder();
    mojoInstance.writeThrowsTag(sb, javaMethod, javaEntityTags, new String[] { "com.foo.FatalException" });
    assertEquals(" * @throws com.foo.FatalException if any.", sb.toString());
}
Also used : MavenProjectStub(org.apache.maven.plugin.testing.stubs.MavenProjectStub) JavaEntityTags(org.apache.maven.plugin.javadoc.AbstractFixJavadocMojo.JavaEntityTags) StringReader(java.io.StringReader) JavaMethod(com.thoughtworks.qdox.model.JavaMethod) JavaDocBuilder(com.thoughtworks.qdox.JavaDocBuilder)

Example 4 with JavaDocBuilder

use of com.thoughtworks.qdox.JavaDocBuilder 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 + "}";
    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 javadoc = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "extractOriginalJavadoc", new Class[] { String.class, AbstractJavaEntity.class }, new Object[] { 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 = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "extractOriginalJavadocContent", new Class[] { String.class, AbstractJavaEntity.class }, new Object[] { 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 = (String) PrivateAccessor.invoke(AbstractFixJavadocMojo.class, "getJavadocComment", new Class[] { String.class, AbstractJavaEntity.class }, new Object[] { 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().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 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()[1];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { 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()[2];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { 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()[3];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { 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()[4];
    tagJavadoc = (String) PrivateAccessor.invoke(mojoInstance, "getJavadocComment", new Class[] { String.class, AbstractInheritableJavaEntity.class, DocletTag.class }, new Object[] { 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 : 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)

Example 5 with JavaDocBuilder

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

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