我需要知道如何在运行时读Javadoc注释如何通过反射阅读Javadoc评论?
说我有以下功能(可能是反射?):
/**
* function that do some thing
*/
public void myFunc()
{
//...
}
在运行时,我能得到这个功能多的信息通过反思..但不能读取评论。所以问题是,如何在运行时阅读这个javadoc评论。
我需要知道如何在运行时读Javadoc注释如何通过反射阅读Javadoc评论?
说我有以下功能(可能是反射?):
/**
* function that do some thing
*/
public void myFunc()
{
//...
}
在运行时,我能得到这个功能多的信息通过反思..但不能读取评论。所以问题是,如何在运行时阅读这个javadoc评论。
考虑使用注释代替Javadoc并编写注释处理器。
你不能。他们从编译后的代码中被删除。
你不能那样做,因为javadoc没有被编译到最终的类中。
Doclet类:
public class ExtractCommentsDoclet {
public static boolean start(RootDoc root) throws IOException {
for (ClassDoc c : root.classes()) {
print(c.qualifiedName(), c.commentText());
for (FieldDoc f : c.fields(false)) {
print(f.qualifiedName(), f.commentText());
}
for (MethodDoc m : c.methods(false)) {
print(m.qualifiedName(), m.commentText());
if (m.commentText() != null && m.commentText().length() > 0) {
for (ParamTag p : m.paramTags())
print(m.qualifiedName() + "@" + p.parameterName(), p.parameterComment());
for (Tag t : m.tags("return")) {
if (t.text() != null && t.text().length() > 0)
print(m.qualifiedName() + "@return", t.text());
}
}
}
}
return true;
}
private static void print(String name, String comment) throws IOException {
if (comment != null && comment.length() > 0) {
new FileWriter(name + ".txt").append(comment).close();
}
}
}
和Maven执行:从类路径
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
<configuration>
<doclet>ExtractCommentsDoclet</doclet>
<docletPath>${project.build.directory}/classes</docletPath>
<reportOutputDirectory>${project.build.outputDirectory}/META-INF</reportOutputDirectory>
<useStandardDocletOptions>false</useStandardDocletOptions>
</configuration>
</plugin>
阅读文档:META-INF/apidocs
由于这里的2名回答说,他们不能用做编译后的代码,你应该可以使用源代码。为了做到这一点,请尝试搜索“从源代码提取javadoc”,然后获取一些开源工具并根据需要对其进行修改。 – 2011-12-14 12:06:39
我认为使用@Puce更容易,因为我是源代码的所有者! – 2011-12-14 12:41:22