Static initializers in bcel

若如初见. 提交于 2019-12-24 05:56: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 ...



来源:https://stackoverflow.com/questions/264126/static-initializers-in-bcel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!