下面列出了怎么用com.sun.javadoc.RootDoc的API类实例代码及写法,或者点击链接到github查看源代码。
public static boolean start(RootDoc rootDoc) {
// 插件启动入口
DocLogger.setRootDoc(rootDoc);
if (StringUtils.isEmpty(classDir)) {
DocLogger.error("-classDir is not specified.");
return false;
}
DocLogger.info("Writing classes to " + classDir);
try {
parseAndAnnotate(rootDoc);
} catch (Exception e) {
DocLogger.error(e.getMessage() + "\n" +
Stream.of(e.getStackTrace()).map(StackTraceElement::toString)
.collect(joining("\n")));
if (nonNull(e.getCause())) {
DocLogger.error(e.getCause().getMessage() + "\n" +
Stream.of(e.getStackTrace()).map(StackTraceElement::toString)
.collect(joining("\n")));
}
}
return true;
}
private static void parseAndAnnotate(RootDoc rootDoc) throws ClassNotFoundException, NotFoundException, CannotCompileException, IOException {
for (ClassDoc classDoc : rootDoc.classes()) {
if (StringUtils.isEmpty(classDoc.getRawCommentText())) {
DocLogger.warn("No javadoc found in class " + classDoc.qualifiedName() + "." + classDoc.name());
}
Class clazz = Class.forName(classDoc.qualifiedName());
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(clazz));
CtClass ctClass = pool.get(clazz.getName());
ApiInfo apiInfo = ApiInfo.fromClassDoc(clazz, classDoc);
if (!apiInfo.hidden()) {
annotateClassAnn(ctClass, apiInfo);
for (MethodDoc methodDoc : classDoc.methods()) {
ApiMethodInfo apiMethodInfo = ApiMethodInfo.fromMethodDoc(matchingMethod(clazz, methodDoc), methodDoc);
if (StringUtils.isEmpty(methodDoc.getRawCommentText())) {
DocLogger.warn("No javadoc found in method " + classDoc.qualifiedName() + "." + methodDoc.name() + methodDoc.signature());
}
annotateMethodAnn(ctClass, apiMethodInfo);
}
DocLogger.info("Successfully annotated " + clazz.getTypeName());
}
ctClass.writeFile(classDir);
}
}
/**
* Construct a new Markdown Doclet.
* @param options The command line options.
* @param rootDoc The root document.
*/
public MarkdownDoclet(Options options, RootDoc rootDoc) {
this.options = options;
this.rootDoc = rootDoc;
tagRenderers.put("@author", SimpleTagRenderer.INSTANCE);
tagRenderers.put("@version", SimpleTagRenderer.INSTANCE);
tagRenderers.put("@return", SimpleTagRenderer.INSTANCE);
tagRenderers.put("@deprecated", SimpleTagRenderer.INSTANCE);
tagRenderers.put("@since", SimpleTagRenderer.INSTANCE);
tagRenderers.put("@param", ParamTagRenderer.INSTANCE);
tagRenderers.put("@throws", ThrowsTagRenderer.INSTANCE);
tagRenderers.put("@see", SeeTagRenderer.INSTANCE);
UmlTagRenderer umlTagRenderer = new UmlTagRenderer();
tagRenderers.put("@uml", umlTagRenderer);
tagRenderers.put("@startuml", umlTagRenderer);
tagRenderers.put("@enduml", TagRenderer.ELIDE);
tagRenderers.put("@todo", new TodoTagRenderer());
}
RootDoc getRootDoc(boolean withOption) {
List<String> opts = new ArrayList<>();
if (withOption)
opts.add("-XDfileManager.deferClose=10");
opts.add("-doclet");
opts.add(getClass().getName());
opts.add("-classpath");
opts.add(jarPath.toString());
opts.add(Paths.get(System.getProperty("test.src"), "p", "Test.java").toString());
System.err.println("javadoc opts: " + opts);
int rc = com.sun.tools.javadoc.Main.execute(
"javadoc",
// by specifying our own class loader, we get the same Class instance as this
getClass().getClassLoader(),
opts.toArray(new String[opts.size()]));
if (rc != 0) {
error("unexpected exit from javadoc or doclet: " + rc);
}
return cachedRoot;
}
public static boolean start(RootDoc root) {
//look for debug flag
for (String[] opts : root.options()) {
for (String opt : opts) {
if (opt.equals(DEBUG_SWITCH)) {
debugMode = true;
}
}
}
logMessage("Running doclet " + ConfigStandardDoclet.class.getSimpleName());
ClassDoc[] classes = root.classes();
for (int i = 0; i < classes.length; ++i) {
processDoc(classes[i]);
}
return true;
}
@SuppressWarnings("checkstyle:all")
@Override
public boolean start(AbstractDoclet doclet, RootDoc root) {
configuration().root = configuration().getProxyInstaller().installProxies(root);
if (!isValidDoclet(doclet)) {
return false;
}
try {
Reflect.callProc(this, AbstractDoclet.class, "startGeneration", //$NON-NLS-1$
new Class[] { RootDoc.class },
configuration().root);
} catch (DocletAbortException e) {
final Throwable cause = Utils.getCause(e);
if (cause.getLocalizedMessage() != null) {
configuration().root.printError(cause.getLocalizedMessage());
} else {
configuration().root.printError(cause.toString());
}
return false;
} catch (Throwable exc) {
Utils.getCause(exc).printStackTrace();
return false;
}
return true;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
public static boolean start(RootDoc root) {
String text = root.commentText();
if (text.length() < 64)
System.err.println("text: '" + text + "'");
else
System.err.println("text: '"
+ text.substring(0, 20)
+ "..."
+ text.substring(text.length() - 20)
+ "'");
return text.startsWith("ABC") && text.endsWith("XYZ");
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
@Override
public RootDoc installProxies(RootDoc obj) {
if (obj instanceof RootDocImpl) {
return new RootDocWrapper((RootDocImpl) obj);
}
return obj;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
@Override
public void installProxies(SarlConfiguration configuration) {
final RootDoc doc = configuration.root;
if (!(doc instanceof Proxy) && (doc instanceof RootDocImpl)) {
configuration.root = new RootDocWrapper((RootDocImpl) doc);
}
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
/**
* The entry point into parsing the javadoc.
*
* @param rootDoc
* The RootDoc intstance obtained via the doclet API
* @return The root node, containing everything parsed from javadoc doclet
*/
public Root parseRootDoc(RootDoc rootDoc) {
Root rootNode = objectFactory.createRoot();
for (ClassDoc classDoc : rootDoc.classes()) {
PackageDoc packageDoc = classDoc.containingPackage();
Package packageNode = packages.get(packageDoc.name());
if (packageNode == null) {
packageNode = parsePackage(packageDoc);
packages.put(packageDoc.name(), packageNode);
rootNode.getPackage().add(packageNode);
}
if (classDoc instanceof AnnotationTypeDoc) {
packageNode.getAnnotation().add(parseAnnotationTypeDoc((AnnotationTypeDoc) classDoc));
} else if (classDoc.isEnum()) {
packageNode.getEnum().add(parseEnum(classDoc));
} else if (classDoc.isInterface()) {
packageNode.getInterface().add(parseInterface(classDoc));
} else {
packageNode.getClazz().add(parseClass(classDoc));
}
}
return rootNode;
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
/**
* As specified by the Doclet specification.
*
* @param rootDoc The root doc.
*
* @return `true`, if process was successful.
*
* @see com.sun.javadoc.Doclet#start(RootDoc)
*/
public static boolean start(RootDoc rootDoc) {
final MarkdownTaglets markdownTaglets = MarkdownTaglets.instance();
Options options = new Options();
String[][] forwardedOptions = options.load(rootDoc.options(), rootDoc);
if ( forwardedOptions == null ) {
return false;
}
MarkdownDoclet doclet = new MarkdownDoclet(options, rootDoc);
markdownTaglets.setDocErrorReporter(doclet);
doclet.process();
if ( doclet.isError() ) {
return false;
}
RootDocWrapper rootDocWrapper = new RootDocWrapper(rootDoc, forwardedOptions);
if ( options.isHighlightEnabled() ) {
// find the footer option
int i = 0;
for ( ; i < rootDocWrapper.options().length; i++ ) {
if ( rootDocWrapper.options()[i][0].equals("-footer") ) {
rootDocWrapper.options()[i][1] += HIGHLIGHT_JS_HTML;
break;
}
}
if ( i >= rootDocWrapper.options().length ) {
rootDocWrapper.appendOption("-footer", HIGHLIGHT_JS_HTML);
}
}
return Standard.start(rootDocWrapper) && doclet.postProcess();
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
public void generateDoc(Operator op, RootDoc rootDoc, PrintWriter out) {
ClassDoc opDoc = rootDoc.classNamed(op.getClass().getName());
// name
out.println(op.getOperatorDescription().getName());
// Description
out.println(transformHTMLJavadocComment(opDoc.commentText(), op.getClass(), op.getOperatorDescription().getName()));
out.println("#####");
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
public static boolean start(RootDoc root) {
for (ClassDoc d : root.classes()) {
for (AnnotationDesc a : d.annotations()) {
System.out.println(a.annotationType());
}
}
return true;
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
/**
* Creates a new BatchEnvironment with the specified RootDoc.
**/
public BatchEnvironment(RootDoc rootDoc) {
this.rootDoc = rootDoc;
/*
* Initialize cached ClassDoc for types used by rmic. Note
* that any of these could be null if the boot class path is
* incorrect, which could cause a NullPointerException later.
*/
docRemote = rootDoc().classNamed(REMOTE);
docException = rootDoc().classNamed(EXCEPTION);
docRemoteException = rootDoc().classNamed(REMOTE_EXCEPTION);
docRuntimeException = rootDoc().classNamed(RUNTIME_EXCEPTION);
}
public static void setRootDoc(RootDoc rootDoc) {
DocLogger.rootDoc = rootDoc;
}
/**
* Doclet class entry point.
**/
public static boolean start(RootDoc rootDoc) {
/*
* Find batch ID among javadoc options, and retrieve
* corresponding batch data from global table.
*/
long batchID = -1;
for (String[] option : rootDoc.options()) {
if (option[0].equals("-batchID")) {
try {
batchID = Long.parseLong(option[1]);
} catch (NumberFormatException e) {
throw new AssertionError(e);
}
}
}
Batch batch = batchTable.get(batchID);
assert batch != null;
/*
* Construct batch environment using class agreed upon by
* generator implementations.
*/
BatchEnvironment env;
try {
Constructor<? extends BatchEnvironment> cons =
batch.envClass.getConstructor(new Class<?>[] { RootDoc.class });
env = cons.newInstance(rootDoc);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InstantiationException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
throw new AssertionError(e);
}
env.setVerbose(batch.verbose);
/*
* Determine the destination directory (the top of the package
* hierarchy) for the output of this batch; if no destination
* directory was specified on the command line, then the
* default is the current working directory.
*/
File destDir = batch.destDir;
if (destDir == null) {
destDir = new File(System.getProperty("user.dir"));
}
/*
* Run each input class through each generator.
*/
for (String inputClassName : batch.classes) {
ClassDoc inputClass = rootDoc.classNamed(inputClassName);
try {
for (Generator gen : batch.generators) {
gen.generate(env, inputClass, destDir);
}
} catch (NullPointerException e) {
/*
* We assume that this means that some class that was
* needed (perhaps even a bootstrap class) was not
* found, and that javadoc has already reported this
* as an error. There is nothing for us to do here
* but try to continue with the next input class.
*
* REMIND: More explicit error checking throughout
* would be preferable, however.
*/
}
}
/*
* Compile any generated source files, if configured to do so.
*/
boolean status = true;
List<File> generatedFiles = env.generatedFiles();
if (!batch.noCompile && !batch.noWrite && !generatedFiles.isEmpty()) {
status = batch.enclosingMain().invokeJavac(batch, generatedFiles);
}
/*
* Delete any generated source files, if configured to do so.
*/
if (!batch.keepGenerated) {
for (File file : generatedFiles) {
file.delete();
}
}
return status;
}
public static boolean start(RootDoc root) {
lastCaller = root.classNamed("pkg.C").commentText().trim();
return true;
}