zoukankan      html  css  js  c++  java
  • 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");
        }
    }

    运行结果如下

  • 相关阅读:
    一行命令搞定AD数据库备份
    PowerShell实战4:批量修改AD账户EMail属性
    PowerShell实战1:Ping_Test
    分享/etc/sysctl.conf和/boot/loader.conf优化
    PowerShell实战5: 批量增加AD组成员
    bsd压力测试工具
    kernel : arp .....的訊息
    将域控制器移到新的 Active Directory 站
    pf限速限链接数
    FreeBSD笔记
  • 原文地址:https://www.cnblogs.com/ph4nt0mer/p/12463230.html
Copyright © 2011-2022 走看看