java代码输出jar包里的类名,属性名,还有方法名

记录源码如下:

package ysoserial.test.exploit;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class getJarClassnameAndAttribute {
    public static void getJarName(String jarFile) throws Exception{

        try {
            File f = new File(jarFile);
            URL url1 = f.toURI().toURL();
            URLClassLoader myClassLoader = new URLClassLoader(new URL[]{url1},Thread.currentThread().getContextClassLoader());

            JarFile jar = new JarFile(jarFile);

            Enumeration<JarEntry> enumFiles = jar.entries();

            JarEntry entry;


            while (enumFiles.hasMoreElements()){
                entry = (JarEntry)enumFiles.nextElement();
                if(entry.getName().indexOf("META-INF")<0){
                    String classFullName = entry.getName();
                    if (classFullName.indexOf(".class")<0){
                        classFullName = classFullName.substring(0,classFullName.length()-1);

                    }else {
                        String className = classFullName.substring(0,classFullName.length()-6).replace("/",".");
                        Class<?> myclass = myClassLoader.loadClass(className);

                        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~打印类名:~~~~~~~~~~~~~~~~~~~~~~~~~" + className);

                        Class clazz = Class.forName(className);
                        Field[] fileds = clazz.getDeclaredFields();
                        for(Field f1:fileds){

                            System.out.println("~~~~~~~~~~~~~~~属性类型:" + f1.getType());
                            System.out.println("~~~~~~~~~~~~~~~属性名称:" + f1.getName());
                        }

                        Method[] methods = myclass.getMethods();

                        for (Method method:methods){
                            String methodName = method.getName();
                            System.out.println("方法名:"+methodName);
                            Class<?>[] parameterTypes = method.getParameterTypes();
                            for (Class<?> clas:parameterTypes){
                                String parameterName = clas.getSimpleName();
                                System.out.println("参数类型:"+parameterName);
                            }
                        }

                    }
                }
            }

        }
        catch (IOException e){
            e.printStackTrace();
        }
    }


    public static void main(String[] args)throws Exception{
        getJarName("commons-beanutils-1.9.2.jar");
    }
}

运行结果如下

原文地址:https://www.cnblogs.com/ph4nt0mer/p/12463230.html