package com.kehaoinfo.rmip.portal.webmvc;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class getClassMessageUtil{
public static void main(String[] args) {
String map[] = {"包名"};
List<Map<String,String>> list = new ArrayList<>();
for(String m : map){
list.addAll(creatDoc(m));
}
try {
//设置纸张大小
Document document = new Document(PageSize.A4);
//建立一个书写器,与document对象关联
RtfWriter2.getInstance(document, new FileOutputStream("d:文档说明.doc"));
Table table=null;
document.open();
for(Map<String,String> m :list){
table = createDocContext("说明",m,document);
document.add(table);
}
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<Map<String,String>> creatDoc(String pac){
List<Map<String,String>> list = new ArrayList<>();
try {
Map<String,String> map = null;
Class c = null;
Method[] mothod = null;
Set<String> classNames = getClassMessageUtil.getClassName(pac, false);
if (classNames != null) {
for (String className : classNames) {
String classAnns = "";
String s = "";
c = Class.forName(className);
Annotation[] classAnn = c.getAnnotations();
for(Annotation a : classAnn){
s += a;
}
// String t = "";
String mping = "";
/*if(s.indexOf("RestController(value=")>1){
t = s.substring(s.indexOf("RestController(value="),s.length());
t = t.substring(0,t.indexOf(")")+1);
}*/
if(s.indexOf("RequestMapping")>0){
mping = s.substring(s.indexOf("value=[")+7,s.length());
mping = mping.substring(0,mping.indexOf("]"));
}
classAnns =mping;
mothod = c.getDeclaredMethods();
for(Method m : mothod){
map = new HashMap<>();
String ann = "";
String want = "";
String dress = "";
map.put("className", className.substring(className.lastIndexOf(".")+1,className.length()));
Annotation an[]= m.getDeclaredAnnotations();
for(Annotation a : an){
ann+=a;
}
if(ann.indexOf("RequestMapping")>0){
want="页面跳转 ";
dress = ann.substring(ann.indexOf("value=[")+7,ann.length());
dress = dress.substring(0,dress.indexOf("]"));
}
if(ann.indexOf("ResponseBody")>0){
want=" 数据返回";
}
map.put("dress", mping+dress);
map.put("annotations", want);
map.put("methodName", m.getName());
map.put("classAnns", classAnns);
list.add(map);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list;
}
public static Table createDocContext(String contextString,Map<String,String> data, Document document)throws DocumentException, IOException{
//正文字体风格
Font contextFont = new Font(10,Font.NORMAL);
Paragraph context = new Paragraph(contextString);
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
//设置第一行空的列数
context.setFirstLineIndent(20);
document.add(context);
//设置Table表格,创建一个表格
Table table = new Table(2);
int width[] = {20,80};//设置每列宽度比例
table.setWidths(width);
table.setWidth(100);//占页面宽度比例
table.setAlignment(Element.ALIGN_CENTER);//居中
table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.setAutoFillEmptyCells(true);//自动填满
table.setBorderWidth(1);//边框宽度
Font fontChinese = new Font(14,12,Font.BOLD,Color.BLACK);
Cell cell = new Cell(new Paragraph("描述",fontChinese));
table.addCell(cell);
table.addCell(new Cell(""));
cell = new Cell(new Paragraph("路径",fontChinese));
table.addCell(cell);
table.addCell(new Cell(data.get("dress")));
cell = new Cell(new Paragraph("类及方法",fontChinese));
table.addCell(cell);
table.addCell(new Cell(data.get("className")+"#"+data.get("methodName")));
cell = new Cell(new Paragraph("用途",fontChinese));
table.addCell(cell);
table.addCell(new Cell(data.get("annotations")));
return table;
}
/**
* 获取某包下所有类
* @param packageName 包名
* @param isRecursion 是否遍历子包
* @return 类的完整名称
*/
public static Set<String> getClassName(String packageName, boolean isRecursion) {
Set<String> classNames = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String packagePath = packageName.replace(".", "/");
URL url = loader.getResource(packagePath);
if (url != null) {
String protocol = url.getProtocol();
if (protocol.equals("file")) {
classNames = getClassNameFromDir(url.getPath(), packageName, isRecursion);
} else if (protocol.equals("jar")) {
JarFile jarFile = null;
try{
jarFile = ((JarURLConnection) url.openConnection()).getJarFile();
} catch(Exception e){
e.printStackTrace();
}
if(jarFile != null){
getClassNameFromJar(jarFile.entries(), packageName, isRecursion);
}
}
} else {
/*从所有的jar包中查找包名*/
classNames = getClassNameFromJars(((URLClassLoader)loader).getURLs(), packageName, isRecursion);
}
return classNames;
}
/**
* 从项目文件获取某包下所有类
* @param filePath 文件路径
* @param className 类名集合
* @param isRecursion 是否遍历子包
* @return 类的完整名称
*/
private static Set<String> getClassNameFromDir(String filePath, String packageName, boolean isRecursion) {
Set<String> className = new HashSet<String>();
File file = new File(filePath);
File[] files = file.listFiles();
for (File childFile : files) {
if (childFile.isDirectory()) {
if (isRecursion) {
className.addAll(getClassNameFromDir(childFile.getPath(), packageName+"."+childFile.getName(), isRecursion));
}
} else {
String fileName = childFile.getName();
if (fileName.endsWith(".class") && !fileName.contains("$")) {
className.add(packageName+ "." + fileName.replace(".class", ""));
}
}
}
return className;
}
/**
* @param jarEntries
* @param packageName
* @param isRecursion
* @return
*/
private static Set<String> getClassNameFromJar(Enumeration<JarEntry> jarEntries, String packageName, boolean isRecursion){
Set<String> classNames = new HashSet<String>();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
if(!jarEntry.isDirectory()){
/*
* 这里是为了方便,先把"/" 转成 "." 再判断 ".class" 的做法可能会有bug
* (FIXME: 先把"/" 转成 "." 再判断 ".class" 的做法可能会有bug)
*/
String entryName = jarEntry.getName().replace("/", ".");
if (entryName.endsWith(".class") && !entryName.contains("$") && entryName.startsWith(packageName)) {
entryName = entryName.replace(".class", "");
if(isRecursion){
classNames.add(entryName);
} else if(!entryName.replace(packageName+".", "").contains(".")){
classNames.add(entryName);
}
}
}
}
return classNames;
}
/**
* 从所有jar中搜索该包,并获取该包下所有类
* @param urls URL集合
* @param packageName 包路径
* @param isRecursion 是否遍历子包
* @return 类的完整名称
*/
private static Set<String> getClassNameFromJars(URL[] urls, String packageName, boolean isRecursion) {
Set<String> classNames = new HashSet<String>();
for (int i = 0; i < urls.length; i++) {
String classPath = urls[i].getPath();
//不必搜索classes文件夹
if (classPath.endsWith("classes/")) {continue;}
JarFile jarFile = null;
try {
jarFile = new JarFile(classPath.substring(classPath.indexOf("/")));
} catch (IOException e) {
e.printStackTrace();
}
if (jarFile != null) {
classNames.addAll(getClassNameFromJar(jarFile.entries(), packageName, isRecursion));
}
}
return classNames;
}
}
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class getClassMessageUtil{
public static void main(String[] args) {
String map[] = {"包名"};
List<Map<String,String>> list = new ArrayList<>();
for(String m : map){
list.addAll(creatDoc(m));
}
try {
//设置纸张大小
Document document = new Document(PageSize.A4);
//建立一个书写器,与document对象关联
RtfWriter2.getInstance(document, new FileOutputStream("d:文档说明.doc"));
Table table=null;
document.open();
for(Map<String,String> m :list){
table = createDocContext("说明",m,document);
document.add(table);
}
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<Map<String,String>> creatDoc(String pac){
List<Map<String,String>> list = new ArrayList<>();
try {
Map<String,String> map = null;
Class c = null;
Method[] mothod = null;
Set<String> classNames = getClassMessageUtil.getClassName(pac, false);
if (classNames != null) {
for (String className : classNames) {
String classAnns = "";
String s = "";
c = Class.forName(className);
Annotation[] classAnn = c.getAnnotations();
for(Annotation a : classAnn){
s += a;
}
// String t = "";
String mping = "";
/*if(s.indexOf("RestController(value=")>1){
t = s.substring(s.indexOf("RestController(value="),s.length());
t = t.substring(0,t.indexOf(")")+1);
}*/
if(s.indexOf("RequestMapping")>0){
mping = s.substring(s.indexOf("value=[")+7,s.length());
mping = mping.substring(0,mping.indexOf("]"));
}
classAnns =mping;
mothod = c.getDeclaredMethods();
for(Method m : mothod){
map = new HashMap<>();
String ann = "";
String want = "";
String dress = "";
map.put("className", className.substring(className.lastIndexOf(".")+1,className.length()));
Annotation an[]= m.getDeclaredAnnotations();
for(Annotation a : an){
ann+=a;
}
if(ann.indexOf("RequestMapping")>0){
want="页面跳转 ";
dress = ann.substring(ann.indexOf("value=[")+7,ann.length());
dress = dress.substring(0,dress.indexOf("]"));
}
if(ann.indexOf("ResponseBody")>0){
want=" 数据返回";
}
map.put("dress", mping+dress);
map.put("annotations", want);
map.put("methodName", m.getName());
map.put("classAnns", classAnns);
list.add(map);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list;
}
public static Table createDocContext(String contextString,Map<String,String> data, Document document)throws DocumentException, IOException{
//正文字体风格
Font contextFont = new Font(10,Font.NORMAL);
Paragraph context = new Paragraph(contextString);
context.setAlignment(Element.ALIGN_LEFT);
context.setFont(contextFont);
//设置第一行空的列数
context.setFirstLineIndent(20);
document.add(context);
//设置Table表格,创建一个表格
Table table = new Table(2);
int width[] = {20,80};//设置每列宽度比例
table.setWidths(width);
table.setWidth(100);//占页面宽度比例
table.setAlignment(Element.ALIGN_CENTER);//居中
table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
table.setAutoFillEmptyCells(true);//自动填满
table.setBorderWidth(1);//边框宽度
Font fontChinese = new Font(14,12,Font.BOLD,Color.BLACK);
Cell cell = new Cell(new Paragraph("描述",fontChinese));
table.addCell(cell);
table.addCell(new Cell(""));
cell = new Cell(new Paragraph("路径",fontChinese));
table.addCell(cell);
table.addCell(new Cell(data.get("dress")));
cell = new Cell(new Paragraph("类及方法",fontChinese));
table.addCell(cell);
table.addCell(new Cell(data.get("className")+"#"+data.get("methodName")));
cell = new Cell(new Paragraph("用途",fontChinese));
table.addCell(cell);
table.addCell(new Cell(data.get("annotations")));
return table;
}
/**
* 获取某包下所有类
* @param packageName 包名
* @param isRecursion 是否遍历子包
* @return 类的完整名称
*/
public static Set<String> getClassName(String packageName, boolean isRecursion) {
Set<String> classNames = null;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String packagePath = packageName.replace(".", "/");
URL url = loader.getResource(packagePath);
if (url != null) {
String protocol = url.getProtocol();
if (protocol.equals("file")) {
classNames = getClassNameFromDir(url.getPath(), packageName, isRecursion);
} else if (protocol.equals("jar")) {
JarFile jarFile = null;
try{
jarFile = ((JarURLConnection) url.openConnection()).getJarFile();
} catch(Exception e){
e.printStackTrace();
}
if(jarFile != null){
getClassNameFromJar(jarFile.entries(), packageName, isRecursion);
}
}
} else {
/*从所有的jar包中查找包名*/
classNames = getClassNameFromJars(((URLClassLoader)loader).getURLs(), packageName, isRecursion);
}
return classNames;
}
/**
* 从项目文件获取某包下所有类
* @param filePath 文件路径
* @param className 类名集合
* @param isRecursion 是否遍历子包
* @return 类的完整名称
*/
private static Set<String> getClassNameFromDir(String filePath, String packageName, boolean isRecursion) {
Set<String> className = new HashSet<String>();
File file = new File(filePath);
File[] files = file.listFiles();
for (File childFile : files) {
if (childFile.isDirectory()) {
if (isRecursion) {
className.addAll(getClassNameFromDir(childFile.getPath(), packageName+"."+childFile.getName(), isRecursion));
}
} else {
String fileName = childFile.getName();
if (fileName.endsWith(".class") && !fileName.contains("$")) {
className.add(packageName+ "." + fileName.replace(".class", ""));
}
}
}
return className;
}
/**
* @param jarEntries
* @param packageName
* @param isRecursion
* @return
*/
private static Set<String> getClassNameFromJar(Enumeration<JarEntry> jarEntries, String packageName, boolean isRecursion){
Set<String> classNames = new HashSet<String>();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
if(!jarEntry.isDirectory()){
/*
* 这里是为了方便,先把"/" 转成 "." 再判断 ".class" 的做法可能会有bug
* (FIXME: 先把"/" 转成 "." 再判断 ".class" 的做法可能会有bug)
*/
String entryName = jarEntry.getName().replace("/", ".");
if (entryName.endsWith(".class") && !entryName.contains("$") && entryName.startsWith(packageName)) {
entryName = entryName.replace(".class", "");
if(isRecursion){
classNames.add(entryName);
} else if(!entryName.replace(packageName+".", "").contains(".")){
classNames.add(entryName);
}
}
}
}
return classNames;
}
/**
* 从所有jar中搜索该包,并获取该包下所有类
* @param urls URL集合
* @param packageName 包路径
* @param isRecursion 是否遍历子包
* @return 类的完整名称
*/
private static Set<String> getClassNameFromJars(URL[] urls, String packageName, boolean isRecursion) {
Set<String> classNames = new HashSet<String>();
for (int i = 0; i < urls.length; i++) {
String classPath = urls[i].getPath();
//不必搜索classes文件夹
if (classPath.endsWith("classes/")) {continue;}
JarFile jarFile = null;
try {
jarFile = new JarFile(classPath.substring(classPath.indexOf("/")));
} catch (IOException e) {
e.printStackTrace();
}
if (jarFile != null) {
classNames.addAll(getClassNameFromJar(jarFile.entries(), packageName, isRecursion));
}
}
return classNames;
}
}