use of com.thoughtworks.qdox.model.JavaMethod 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.model.JavaMethod 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.JavaMethod 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.JavaMethod in project maven-plugins by apache.
the class AbstractFixJavadocMojo method processFix.
/**
* Process the given {@link JavaClass}, ie add missing javadoc tags depending user parameters.
*
* @param javaClass not null
* @throws IOException if any
* @throws MojoExecutionException if any
*/
private void processFix(JavaClass javaClass) throws IOException, MojoExecutionException {
// Skipping inner classes
if (javaClass.isInner()) {
return;
}
File javaFile = new File(javaClass.getSource().getURL().getFile());
// the original java content in memory
final String originalContent = StringUtils.unifyLineSeparators(FileUtils.fileRead(javaFile, encoding));
if (getLog().isDebugEnabled()) {
getLog().debug("Analyzing " + javaClass.getFullyQualifiedName());
}
final StringWriter stringWriter = new StringWriter();
BufferedReader reader = null;
boolean changeDetected = false;
try {
reader = new BufferedReader(new StringReader(originalContent));
int lineNumber = 0;
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
lineNumber++;
final String indent = autodetectIndentation(line);
// fixing classes
if (javaClass.getComment() == null && javaClass.getAnnotations() != null && javaClass.getAnnotations().length != 0) {
if (lineNumber == javaClass.getAnnotations()[0].getLineNumber()) {
changeDetected |= fixClassComment(stringWriter, originalContent, javaClass, indent);
takeCareSingleComment(stringWriter, originalContent, javaClass);
}
} else {
if (lineNumber == javaClass.getLineNumber()) {
changeDetected |= fixClassComment(stringWriter, originalContent, javaClass, indent);
takeCareSingleComment(stringWriter, originalContent, javaClass);
}
}
// fixing fields
if (javaClass.getFields() != null) {
for (JavaField field : javaClass.getFields()) {
if (lineNumber == field.getLineNumber()) {
changeDetected |= fixFieldComment(stringWriter, javaClass, field, indent);
}
}
}
// fixing methods
if (javaClass.getMethods() != null) {
for (JavaMethod method : javaClass.getMethods()) {
if (lineNumber == method.getLineNumber()) {
changeDetected |= fixMethodComment(stringWriter, originalContent, method, indent);
takeCareSingleComment(stringWriter, originalContent, method);
}
}
}
stringWriter.write(line);
stringWriter.write(EOL);
}
reader.close();
reader = null;
} finally {
IOUtil.close(reader);
}
if (changeDetected) {
if (getLog().isInfoEnabled()) {
getLog().info("Saving changes to " + javaClass.getFullyQualifiedName());
}
if (outputDirectory != null && !outputDirectory.getAbsolutePath().equals(getProjectSourceDirectory().getAbsolutePath())) {
String path = StringUtils.replace(javaFile.getAbsolutePath().replaceAll("\\\\", "/"), project.getBuild().getSourceDirectory().replaceAll("\\\\", "/"), "");
javaFile = new File(outputDirectory, path);
javaFile.getParentFile().mkdirs();
}
writeFile(javaFile, encoding, stringWriter.toString());
} else {
if (getLog().isDebugEnabled()) {
getLog().debug("No changes made to " + javaClass.getFullyQualifiedName());
}
}
}
use of com.thoughtworks.qdox.model.JavaMethod in project maven-plugins by apache.
the class AbstractFixJavadocMojo method addDefaultMethodComment.
/**
* Add in the buffer a default Javadoc for the given class:
* <br/>
* <code>
* <font color="#808080">1</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">/**</font><br />
* <font color="#808080">2</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">* {Comment based on the method name}</font><br />
* <font color="#808080">3</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">*</font><br />
* <font color="#808080">4</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">* </font><font color="#7f9fbf">@param </font>
* <font color="#3f5fbf">X {added if addMissingParam}</font><br />
* <font color="#808080">5</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">* </font><font color="#7f9fbf">@return </font>
* <font color="#3f5fbf">X {added if addMissingReturn}</font><br />
* <font color="#808080">6</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">* </font><font color="#7f9fbf">@throws </font>
* <font color="#3f5fbf">X {added if addMissingThrows}</font><br />
* <font color="#808080">7</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">* </font><font color="#7f9fbf">@since </font>
* <font color="#3f5fbf">X {added if addMissingSince and new classes
* from previous version}</font><br />
* <font color="#808080">8</font> <font color="#ffffff"> </font>
* <font color="#3f5fbf">*/</font><br />
* <font color="#808080">9</font> <font color="#7f0055"><b>public </b></font>
* <font color="#7f0055"><b>void </b></font><font color="#000000">dummyMethod</font>
* <font color="#000000">( </font><font color="#000000">String s </font>
* <font color="#000000">){}</font>
* </code>
*
* @param buffer not null
* @param javaExecutable not null
* @param indent not null
* @throws MojoExecutionException if any
* @see #getDefaultMethodJavadocComment(JavaMethod)
* @see #appendDefaultSinceTag(StringBuilder, String)
*/
private void addDefaultMethodComment(final StringWriter stringWriter, final JavaExecutable javaExecutable, final String indent) throws MojoExecutionException {
StringBuilder sb = new StringBuilder();
// special case
if (isInherited(javaExecutable)) {
sb.append(indent).append(INHERITED_JAVADOC);
sb.append(EOL);
stringWriter.write(sb.toString());
return;
}
sb.append(indent).append(START_JAVADOC);
sb.append(EOL);
sb.append(indent).append(SEPARATOR_JAVADOC);
sb.append(getDefaultMethodJavadocComment(javaExecutable));
sb.append(EOL);
boolean separatorAdded = false;
if (fixTag(PARAM_TAG)) {
if (javaExecutable.getParameters() != null) {
for (JavaParameter javaParameter : javaExecutable.getParameters()) {
separatorAdded = appendDefaultParamTag(sb, indent, separatorAdded, javaParameter);
}
}
// is generic?
if (javaExecutable.getTypeParameters() != null) {
for (JavaTypeVariable<JavaGenericDeclaration> typeParam : javaExecutable.getTypeParameters()) {
separatorAdded = appendDefaultParamTag(sb, indent, separatorAdded, typeParam);
}
}
}
if (javaExecutable instanceof JavaMethod) {
JavaMethod javaMethod = (JavaMethod) javaExecutable;
if (fixTag(RETURN_TAG) && 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);
}
}
if (fixTag(SINCE_TAG) && isNewMethodFromLastRevision(javaExecutable)) {
separatorAdded = appendDefaultSinceTag(sb, indent, separatorAdded);
}
sb.append(indent).append(" ").append(END_JAVADOC);
sb.append(EOL);
stringWriter.write(sb.toString());
}
Aggregations