gpt4 book ai didi

java - 在自定义 JavaDoc Taglet 中加载类?

转载 作者:搜寻专家 更新时间:2023-11-01 02:30:03 26 4
gpt4 key购买 nike

我正在编写自定义 JavaDoc Taglet,并希望加载和创建正在处理 JavaDoc 的类的实例。这是我的代码(公共(public)类 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 输出的一部分返回?

最佳答案

<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:...' \
...

关于java - 在自定义 JavaDoc Taglet 中加载类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11776981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com