java反射查看jar包中所有的类名方法名

不反编译,不用其他工具,用java反射查看jar包中所有的类名方法名,网上很多都报错,下面这个你试试看:话不多说直接撸代码:

 1 import java.lang.reflect.Field;
 2 import java.lang.reflect.Method;
 3 import java.net.URL;
 4 import java.net.URLClassLoader;
 5 import java.util.Enumeration;
 6 import java.util.HashMap;
 7 import java.util.List;
 8 import java.util.Map;
 9 import java.util.jar.JarEntry;
10 import java.util.jar.JarFile;
11 
12 
13 
14 public class JarUtil extends URLClassLoader {  
15     /** 
16      * 这些默认方法不打印 
17      */  
18     private static String DEFAULT_METHOD = "waitequalsnotifynotifyAlltoStringhashCodegetClass"; 
19     
20     public JarUtil(URL url) {  
21         super(new URL[] { url });  
22     } 
23 
24     /** 
25      * 获取jar中某个class 
26      *  
27      * @param jarPath 
28      * @param classPath 
29      * @return 
30      */  
31     @SuppressWarnings({ "rawtypes", "resource" })
32     public static Object getClassObject(String jarPath) {  
33         try {  
34             if(jarPath.indexOf("file:///")<0){
35                 jarPath = "file:///"+jarPath;
36             }
37             URL url = new URL(jarPath);  
38             JarUtil t = new JarUtil(url);  
39             Map<String,List<String>> rtnList = new HashMap<String,List<String>>();
40            //通过jarFile和JarEntry得到所有的类  
41             JarFile jar = new JarFile(jarPath.replace("file:///",""));  
42             //返回zip文件条目的枚举  
43             Enumeration<JarEntry> enumFiles = jar.entries();  
44             JarEntry entry;  
45 
46             //测试此枚举是否包含更多的元素  
47             while(enumFiles.hasMoreElements()){  
48                 entry = (JarEntry)enumFiles.nextElement(); 
49                 if(entry.getName().indexOf("META-INF")<0 && entry.getName().indexOf("eclipse")<0 && entry.getName().indexOf(".class")>=0){
50                     String classFullName = entry.getName();
51                     //去掉后缀.class  
52                     String className = classFullName.substring(0,classFullName.length()-6).replace("/", "."); 
53                     System.out.println("~~~~~~~~~~~~~~~Class名称:" + className);  
54                     
55                     Class c ;
56                     try {
57                         c= t.findClass(className);
58                     } catch (LinkageError e) {
59                         c =   Class.forName(className); 
60                     }
61 
62                   //根据class对象获得属性  
63                   Field[] fields = c.getDeclaredFields();  
64                   for(Field f1 : fields){  
65                       //打印每个属性的类型  
66                       System.out.println("~~~~~~~~~~~~~~~属性类型:" + f1.getType());  
67                       //打印每个属性的名字  
68                       System.out.println("~~~~~~~~~~~~~~~属性名称:" + f1.getName());  
69                   }
70                   //通过getMethod得到类中包含的方法  
71                   Method methods[] = c.getMethods(); 
72                   for (Method method : methods) {
73                       String sm = method.getName(); 
74                     //打印除默认方法外的方法  
75                     if(DEFAULT_METHOD.indexOf(sm)<0){  
76                          System.out.println("方法名:" + sm);
77                     }
78 
79                   }
80 
81                 }
82             }
83 
84         } catch (Exception ex) {  
85             ex.printStackTrace();  
86         }  
87         return null;  
88     }  
89     
90     public static void main(String[] args) throws Exception {
91         
92         getClassObject("file:///C:\Users\zhang\Desktop\testrobot.jar");
93     }
94 }  
代码拷来总觉浅,绝知此事要躬行
原文地址:https://www.cnblogs.com/yinxiaoqiexuxing/p/6566644.html