• 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");
        }
    }

    运行结果如下

  • 相关阅读:
    webform文件上传加水印
    2017-6-6 ASP.NET Ajax版页面无刷新三级联动
    2017-6-5 Ajax应用
    转【 正则表达式】
    2017-6-4 JQuery中的选择器和动画 弹窗遮罩
    Linq 组合分页查询
    2017-6-2 Linq高级查询和函数
    2017-6-3 JQuery中的Dom操作和事件
    WebForm母版页
    WebForm内置对象:Application和ViewState、Repeater的Command用法
  • 原文地址:https://www.cnblogs.com/ph4nt0mer/p/12463230.html
Copyright © 2020-2023  润新知