use of com.thoughtworks.qdox.model.DocletTag 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.model.DocletTag in project maven-plugins by apache.
the class AbstractFixJavadocMojo method updateJavadocComment.
/**
* @param stringWriter not null
* @param originalContent not null
* @param entity not null
* @param indent not null
* @throws MojoExecutionException if any
* @throws IOException if any
*/
private void updateJavadocComment(final StringWriter stringWriter, final String originalContent, final AbstractInheritableJavaEntity entity, final String indent) throws MojoExecutionException, IOException {
if (entity.getComment() == null && (entity.getTags() == null || entity.getTags().length == 0)) {
return;
}
boolean isJavaMethod = false;
if (entity instanceof JavaMethod) {
isJavaMethod = true;
}
StringBuilder sb = new StringBuilder();
// special case for inherited method
if (isJavaMethod) {
JavaMethod javaMethod = (JavaMethod) entity;
if (isInherited(javaMethod)) {
// QDOX-154 could be empty
if (StringUtils.isEmpty(javaMethod.getComment())) {
sb.append(indent).append(INHERITED_JAVADOC);
sb.append(EOL);
stringWriter.write(sb.toString());
return;
}
String javadoc = getJavadocComment(originalContent, javaMethod);
// case: /** {@inheritDoc} */ or no tags
if (hasInheritedTag(javadoc) && (javaMethod.getTags() == null || javaMethod.getTags().length == 0)) {
sb.append(indent).append(INHERITED_JAVADOC);
sb.append(EOL);
stringWriter.write(sb.toString());
return;
}
if (javadoc.contains(START_JAVADOC)) {
javadoc = javadoc.substring(javadoc.indexOf(START_JAVADOC) + START_JAVADOC.length());
}
if (javadoc.contains(END_JAVADOC)) {
javadoc = javadoc.substring(0, javadoc.indexOf(END_JAVADOC));
}
sb.append(indent).append(START_JAVADOC);
sb.append(EOL);
if (!javadoc.contains(INHERITED_TAG)) {
sb.append(indent).append(SEPARATOR_JAVADOC).append(INHERITED_TAG);
sb.append(EOL);
appendSeparator(sb, indent);
}
javadoc = removeLastEmptyJavadocLines(javadoc);
javadoc = alignIndentationJavadocLines(javadoc, indent);
sb.append(javadoc);
sb.append(EOL);
if (javaMethod.getTags() != null) {
for (int i = 0; i < javaMethod.getTags().length; i++) {
DocletTag docletTag = javaMethod.getTags()[i];
// Voluntary ignore these tags
if (JavadocUtil.equals(docletTag.getName(), PARAM_TAG, RETURN_TAG, THROWS_TAG)) {
continue;
}
String s = getJavadocComment(originalContent, entity, docletTag);
s = removeLastEmptyJavadocLines(s);
s = alignIndentationJavadocLines(s, indent);
sb.append(s);
sb.append(EOL);
}
}
sb.append(indent).append(" ").append(END_JAVADOC);
sb.append(EOL);
if (hasInheritedTag(sb.toString().trim())) {
sb = new StringBuilder();
sb.append(indent).append(INHERITED_JAVADOC);
sb.append(EOL);
stringWriter.write(sb.toString());
return;
}
stringWriter.write(sb.toString());
return;
}
}
sb.append(indent).append(START_JAVADOC);
sb.append(EOL);
// comment
if (StringUtils.isNotEmpty(entity.getComment())) {
updateJavadocComment(sb, originalContent, entity, indent);
} else {
addDefaultJavadocComment(sb, entity, indent, isJavaMethod);
}
// tags
if (entity.getTags() != null && entity.getTags().length > 0) {
updateJavadocTags(sb, originalContent, entity, indent, isJavaMethod);
} else {
addDefaultJavadocTags(sb, entity, indent, isJavaMethod);
}
sb = new StringBuilder(removeLastEmptyJavadocLines(sb.toString())).append(EOL);
sb.append(indent).append(" ").append(END_JAVADOC);
sb.append(EOL);
stringWriter.write(sb.toString());
}
use of com.thoughtworks.qdox.model.DocletTag in project maven-plugins by apache.
the class AbstractFixJavadocMojo method parseJavadocTags.
/**
* Parse entity tags
*
* @param originalContent not null
* @param entity not null
* @param indent not null
* @param isJavaMethod
* @return an instance of {@link JavaEntityTags}
* @throws IOException if any
*/
JavaEntityTags parseJavadocTags(final String originalContent, final AbstractInheritableJavaEntity entity, final String indent, final boolean isJavaMethod) throws IOException {
JavaEntityTags javaEntityTags = new JavaEntityTags(entity, isJavaMethod);
for (int i = 0; i < entity.getTags().length; i++) {
DocletTag docletTag = entity.getTags()[i];
String originalJavadocTag = getJavadocComment(originalContent, entity, docletTag);
originalJavadocTag = removeLastEmptyJavadocLines(originalJavadocTag);
originalJavadocTag = alignIndentationJavadocLines(originalJavadocTag, indent);
javaEntityTags.getNamesTags().add(docletTag.getName());
if (isJavaMethod) {
String[] params = docletTag.getParameters();
if (params.length < 1) {
continue;
}
params = fixQdox173(params);
String paramName = params[0];
if (docletTag.getName().equals(PARAM_TAG)) {
javaEntityTags.putJavadocParamTag(paramName, originalJavadocTag);
} else if (docletTag.getName().equals(RETURN_TAG)) {
javaEntityTags.setJavadocReturnTag(originalJavadocTag);
} else if (docletTag.getName().equals(THROWS_TAG)) {
javaEntityTags.putJavadocThrowsTag(paramName, originalJavadocTag);
} else {
javaEntityTags.getUnknownTags().add(originalJavadocTag);
}
} else {
javaEntityTags.getUnknownTags().add(originalJavadocTag);
}
}
return javaEntityTags;
}
use of com.thoughtworks.qdox.model.DocletTag in project sling by apache.
the class BeanDescriptor method fromMethod.
static BeanDescriptor fromMethod(Log log, JavaMethod method) {
DocletTag tag = method.getTagByName(TAG_BEAN_DESCRIPTOR);
if (tag == null) {
return null;
}
// field name is the method name, unless overwritten with the fieldName
// tag
String fieldName = tag.getNamedParameter(FIELD_NAME);
if (fieldName == null) {
fieldName = getFieldFromMethod(method);
}
return new BeanDescriptor(log, tag, fieldName);
}
use of com.thoughtworks.qdox.model.DocletTag 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