Static initializers in bcel

2019-08-11 08:24发布

问题:

In BCEL, I would like to initialize static fields in the static initializer of a class. I haven't found a way to do so however... any hints?

I need something like:

// Field descriptor #8 [I
private static int[] a;

static {};
     0  bipush 10
     2  multianewarray int[] [9]
     6  putstatic Output.a : int[] [11]
     9  return

I however only seem to be able to generate (with MethodGen) things like:

public static void {}();
   0  bipush 10
   2  multianewarray int[] [9]
   6  putstatic Output.a : int[] [11]
   9  return

Which is of course not the same.

回答1:

Just found it myself.

    MethodGen method = new MethodGen(Constants.ACC_STATIC,
            Type.VOID,
            new Type[] { }, new String[] { }, "<clinit>",
            cg.getClassName(), il, cg.getConstantPool());

clinit seems to be the class initializer ...



标签: bcel