运用反射原理 + POI工具写一个java接口doc工具 (扫描包下接口并生成简单接口文档)

17 篇文章 0 订阅
2 篇文章 0 订阅

目标:通过java反射的方式,获取一个包结构下所有的接口数据。之后通过POI工具生成一份Excel文档,来实现最简单的JavaInterfaceDoc.

相应源码以上传至基友网站:https://github.com/liujinghui1994/JavaInterfaceDocTool.git

首先我们先定义一个Excel数据的VO:

package com.util.doctool;

public class ExcelDataVO {

    private String sourcePackageName;
    private String sourceFileName;
    private String sourceFunctionName;
    private String param;
    private String returnType;

    public String getSourcePackageName() {
        return sourcePackageName;
    }

    public void setSourcePackageName(String sourcePackageName) {
        this.sourcePackageName = sourcePackageName;
    }

    public String getSourceFileName() {
        return sourceFileName;
    }

    public void setSourceFileName(String sourceFileName) {
        this.sourceFileName = sourceFileName;
    }

    public String getSourceFunctionName() {
        return sourceFunctionName;
    }

    public void setSourceFunctionName(String sourceFunctionName) {
        this.sourceFunctionName = sourceFunctionName;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getReturnType() {
        return returnType;
    }

    public void setReturnType(String returnType) {
        this.returnType = returnType;
    }

    public ExcelDataVO(String sourcePackageName, String sourceFileName, String sourceFunctionName, String param, String returnType) {
        this.sourcePackageName = sourcePackageName;
        this.sourceFileName = sourceFileName;
        this.sourceFunctionName = sourceFunctionName;
        this.param = param;
        this.returnType = returnType;
    }

    public ExcelDataVO() {
    }
}

其次我们编写主程序:

package com.util.doctool;

import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

public class JavaInterfaceDocTool {

    private static final List<ExcelDataVO> result = new ArrayList<ExcelDataVO>();

    public static void getAllFile(String filePath){
        File dir = new File(filePath);
        File[] files = dir.listFiles();
        if(files != null){
            for(int i=0;i<files.length;i++){
                String fileName = files[i].getName();
                if(files[i].isDirectory()){
                    getAllFile(files[i].getAbsolutePath());
                }else if(fileName.endsWith(".class")){
                    try{
                        String keyWord = "\\com\\";
                        String absPth = files[i].getCanonicalPath();
                        absPth = absPth.substring(absPth.indexOf(keyWord)+1,absPth.length()-6);
                        absPth = absPth.replace("\\",".");
                        Class c = Class.forName(absPth);
                        if(c.isInterface()){
                            Method[] methods = c.getDeclaredMethods();
                            for(int j=0;j<methods.length;j++){
                                ExcelDataVO e = new ExcelDataVO();
                                e.setSourcePackageName(c.getPackage().getName().toString());
                                e.setSourceFileName(c.getSimpleName().toString());
                                e.setSourceFunctionName(methods[j].getName().toString());
                                e.setParam(getParams(methods[j].getParameters()));
                                e.setReturnType(methods[j].getGenericReturnType().getTypeName().toString());
                                result.add(e);
                            }
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static String getParams(Parameter[] params){
        StringBuilder typeString = new StringBuilder("[");
        for(int i=0;i<params.length;i++){
            typeString.append(params[i].getName().toString());
            if(i != params.length-1){
                typeString.append(",");
            }
        }
        typeString.append("]");
        return typeString.toString();
    }


    public static void excelTool(List<ExcelDataVO> result) throws IOException {
        OutputStream out = new FileOutputStream("D:\\导出接口Doc.xlsx");
        SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
        SXSSFSheet sxssfSheet = sxssfWorkbook.createSheet("InterfaceDoc");
        SXSSFRow sxssfRowTitle = sxssfSheet.createRow(0);
        sxssfRowTitle.createCell(0).setCellValue("包名");
        sxssfRowTitle.createCell(1).setCellValue("接口名");
        sxssfRowTitle.createCell(2).setCellValue("方法名");
        sxssfRowTitle.createCell(3).setCellValue("参数名");
        sxssfRowTitle.createCell(4).setCellValue("返回值类型");

        int index = 1;
        for(ExcelDataVO excelDataVO : result){
            SXSSFRow sxssfRowDetail = sxssfSheet.createRow(index);
            sxssfRowDetail.createCell(0).setCellValue(excelDataVO.getSourcePackageName()==null?"":excelDataVO.getSourcePackageName());
            sxssfRowDetail.createCell(1).setCellValue(excelDataVO.getSourceFileName()==null?"":excelDataVO.getSourceFileName());
            sxssfRowDetail.createCell(2).setCellValue(excelDataVO.getSourceFunctionName()==null?"":excelDataVO.getSourceFunctionName());
            sxssfRowDetail.createCell(3).setCellValue(excelDataVO.getParam()==null?"":excelDataVO.getParam());
            sxssfRowDetail.createCell(4).setCellValue(excelDataVO.getReturnType()==null?"":excelDataVO.getReturnType());
            index++;
        }
        sxssfWorkbook.write(out);
    }

    public static void main(String[] args) {
        String filePath = "D:\\IDEAWorkSpace\\JavaInterfaceDocTool";
        try{
            getAllFile(filePath);
            excelTool(result);
        }catch(Exception e){
            e.printStackTrace();
        }
    }



}

因为我用的是Maven,并且添加了打包插件,所以pom也相应列出一下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>JavaInterfaceDocTool</groupId>
    <artifactId>JavaInterfaceDocTool</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>

    </dependencies>

    <build>

        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>mapping/*.xml</include>
                    <include>*.xml</include>
                    <include>*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>*.xml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <useUniqueVersions>false</useUniqueVersions>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.util.doctool.JavaInterfaceDocTool</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>

                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>

                <!-- 编码和编译和JDK版本 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>utf8</encoding>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>
    </build>
</project>

ok 到此为止主程序写完了,下面我们要编写几个测试的接口,用于导出Doc。

package com.util.doctool;

public interface TestInterfaceA {
    public void add(String name,String age);
    public void delete();
    public void queryStudent(String name);
}


package com.util.doctool;

public interface TestInterfaceB {
    public void doA();
    public void doB();
}

大家可以看到我见了两个接口 Test..A 和 Test..B。由于加了打包插件,会自动生成.class文件。

最后我们测试一下代码。开始执行main方法!

可以看到 ,很简单的javadoc小工具做好了 大家自己试试吧。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值