Skip to content

Instantly share code, notes, and snippets.

@argius
Last active January 2, 2016 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save argius/8298691 to your computer and use it in GitHub Desktop.
Save argius/8298691 to your computer and use it in GitHub Desktop.
MethodListCsvDoclet - A custom doclet which outputs a method info list as CSV
<?xml version="1.0" encoding="UTF-8"?>
<project name="Custom Doclet">
<target name="run-custom-doclet" description="run custom doclet">
<javadoc doclet="doclet.MethodListCsvDoclet"
docletpath="bin"
access="private"
additionalparam="">
<sourcepath>
<pathelement location="src" />
</sourcepath>
</javadoc>
</target>
</project>
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 9 columns, instead of 12. in line 1.
class,package,FQCN,method,modifiers,synthetic?,parameters,return type,since
MethodListCsvDoclet,doclet,doclet.MethodListCsvDoclet,start,public static,No,"RootDoc rootDoc","boolean",,,,
MethodListCsvDoclet,doclet,doclet.MethodListCsvDoclet,languageVersion,public static,No,"","com.sun.javadoc.LanguageVersion",,,,
MethodListCsvDoclet,doclet,doclet.MethodListCsvDoclet,writeTo,private static,No,"PrintWriter out,RootDoc rootDoc","void",,,,
MethodListCsvDoclet,doclet,doclet.MethodListCsvDoclet,parseSince,private static,No,"com.sun.javadoc.Doc[] docs","java.lang.String",,,,
MethodListCsvDoclet,doclet,doclet.MethodListCsvDoclet,quote,private static,No,"Object o","java.lang.String",,,,
MethodListCsvDoclet,doclet,doclet.MethodListCsvDoclet,join,private static,No,"T[] a","java.lang.String",,,,
package doclet;
import java.io.*;
import java.util.*;
import com.sun.javadoc.*;
public final class MethodListCsvDoclet {
// entry point
public static boolean start(RootDoc rootDoc) {
File file = new File("doclet-methodlist.csv");
try {
PrintWriter out = new PrintWriter(file);
try {
writeTo(out, rootDoc);
if (out.checkError())
return false;
} finally {
out.close();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return true;
}
public static LanguageVersion languageVersion() {
return LanguageVersion.JAVA_1_5;
}
private static void writeTo(PrintWriter out, RootDoc rootDoc) {
// header
out.println(join(new String[]{"class", "package", "FQCN", "method", "modifiers",
"synthetic?", "parameters", "return type", "since"}));
// body
for (ClassDoc classDoc : rootDoc.classes()) {
for (MethodDoc methodDoc : classDoc.methods(true)) {
String[] a = new String[12];
Arrays.fill(a, "");
int i = -1;
a[++i] = classDoc.name();
a[++i] = classDoc.containingPackage().name();
a[++i] = classDoc.qualifiedName();
a[++i] = methodDoc.name();
a[++i] = methodDoc.modifiers();
a[++i] = methodDoc.isSynthetic() ? "Yes" : "No";
a[++i] = quote(join(methodDoc.parameters()));
a[++i] = quote(methodDoc.returnType());
a[++i] = parseSince(methodDoc, classDoc);
out.println(join(a));
}
}
}
private static String parseSince(Doc... docs) {
for (Doc doc : docs) {
String s = join(doc.tags("since")).replace(",", "");
if (s.contains("@since:"))
s = s.replaceAll("@since:", "");
s = s.trim();
if (!s.isEmpty())
return s;
}
return "";
}
private static String quote(Object o) {
return String.format("\"%s\"", o);
}
private static <T> String join(T[] a) {
if (a.length == 0)
return "";
StringBuilder s = new StringBuilder(String.valueOf(a[0]));
for (int i = 1; i < a.length; i++)
s.append(",").append(a[i]);
return s.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment