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."));
}
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");
}
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());
}
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"));
}
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);
}
Aggregations