private final static TestClassLoader LOADER = new TestClassLoader();
public void test() throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor cv = new StaticInitMerger("$clinit$", cw);
cv.visit(V1_1, ACC_PUBLIC, "A", null, "java/lang/Object", null);
cv.visitField(ACC_PUBLIC + ACC_STATIC, "counter", "I", null, null);
for (int i = 0; i < 5; ++i) {
MethodVisitor mv = cv.visitMethod(ACC_PUBLIC,
"<clinit>",
"()V",
null,
null);
mv.visitFieldInsn(GETSTATIC, "A", "counter", "I");
mv.visitInsn(ICONST_1);
mv.visitInsn(IADD);
mv.visitFieldInsn(PUTSTATIC, "A", "counter", "I");
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
}
MethodVisitor mv = cv.visitMethod(ACC_PUBLIC,
"<init>",
"()V",
null,
null);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
cv.visitEnd();
Class<?> c = LOADER.defineClass("A", cw.toByteArray());
assertEquals(c.getField("counter").getInt(c.newInstance()), 5);
}