【发布时间】:2018-10-02 12:17:47
【问题描述】:
我正在尝试删除所有 System#exit 调用,它似乎正在工作,但我最终得到了方法重复。
我的代码如下所示:
JarInputStream jis = new JarInputStream(new FileInputStream(new File(input)));
JarOutputStream jos = new JarOutputStream(new FileOutputStream(new File(output)));
byte[] buffer = new byte[4096];
JarEntry entry;
while ((entry = jis.getNextJarEntry()) != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int size;
while ((size = jis.read(buffer)) != -1) {
bos.write(buffer, 0, size);
}
bos.close();
jos.putNextEntry(new JarEntry(entry.getName()));
if (entry.getName().endsWith(".class")) {
ClassReader cr = new ClassReader(bos.toByteArray());
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
ClassTransformer cm = new ClassTransformer(cw);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
System.out.println("Analyzing bytecode in class " + cn.name);
List<MethodNode> methods = cn.methods;
for (MethodNode method : methods) {
System.out.println("Analyzing bytecode in method " + method.name);
String[] exceptions = (String[]) method.exceptions.toArray(new String[method.exceptions.size()]);
cm.visitMethod(method.access, method.name, method.desc, method.signature, exceptions);
}
cr.accept(cm, ClassReader.EXPAND_FRAMES);
jos.write(cw.toByteArray());
} else {
jos.write(bos.toByteArray());
}
}
jos.close();
jis.close();
ClassTransformer 和 MethodTransformer 都是自定义类。如果你想让我也提供它们,请告诉我。
【问题讨论】: