I am looking for a BCEL code example on how to create an array of size 1 containing instances of java.lang.Class and initialize its only element with a reference to java.lang.String.class
in other terms, I am looking for a BCEL code sample to put "new Class[]{String.class}" on the stack.
Thanks.
Assuming that you want to create a class file version 49 (Java 5) or higher, the required instruction sequence is:
iconst_1
anewarray
java/lang/Class
dup
iconst_0
ldc
java.lang.String.class
aastore
Now the only question left is how to generate that in BCEL. According to what I saw from its website, the generator code may look like this:
il.append(InstructionConstants.ICONST_1);
il.append(factory.createNewArray(Type.getType(Class.class), 1));
il.append(InstructionConstants.DUP);
il.append(InstructionConstants.ICONST_0);
il.append(new LDC(constantPoolGen.addClass(Type.getType(String.class))));
il.append(InstructionConstants.AASTORE);
Though I haven’t tested it.