简体   繁体   中英

Determine the classes referenced in a .class file

(以编程方式)解析已编译的Java(.class)文件并生成它引用的任何和所有其他Java类的列表的最佳方法是什么?

It's easy do it by yourself if you know the spec .

Heres a quick and dirty demo program how to do it:

package test;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {

    private static Map<Integer, String> strings = new HashMap<Integer, String>();
    private static Set<Integer> classes = new HashSet<Integer>();
    private static int indexCorrection = 0; // for correcting indexes to constant pools with long and double entries

    public static void main(String[] args) throws Exception {
        printReferencedClassesFromClass(String.class);
    }

    private static void printReferencedClassesFromClass(Class<?> c) throws IOException {
        saveReferencedClassesFromStream(c.getResourceAsStream(c.getSimpleName() + ".class"));
        printReferencedClasses();
    }

    private static void printReferencedClasses() {
        for (Integer index : classes)
            System.out.println(strings.get(index));
    }

    private static void saveReferencedClassesFromStream(InputStream stream) throws IOException {
        DataInputStream dataStream = new DataInputStream(stream);
        skipHeader(dataStream);
        saveReferencedClassesFromConstantPool(dataStream);
    }

    private static void skipHeader(DataInputStream dataInputStream) throws IOException {
        readU4(dataInputStream); // magic byte
        readU2(dataInputStream); // minor version
        readU2(dataInputStream); // major version
    }

    private static void saveReferencedClassesFromConstantPool(DataInputStream stream) throws IOException {
        int poolSize = readU2(stream);
        for (int n = 1; n < poolSize - indexCorrection; n++)
            savePoolEntryIfIsClassReference(n, stream);
    }

    private static void savePoolEntryIfIsClassReference(int index, DataInputStream stream) throws IOException {
        int tag = readU1(stream);
        switch (tag) {
        case 1: // Utf8
            saveStringFromUtf8Entry(index, stream);
            break;
        case 7: // Class
            saveClassEntry(stream);
            break;
        case 8: // String
        case 16: // MethodType
            readU2(stream);
            break;
        case 3: // Integer
        case 4: // Float
            readU4(stream);
            break;
        case 5: // Long
        case 6: // Double
            readU4(stream);
            readU4(stream);
            indexCorrection++;
            break;
        case 9: // Fieldref
        case 10: // Methodref
        case 11: // InterfaceMethodref
        case 12: // NameAndType
        case 18: // InvokeDynmaic
            readU2(stream);
            readU2(stream);
            break;
        case 15: // MethodHandle
            readU1(stream);
            readU2(stream);
            break;
        }
    }

    private static void saveClassEntry(DataInputStream stream) throws IOException {
        classes.add(readU2(stream));
    }

    private static void saveStringFromUtf8Entry(int index, DataInputStream stream) throws IOException {
        strings.put(index + indexCorrection, readString(stream));
    }

    private static String readString(DataInputStream stream) throws IOException {
        return stream.readUTF();
    }

    private static int readU1(DataInputStream stream) throws IOException {
        return stream.readUnsignedByte();
    }

    private static int readU2(DataInputStream stream) throws IOException {
        return stream.readUnsignedShort();
    }

    private static int readU4(DataInputStream stream) throws IOException {
        return stream.readInt();
    }

}

Output is:

test/Main
java/lang/Object
java/io/BufferedInputStream
java/util/Iterator
java/io/FileInputStream
java/lang/System
java/lang/Integer
java/lang/Exception
java/lang/String
java/io/File
java/util/Map
java/util/HashMap
java/io/IOException
java/util/Set
java/io/DataInputStream
java/io/PrintStream
java/util/HashSet
java/lang/Throwable

Have you tried JDepend? It analyzes class files to determine references.

这可以与Apache的BCEL一起使用

我想你可以使用我的Java Dependency Resolver项目,它不仅会找到引用的java文件,还会找到项目中特定类的jar文件

I don't know what the best way is. I do know there are 2 APIs that might help.

You can try jclasslib, for inspecting .class files
http://www.ej-technologies.com/products/jclasslib/resources.html
or BCEL (byte code engineering library) for analyzing and manipulating .class files.
http://jakarta.apache.org/bcel/

My understanding is that any class to be used within a .class (as far as a field, parameter, return type, etc) is defined in the constant_pool. So you may want to read up on that.
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM