我正在编写自定义JavaDoc Taglet,并希望加载并创建正在处理JavaDoc的类的实例。这是我的代码(公共类CDKIOOptionsTaglet实现Taglet):
public String toString(Tag tag) {
return "<DT><B>IO options: </B><DD>"
+ expand(tag) + "</DD>\n";
}
public String toString(Tag[] tags) {
if (tags.length == 0) {
return null;
} else {
return toString(tags[0]);
}
}
private String expand(Tag tag) {
// create a table with IOOptions
StringBuffer tableContent = new StringBuffer();
SourcePosition file = tag.position();
String pathAndFile = file.file().toString();
pathAndFile = pathAndFile.replaceAll("/", ".");
pathAndFile = pathAndFile.substring(pathAndFile.indexOf("src.main") + 9);
pathAndFile = pathAndFile.substring(0, pathAndFile.indexOf(".java"));
try {
Class ioClass = Class.forName(pathAndFile);
Object ioInstance = ioClass.newInstance();
if (ioInstance instanceof IChemObjectIO) {
IChemObjectIO objectIO = (IChemObjectIO)ioInstance;
tableContent.append("<table>");
for (IOSetting setting : objectIO.getIOSettings()) {
tableContent.append("<tr>");
tableContent.append("<td>" + setting.getName() + "</td>");
tableContent.append("<td></td>");
tableContent.append("</tr>");
}
tableContent.append("</table>");
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
return tableContent.toString();
}
这是包含jar的Ant脚本,其中要加载的类被添加到调用期间使用的类路径中:
<classpath>
<pathelement location="." />
<fileset dir="dist/jar">
<include name="*.jar" />
</fileset>
<javadoc ...>
</classpath>
我正在创建JavaDoc的类所在的jar位于dist / jar中。为了完整性(并根据要求),这是完整的电话:
<javadoc destdir="${javadoc.dir}"
author="true" version="true" use="true"
public="true"
windowtitle="CDK API - version ${TODAY_SVN}">
<classpath>
<pathelement location="." />
<fileset dir="dist/jar">
<include name="*.jar" />
</fileset>
<fileset dir="jar">
<include name="*.jar" />
</fileset>
<fileset dir="jar/jena">
<include name="*.jar" />
</fileset>
<dirset dir="src/main">
<include name="net/**/*"/>
</dirset>
</classpath>
<tag name="cdk.set" scope="all" description="Belongs to the set:" />
<tag name="cdk.keyword" scope="all" description="Keywords:" />
<tag name="cdk.created" scope="all" description="Created on:" />
<tag name="cdk.require" scope="all" description="Requires:" />
<tag name="cdk.depends" scope="all" description="Required library to be able to run this class:" />
<tag name="cdk.builddepends" scope="all" description="Required library to be able to compile this class:" />
<tag name="cdk.todo" scope="all" description="To do:" />
<tag name="cdk.license" scope="all" description="License:" />
<taglet name="net.sf.cdk.tools.doclets.CDKCiteTaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKModuleTaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKBugTaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKDictRefTaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKInChITaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKThreadSafeTaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKThreadNonSafeTaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKGitTaglet" path="jar/xom-1.1.jar:src/main" />
<taglet name="net.sf.cdk.tools.doclets.CDKIOOptionsTaglet" path="jar/xom-1.1.jar:src/main" />
<packageset dir="${src}/main" defaultexcludes="yes">
<include name="org/openscience/cdk/**" />
</packageset>
<link href="${javaURL}"/>
</javadoc>
我用“ant -v”确认适当的jar确实包含了正在加载的类。不过,我得到了这个堆栈跟踪:
[javadoc] java.lang.ClassNotFoundException: org.openscience.cdk.Atom
[javadoc] at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
[javadoc] at java.security.AccessController.doPrivileged(Native Method)
[javadoc] at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
[javadoc] at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
[javadoc] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
[javadoc] at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
[javadoc] at net.sf.cdk.tools.doclets.CDKIOOptionsTaglet.expand(CDKIOOptionsTaglet.java:104)
[javadoc] at net.sf.cdk.tools.doclets.CDKIOOptionsTaglet.toString(CDKIOOptionsTaglet.java:83)
[javadoc] at net.sf.cdk.tools.doclets.CDKIOOptionsTaglet.toString(CDKIOOptionsTaglet.java:91)
[javadoc] at com.sun.tools.doclets.internal.toolkit.taglets.LegacyTaglet.getTagletOutput(LegacyTaglet.java:130)
[javadoc] at com.sun.tools.doclets.internal.toolkit.taglets.TagletWriter.genTagOuput(TagletWriter.java:185)
[javadoc] at com.sun.tools.doclets.formats.html.HtmlDocletWriter.printTags(HtmlDocletWriter.java:230)
[javadoc] at com.sun.tools.doclets.formats.html.ClassWriterImpl.writeClassTagInfo(ClassWriterImpl.java:246)
[javadoc] at com.sun.tools.doclets.internal.toolkit.builders.ClassBuilder.buildClassTagInfo(ClassBuilder.java:287)
如何让Taglet加载它正在创建JavaDoc的类,以便我可以使用反射(等)来了解该类的更多信息,并将其作为Taglet输出的一部分返回?
答案 0 :(得分:5)
<classpath>
配置不是声明实例化的类的正确位置。由于taglet实例化了类,因此需要将这些类添加到taglet的类路径中:
<taglet name="net.sf.cdk.tools.doclets.CDKCiteTaglet">
<path>
<pathelement location="." />
<fileset dir="dist/jar">
<include name="*.jar" />
</fileset>
</path>
</taglet>
在命令行中,它应如下所示:
javadoc \
...
-taglet 'net.sf.cdk.tools.doclets.CDKCiteTaglet' \
-tagletpath '/.../jar/xom-1.1.jar:/.../jar/my-lib1.jar:/.../jar/my-lib2.jar:...' \
...