以编程方式分析jar文件 [英] analyze jar file programmatically

查看:105
本文介绍了以编程方式分析jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式计算给定jar文件中已编译的类,接口和枚举的数量(因此我需要三个单独的数字)。哪种API对我有帮助? (我不能使用第三方库。)

I need to count the number of compiled classes, interfaces and enums in a given jar file programmatically (so I need three separate numbers). Which API would help me? (I can't use third party libraries.)

我已经尝试了相当棘手的方案,似乎并不总是正确的。也就是说,我将每个ZipEntry读入一个byte [],然后将结果提供给我的自定义类加载器,它扩展了标准的CalssLoader,并将此byte []发送到ClassLoader.defineClass(这是保护,无法直接从应用程序代码调用)。完整代码是在Pastebin上

I've already tried quite tricky scheme which seems not always correct. Namely, I read each ZipEntry into a byte[] and then feed the result to my custom class loader which extends standard CalssLoader and just sends this byte[] to ClassLoader.defineClass (which is protect and couldn't be called from application code directly). Full code is on the Pastebin.

推荐答案

jar文件是具有特定模式的zip文件。
您可以使用ZipFile和ZipEntry或他们的子类JarFile和JarEntry。

A jar file is a zip file with a specific pattern. You may use a ZipFile and a ZipEntry or their children classes JarFile and JarEntry.

此代码(自定义类加载器的方法)将返回一个Map您需要的每种类型类的数组。

This code (a method of a custom classloader) will return a Map with arrays of each type of "class" you need.

public Map<String, List<Class<?>>> loadAndScanJar(File jarFile)
        throws ClassNotFoundException, ZipException, IOException {

    // Load the jar file into the JVM
    // You can remove this if the jar file already loaded.
    super.addURL(jarFile.toURI().toURL());

    Map<String, List<Class<?>>> classes = new HashMap<String, List<Class<?>>>();

    List<Class<?>> interfaces = new ArrayList<Class<?>>();
    List<Class<?>> clazzes = new ArrayList<Class<?>>();
    List<Class<?>> enums = new ArrayList<Class<?>>();
    List<Class<?>> annotations = new ArrayList<Class<?>>();

    classes.put("interfaces", interfaces);
    classes.put("classes", clazzes);
    classes.put("annotations", annotations);
    classes.put("enums", enums);

    // Count the classes loaded
    int count = 0;

    // Your jar file
    JarFile jar = new JarFile(jarFile);
    // Getting the files into the jar
    Enumeration<? extends JarEntry> enumeration = jar.entries();

    // Iterates into the files in the jar file
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = enumeration.nextElement();

        // Is this a class?
        if (zipEntry.getName().endsWith(".class")) {

            // Relative path of file into the jar.
            String className = zipEntry.getName();

            // Complete class name
            className = className.replace(".class", "").replace("/", ".");
            // Load class definition from JVM
            Class<?> clazz = this.loadClass(className);

            try {
                // Verify the type of the "class"
                if (clazz.isInterface()) {
                    interfaces.add(clazz);
                } else if (clazz.isAnnotation()) {
                    annotations.add(clazz);
                } else if (clazz.isEnum()) {
                    enums.add(clazz);
                } else {
                    clazzes.add(clazz);
                }

                count++;
            } catch (ClassCastException e) {

            }
        }
    }

    System.out.println("Total: " + count);

    return classes;
}

这篇关于以编程方式分析jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆