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

    运行结果如下

  • 相关阅读:
    elk+redis
    elk7.4+filebeat收集日志
    k8s-高可用集群实现(keepalived+haproxy)
    k8s-高可用集群实现(keepalived)
    keepalived(双主模式)+haproxy+mysql_slave
    haproxy-实现mysql多slave读负载均衡
    MySQL数据库的配置
    前端模块化(AMD和CMD、CommonJs)
    一分钟配置jdk
    MySQL基础语法
  • 原文地址:https://www.cnblogs.com/ph4nt0mer/p/12463230.html
Copyright © 2011-2022 走看看