xiaour / spring.boot.sapi.starter

🍃一个简单的Restful API输出工具,只需一行配置,即可将接口中的所有API接口及参数全部以结构化的方式输出的页面上。
Apache License 2.0
105 stars 35 forks source link

获取controller的class文件建议使用spring的工具类,在jar内class根本无法找到 #2

Closed dyj2012 closed 6 years ago

dyj2012 commented 6 years ago

private static Set getClassName(String filePath) throws ClassNotFoundException { Set classes= new HashSet(); filePath = ClassLoader.getSystemResource("").getPath() + filePath.replace(".", "/"); File file = new File(filePath);

    File[] childFiles = file.listFiles();
    if(childFiles==null){
        throw new ClassNotFoundException(filePath+" is not found,Please check the aplication's annotation in @Sapi(controllers) ");
    }
    for (File childFile : childFiles) {
        String childFilePath = childFile.getPath();
        childFilePath = childFilePath.substring(childFilePath.indexOf("/classes") + 9,childFilePath.length());
        childFilePath=childFilePath.replaceAll(".class","");
        childFilePath=childFilePath.replaceAll("/",".");
        classes.add(Class.forName(childFilePath));
    }

    return classes;
}
xiaour commented 6 years ago

嗯,很好的建议,我当时想的是尽量绕开三方jar,但现在看来是绕不开了