- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想使用ASM在.class文件中添加static final字段,源文件是
public class Example {
public Example(int code) {
this.code = code;
}
public int getCode() {
return code;
}
private final int code;
}
反编译生成的类应该是这样的:
public class Example {
public static final Example FIRST = new Example(1);
public static final Example SECOND = new Example(2);
public Example(int code) {
this.code = code;
}
public int getCode() {
return code;
}
private final int code;
}
作为结论,我想使用 ASM 将 FIRST 和 SECOND 常量添加到 .class 文件,我该怎么做?
最佳答案
这个答案显示了如何使用 ASM 的访问者 api(参见 ASM homepage 上的 ASM 4.0 A Java 字节码工程库 的第 2.2 节),因为它是最熟悉的 api我。 ASM 也有一个对象模型 api(请参阅同一文档中的第二部分)变体,在这种情况下通常更容易使用。对象模型可能有点慢,因为它在内存中构建了整个类文件的树,但如果只有少量类需要转换,性能损失应该可以忽略不计。
创建时static final
值不是常量(如数字)的字段,它们的初始化实际上转到“static initializer block”。因此,您的第二个(转换后的)代码 list 等效于以下 java 代码:
public class Example {
public static final Example FIRST;
public static final Example SECOND;
static {
FIRST = new Example(1);
SECOND = new Example(2);
}
...
}
在一个 java 文件中你可以有多个这样的 static { ... } block ,而在类文件中只能有一个。 java编译器自动将多个静态 block 合并为一个来满足这个需求。当操作字节码时,这意味着如果之前没有静态 block ,那么我们创建一个新的,而如果已经存在一个静态 block ,我们需要将我们的代码添加到现有代码的开头(添加比添加更容易)。
对于ASM,静态 block 看起来像一个具有特殊名称<clinit>
的静态方法。 ,就像构造函数看起来像具有特殊名称的方法一样 <init>
.
在使用visitor api时,要知道一个方法是否已经从之前定义过的方法是监听所有的visitMethod()调用,并检查每个调用中的方法名。访问完所有方法后,将调用 visitEnd() 方法,因此如果到那时还没有访问过任何方法,我们就知道需要创建一个新方法。
假设我们有一个 byte[] 格式的原始类,请求的转换可以这样完成:
import org.objectweb.asm.*;
import static org.objectweb.asm.Opcodes.*;
public static byte[] transform(byte[] origClassData) throws Exception {
ClassReader cr = new ClassReader(origClassData);
final ClassWriter cw = new ClassWriter(cr, Opcodes.ASM4);
// add the static final fields
cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "FIRST", "LExample;", null, null).visitEnd();
cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "SECOND", "LExample;", null, null).visitEnd();
// wrap the ClassWriter with a ClassVisitor that adds the static block to
// initialize the above fields
ClassVisitor cv = new ClassVisitor(ASM4, cw) {
boolean visitedStaticBlock = false;
class StaticBlockMethodVisitor extends MethodVisitor {
StaticBlockMethodVisitor(MethodVisitor mv) {
super(ASM4, mv);
}
public void visitCode() {
super.visitCode();
// here we do what the static block in the java code
// above does i.e. initialize the FIRST and SECOND
// fields
// create first instance
super.visitTypeInsn(NEW, "Example");
super.visitInsn(DUP);
super.visitInsn(ICONST_1); // pass argument 1 to constructor
super.visitMethodInsn(INVOKESPECIAL, "Example", "<init>", "(I)V");
// store it in the field
super.visitFieldInsn(PUTSTATIC, "Example", "FIRST", "LExample;");
// create second instance
super.visitTypeInsn(NEW, "Example");
super.visitInsn(DUP);
super.visitInsn(ICONST_2); // pass argument 2 to constructor
super.visitMethodInsn(INVOKESPECIAL, "Example", "<init>", "(I)V");
super.visitFieldInsn(PUTSTATIC, "Example", "SECOND", "LExample;");
// NOTE: remember not to put a RETURN instruction
// here, since execution should continue
}
public void visitMaxs(int maxStack, int maxLocals) {
// The values 3 and 0 come from the fact that our instance
// creation uses 3 stack slots to construct the instances
// above and 0 local variables.
final int ourMaxStack = 3;
final int ourMaxLocals = 0;
// now, instead of just passing original or our own
// visitMaxs numbers to super, we instead calculate
// the maximum values for both.
super.visitMaxs(Math.max(ourMaxStack, maxStack), Math.max(ourMaxLocals, maxLocals));
}
}
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (cv == null) {
return null;
}
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
if ("<clinit>".equals(name) && !visitedStaticBlock) {
visitedStaticBlock = true;
return new StaticBlockMethodVisitor(mv);
} else {
return mv;
}
}
public void visitEnd() {
// All methods visited. If static block was not
// encountered, add a new one.
if (!visitedStaticBlock) {
// Create an empty static block and let our method
// visitor modify it the same way it modifies an
// existing static block
MethodVisitor mv = super.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
mv = new StaticBlockMethodVisitor(mv);
mv.visitCode();
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
super.visitEnd();
}
};
// feed the original class to the wrapped ClassVisitor
cr.accept(cv, 0);
// produce the modified class
byte[] newClassData = cw.toByteArray();
return newClassData;
}
由于您的问题没有进一步说明您的最终目标到底是什么,我决定使用一个硬编码的基本示例来处理您的 Example 类案例。如果您想创建正在转换的类的实例,则必须更改上面包含“Example”的所有字符串,以使用实际正在转换的类的完整类名。或者,如果您特别想要在每个转换后的类中使用 Example 类的两个实例,则上面的示例按原样工作。
关于java - 如何使用 ASM 为初始值设定项添加静态最终字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065262/
我想成为 Spark 纱客户(link)。是否需要安装hadoop?还是只安装 yarn 可以吗? (by this link) 最佳答案 No Spark不需要Hadoop即可运行。 Apache
我是一名优秀的程序员,十分优秀!