使用javassist从jar解析类

时间:2014-10-15 11:11:52

标签: java javassist

我是javassist中的菜鸟。任何人都可以给样本如何从jar加载类并使用javassist保存它们?

jar = new JarFile(fileName);
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) entries.nextElement();
if(jarEntry == null)
    break;
if(jarEntry.getName().endsWith(".class")) {
    // ??????
} else {
    resources.add(new RResource(jarEntry.getName(), jar.getInputStream(jarEntry)));
}

1 个答案:

答案 0 :(得分:2)

您可以通过以下代码从JAR文件中的相应类加载字节:

JarFile jarFile = new JarFile(file);
// lets get a reference to the .class-file contained in the JAR
ZipEntry zipEntry = jarFile.getEntry(className.replace(".", "/")+".class");
if (zipEntry == null) {
    jarFile.close();
    return null;
}
// with our valid reference, we are now able to get the bytes out of the jar-archive
InputStream fis = jarFile.getInputStream(zipEntry);
byte[] classBytes = new byte[fis.available()];
fis.read(classBytes);

要在javassist中加载字节,您可以执行以下操作:

ClassPool cp = ClassPool.getDefault();
cp.insertClassPath(new ClassClassPath(this.getClass()));
ClassPath cp1 = null;
ClassPath cp2 = null;

// add the JAR file to the classpath
try {
    cp1 = cp.insertClassPath(jarFile.getAbsolutePath());
} catch (NotFoundException e1) {
    e1.printStackTrace();
    return null;
}
// add the class file we are going to modify to the classpath
cp2 = cp.appendClassPath(new ByteArrayClassPath(className, classBytes));

byte[] modifiedBytes;
try {
    CtClass cc = cp.get(className);
    // skip instrumentation if the class is frozen and therefore
    // can't be modified
    if (!cc.isFrozen()) {
        // do your javassist stuff here
    }
    modifiedBytes = cc.toBytecode();
} catch (NotFoundException | IOException | CannotCompileException | ClassNotFoundException e) {
    handleException(e);
} finally {
    // free the locked resource files
    cp.removeClassPath(cp1);
    cp.removeClassPath(cp2);
}

// write your modified bytes somewhere
if (modifiedBytes.length > 0) {
    try(FileOutputStream fos = new FileOutputStream("pathname")) {
        fos.write(modifiedBytes);
    }
}

也许可以减少一些代码,但这是我从JAR文件加载字节并将它们加载到Javassist中的方法。由于最终的依赖性,JAR文件被加载到Javassist类路径。由于某种原因,我需要将使用Javassist的类添加到类路径中。

您可以在插件用例中查看我如何使用它们:

HTH