最近在写javadoc方面的内容,本打算自己通过反射的机制拿到关于类中各种信息, 分析了maven的javadoc插件之后,发现它使用了qdox的开源包。试用了一把,还是不错的,大概情况如下:
1、加入依赖:
- <dependency>
- <groupId>com.thoughtworks.qdox</groupId>
- <artifactId>qdox</artifactId>
- <version>1.9.2</version>
- </dependency>
- JavaDocBuilder builder = new JavaDocBuilder();
- builder.addSource(new File("D:/java/com/alibaba/normandy/advice/AdviceHandlerTest.java"));
-
- JavaPackage[] packages = builder.getPackages();
- for (JavaPackage javaPackage : packages) {
- System.out.println(javaPackage.toString());
- }
- JavaSource[] javaSources = builder.getSources();
- for(JavaSource javaSource:javaSources ){
- System.out.println(javaSource.toString());
- }
输出:
- package com.alibaba.normandy.advice //读取出来的包名
- package com.alibaba.normandy.advice;
-
- import org.junit.Assert.*;
- import org.junit.Test;
-
- /**
- * @since 1.1
- * @version 1.1
- * @author keju.wangkj
- */
- public class AdviceHandlerTest {
-
- /**
- * @step guid对应的activatedValue不存在
- * @expected 在Config_Value表中成功插入新记录
- * @expected 返回值1 (version = 1)
- SimpleConfigValue中使用的StringUtil.defaultIfNull(simpleValue)是否有必要
- */
- public void testBeforeServiceActions() {
-
- fail("Not yet implemented");
- }
-
- public void testAfterServiceActions() {
-
- fail("Not yet implemented");
- }
-
- public void testExceptionServiceActions() {
-
- fail("Not yet implemented");
- }
-
- }
3、取得方法的tags
- JavaClass[] javaClasses = builder.getClasses();
- for (JavaClass javaClass : javaClasses) {
- JavaMethod[] javaMethods = javaClass.getMethods();
- for (JavaMethod javaMethod : javaMethods) {
- DocletTag[] docTags = javaMethod.getTags();
- for (DocletTag docletTag : docTags) {
- System.out.println(docletTag.getName() + "->" + docletTag.getValue());
- }
- }
- }
源文件:
-
-
-
-
-
-
- @Test
- public void test1() {
-
- }
-
-
-
-
-
-
-
- @Ignore
- @Test
- public void test2() {
-
- }
测试输出:
param->p1, p2
since->2011
param->p1, p2
since->2011
-------------------------------------------------------------