Java Code Examples for org.apache.bcel.generic.InstructionList#append()
The following examples show how to use
org.apache.bcel.generic.InstructionList#append() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestReturn01Creator.java From commons-bcel with Apache License 2.0 | 6 votes |
private void createMethod_1() { final InstructionList il = new InstructionList(); final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] { }, "foo", TEST_PACKAGE+".TestReturn01", il, _cp); final InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.Object")); Assert.assertNotNull(ih_0); // TODO why is this not used il.append(InstructionConst.DUP); il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); il.append(InstructionConst.NOP); final InstructionHandle ih_8 = il.append(InstructionFactory.createReturn(Type.OBJECT)); Assert.assertNotNull(ih_8); // TODO why is this not used method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 2
Source File: TestArrayAccess04Creator.java From commons-bcel with Apache License 2.0 | 6 votes |
private void createMethod_1() { final InstructionList il = new InstructionList(); final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT }, new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess04", il, _cp); final InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); Assert.assertNotNull(ih_0); // TODO why is this not used il.append(_factory.createNewArray(Type.OBJECT, (short) 1)); il.append(InstructionFactory.createStore(Type.OBJECT, 1)); final InstructionHandle ih_5 = il.append(new PUSH(_cp, 1)); Assert.assertNotNull(ih_5); // TODO why is this not used il.append(InstructionFactory.createStore(Type.INT, 2)); final InstructionHandle ih_7 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); Assert.assertNotNull(ih_7); // TODO why is this not used il.append(new PUSH(_cp, 0)); il.append(InstructionFactory.createLoad(Type.INT, 2)); il.append(InstructionConst.AASTORE); final InstructionHandle ih_11 = il.append(InstructionFactory.createReturn(Type.VOID)); Assert.assertNotNull(ih_11); // TODO why is this not used method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 3
Source File: TestArrayAccess03Creator.java From commons-bcel with Apache License 2.0 | 6 votes |
private void createMethod_1() { final InstructionList il = new InstructionList(); final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT }, new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess03", il, _cp); final InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); Assert.assertNotNull(ih_0); // TODO why is this not used il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess03"), (short) 1)); il.append(InstructionFactory.createStore(Type.OBJECT, 1)); final InstructionHandle ih_5 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); Assert.assertNotNull(ih_5); // TODO why is this not used il.append(new PUSH(_cp, 0)); il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess03")); il.append(InstructionConst.DUP); il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess03", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); il.append(InstructionConst.AASTORE); final InstructionHandle ih_15 = il.append(InstructionFactory.createReturn(Type.VOID)); Assert.assertNotNull(ih_15); // TODO why is this not used method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 4
Source File: ASTIfExpr.java From commons-bcel with Apache License 2.0 | 6 votes |
/** * Fifth pass, produce Java byte code. */ @Override public void byte_code(final InstructionList il, final MethodGen method, final ConstantPoolGen cp) { if_expr.byte_code(il, method, cp); final InstructionList then_code = new InstructionList(); final InstructionList else_code = new InstructionList(); then_expr.byte_code(then_code, method, cp); else_expr.byte_code(else_code, method, cp); BranchHandle i, g; i = il.append(new IFEQ(null)); // If POP() == FALSE(i.e. 0) then branch to ELSE ASTFunDecl.pop(); il.append(then_code); g = il.append(new GOTO(null)); i.setTarget(il.append(else_code)); g.setTarget(il.append(InstructionConstants.NOP)); // May be optimized away later }
Example 5
Source File: TestArrayAccess04Creator.java From commons-bcel with Apache License 2.0 | 5 votes |
private void createMethod_0() { final InstructionList il = new InstructionList(); final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", TEST_PACKAGE+".TestArrayAccess04", il, _cp); final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); Assert.assertNotNull(ih_0); // TODO why is this not used il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); Assert.assertNotNull(ih_4); // TODO why is this not used method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 6
Source File: ComplexObjectCreator.java From cougar with Apache License 2.0 | 5 votes |
private void createMethod_0() { InstructionList il = new InstructionList(); MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", objectType, il, _cp); InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL)); InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID)); method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 7
Source File: ComplexObjectCreator.java From cougar with Apache License 2.0 | 5 votes |
private void createMethod_0() { InstructionList il = new InstructionList(); MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", objectType, il, _cp); InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL)); InstructionHandle ih_4 = il.append(_factory.createReturn(Type.VOID)); method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 8
Source File: TestReturn01Creator.java From commons-bcel with Apache License 2.0 | 5 votes |
private void createMethod_0() { final InstructionList il = new InstructionList(); final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", TEST_PACKAGE+".TestReturn01", il, _cp); final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); Assert.assertNotNull(ih_0); // TODO why is this not used il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); Assert.assertNotNull(ih_4); // TODO why is this not used method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 9
Source File: TestArrayAccess02Creator.java From commons-bcel with Apache License 2.0 | 5 votes |
private void createMethod_0() { final InstructionList il = new InstructionList(); final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "<init>", TEST_PACKAGE+".TestArrayAccess02", il, _cp); final InstructionHandle ih_0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); Assert.assertNotNull(ih_0); // TODO why is this not used il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); final InstructionHandle ih_4 = il.append(InstructionFactory.createReturn(Type.VOID)); Assert.assertNotNull(ih_4); // TODO why is this not used method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 10
Source File: LocalVariableTypeTableTestCase.java From commons-bcel with Apache License 2.0 | 5 votes |
public InstructionList createPrintln(final ConstantPoolGen cp, final Instruction instruction) { final InstructionList il = new InstructionList(); final int out = cp.addFieldref("java.lang.System", "out", "Ljava/io/PrintStream;"); final int println = cp.addMethodref("java.io.PrintStream", "println", "(Ljava/lang/String;)V"); il.append(new GETSTATIC(out)); il.append(instruction); il.append(new INVOKEVIRTUAL(println)); return il; }
Example 11
Source File: HandleTestCase.java From commons-bcel with Apache License 2.0 | 5 votes |
/** * Asserts that instruction handles can be added an instruction list, without * corrupting the list. */ static void handles() { for (int i = 0; i < MAXI; i++) { final InstructionList list = new InstructionList(); try { for (int j = 0; j < MAXJ; j++) { list.append(new ILOAD(j)); } final InstructionHandle[] instructionHandles = list.getInstructionHandles(); for (int j = 0; j < instructionHandles.length; j++) { final InstructionHandle handle = instructionHandles[j]; checkLinkage(handle, j); if (j != ((ILOAD) handle.getInstruction()).getIndex()) { final AssertionFailedError error = new AssertionFailedError("unexpected instruction at index " + j); exception = error; throw error; } } if (exception != null) { return; } } catch (final NullPointerException e) { System.out.println("NPE at i=" + i); exception = e; throw e; } list.dispose(); // this initializes caching of unused instruction handles } }
Example 12
Source File: ASTLetExpr.java From commons-bcel with Apache License 2.0 | 5 votes |
/** * Fifth pass, produce Java byte code. */ @Override public void byte_code(final InstructionList il, final MethodGen method, final ConstantPoolGen cp) { final int size = idents.length; final LocalVariableGen[] l = new LocalVariableGen[size]; for(int i=0; i < size; i++) { final String ident = idents[i].getName(); final Variable entry = (Variable)env.get(ident); final Type t = BasicType.getType((byte)idents[i].getType()); final LocalVariableGen lg = method.addLocalVariable(ident, t, null, null); final int slot = lg.getIndex(); entry.setLocalVariable(lg); InstructionHandle start = il.getEnd(); exprs[i].byte_code(il, method, cp); start = (start == null)? il.getStart() : start.getNext(); lg.setStart(start); il.append(new ISTORE(slot)); ASTFunDecl.pop(); l[i] = lg; } body.byte_code(il, method, cp); final InstructionHandle end = il.getEnd(); for(int i=0; i < size; i++) { l[i].setEnd(end); } }
Example 13
Source File: ASTIdent.java From commons-bcel with Apache License 2.0 | 5 votes |
/** * Fifth pass, produce Java byte code. */ @Override public void byte_code(final InstructionList il, final MethodGen method, final ConstantPoolGen cp) { if(name.equals("TRUE")) { il.append(new PUSH(cp, 1)); } else if(name.equals("FALSE")) { il.append(new PUSH(cp, 0)); } else { final LocalVariableGen local_var = reference.getLocalVariable(); il.append(new ILOAD(local_var.getIndex())); } ASTFunDecl.push(); }
Example 14
Source File: TestArrayAccess02Creator.java From commons-bcel with Apache License 2.0 | 5 votes |
private void createMethod_1() { final InstructionList il = new InstructionList(); final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] { }, "test", TEST_PACKAGE+".TestArrayAccess02", il, _cp); final InstructionHandle ih_0 = il.append(new PUSH(_cp, 1)); Assert.assertNotNull(ih_0); // TODO why is this not used il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess02"), (short) 1)); il.append(InstructionFactory.createStore(Type.OBJECT, 0)); final InstructionHandle ih_5 = il.append(new PUSH(_cp, 1)); Assert.assertNotNull(ih_5); // TODO why is this not used il.append(_factory.createNewArray(Type.STRING, (short) 1)); il.append(InstructionFactory.createStore(Type.OBJECT, 1)); final InstructionHandle ih_10 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); Assert.assertNotNull(ih_10); // TODO why is this not used il.append(new PUSH(_cp, 0)); il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess02")); il.append(InstructionConst.DUP); il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess02", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL)); il.append(InstructionConst.AASTORE); final InstructionHandle ih_20 = il.append(InstructionFactory.createReturn(Type.VOID)); Assert.assertNotNull(ih_20); // TODO why is this not used method.setMaxStack(); method.setMaxLocals(); _cg.addMethod(method.getMethod()); il.dispose(); }
Example 15
Source File: ASTExpr.java From commons-bcel with Apache License 2.0 | 4 votes |
/** * Fifth pass, produce Java byte code. */ public void byte_code(final InstructionList il, final MethodGen method, final ConstantPoolGen cp) { exprs[0].byte_code(il, method, cp); if(unop != -1) { // Apply unary operand if(unop == MINUS) { il.append(InstructionConstants.INEG); } else { // == NOT il.append(new PUSH(cp, 1)); ASTFunDecl.push(); // Push TRUE il.append(InstructionConstants.IXOR); ASTFunDecl.pop(); } } else { // Apply binary operand BranchHandle bh=null; exprs[1].byte_code(il, method, cp); switch(kind) { case PLUS: il.append(InstructionConstants.IADD); ASTFunDecl.pop(); break; case MINUS: il.append(InstructionConstants.ISUB); ASTFunDecl.pop(); break; case MULT: il.append(InstructionConstants.IMUL); ASTFunDecl.pop(); break; case DIV: il.append(InstructionConstants.IDIV); ASTFunDecl.pop(); break; case AND: il.append(InstructionConstants.IAND); ASTFunDecl.pop(); break; case OR: il.append(InstructionConstants.IOR); ASTFunDecl.pop(); break; /* Use negated operands */ case EQ: bh = il.append(new IF_ICMPNE(null)); ASTFunDecl.pop(2); break; case LEQ: bh = il.append(new IF_ICMPGT(null)); ASTFunDecl.pop(2); break; case GEQ: bh = il.append(new IF_ICMPLT(null)); ASTFunDecl.pop(2); break; case NEQ: bh = il.append(new IF_ICMPEQ(null)); ASTFunDecl.pop(2); break; case LT: bh = il.append(new IF_ICMPGE(null)); ASTFunDecl.pop(2); break; case GT: bh = il.append(new IF_ICMPLE(null)); ASTFunDecl.pop(2); break; default: System.err.println("Unhandled case: " + kind); } switch(kind) { case EQ: case LEQ: case GEQ: case NEQ: case LT: case GT: BranchHandle g; il.append(new PUSH(cp, 1)); g = il.append(new GOTO(null)); bh.setTarget(il.append(new PUSH(cp, 0))); g.setTarget(il.append(InstructionConstants.NOP)); // May be optimized away later ASTFunDecl.push(); break; default: break; } } }
Example 16
Source File: ASTInteger.java From commons-bcel with Apache License 2.0 | 4 votes |
/** * Fifth pass, produce Java byte code. */ @Override public void byte_code(final InstructionList il, final MethodGen method, final ConstantPoolGen cp) { il.append(new PUSH(cp, value)); ASTFunDecl.push(); }
Example 17
Source File: ASTFunAppl.java From commons-bcel with Apache License 2.0 | 4 votes |
/** * Fifth pass, produce Java byte code. */ @Override public void byte_code(final InstructionList il, final MethodGen method, final ConstantPoolGen cp) { final String fname = name.getName(); // Function f = function; //ASTIdent fun = f.getName(); // ASTIdent[] args = f.getArgs(); final String class_name = method.getClassName(); if(fname.equals("READ")) { il.append(new INVOKESTATIC(cp.addMethodref(class_name, "_readInt", "()I"))); } else if(fname.equals("WRITE")) { exprs[0].byte_code(il, method, cp); ASTFunDecl.pop(); il.append(new INVOKESTATIC(cp.addMethodref(class_name, "_writeInt", "(I)I"))); } else { // Normal function final int size = exprs.length; Type[] argv = null; if(exprs != null) { argv = new Type[size]; for(int i=0; i < size; i++) { argv[i] = Type.INT; exprs[i].byte_code(il, method, cp); } //ASTFunDecl.push(size); } ASTFunDecl.pop(size); // Function call il.append(new INVOKESTATIC(cp.addMethodref(class_name, fname, Type.getMethodSignature(Type.INT, argv)))); } ASTFunDecl.push(); }
Example 18
Source File: helloify.java From commons-bcel with Apache License 2.0 | 4 votes |
/** * Patch a method. */ private static Method helloifyMethod(Method m) { final Code code = m.getCode(); final int flags = m.getAccessFlags(); final String name = m.getName(); // Sanity check if (m.isNative() || m.isAbstract() || (code == null)) { return m; } // Create instruction list to be inserted at method start. final String mesg = "Hello from " + Utility.methodSignatureToString(m.getSignature(), name, Utility.accessToString(flags)); final InstructionList patch = new InstructionList(); patch.append(new GETSTATIC(out)); patch.append(new PUSH(cp, mesg)); patch.append(new INVOKEVIRTUAL(println)); final MethodGen mg = new MethodGen(m, class_name, cp); final InstructionList il = mg.getInstructionList(); final InstructionHandle[] ihs = il.getInstructionHandles(); if (name.equals("<init>")) { // First let the super or other constructor be called for (int j = 1; j < ihs.length; j++) { if (ihs[j].getInstruction() instanceof INVOKESPECIAL) { il.append(ihs[j], patch); // Should check: method name == "<init>" break; } } } else { il.insert(ihs[0], patch); } // Stack size must be at least 2, since the println method takes 2 argument. if (code.getMaxStack() < 2) { mg.setMaxStack(2); } m = mg.getMethod(); il.dispose(); // Reuse instruction handles return m; }
Example 19
Source File: BCELPerfTest.java From annotation-tools with MIT License | 4 votes |
byte[] counterAdaptClass(final InputStream is, final String name) throws Exception { JavaClass jc = new ClassParser(is, name + ".class").parse(); ClassGen cg = new ClassGen(jc); ConstantPoolGen cp = cg.getConstantPool(); if (!cg.isInterface()) { FieldGen fg = new FieldGen(ACC_PUBLIC, Type.getType("I"), "_counter", cp); cg.addField(fg.getField()); } Method[] ms = cg.getMethods(); for (int j = 0; j < ms.length; ++j) { MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp); if (!mg.getName().equals("<init>") && !mg.isStatic() && !mg.isAbstract() && !mg.isNative()) { if (mg.getInstructionList() != null) { InstructionList il = new InstructionList(); il.append(new ALOAD(0)); il.append(new ALOAD(0)); il.append(new GETFIELD(cp.addFieldref(name, "_counter", "I"))); il.append(new ICONST(1)); il.append(new IADD()); il.append(new PUTFIELD(cp.addFieldref(name, "_counter", "I"))); mg.getInstructionList().insert(il); mg.setMaxStack(Math.max(mg.getMaxStack(), 2)); boolean lv = ms[j].getLocalVariableTable() == null; boolean ln = ms[j].getLineNumberTable() == null; if (lv) { mg.removeLocalVariables(); } if (ln) { mg.removeLineNumbers(); } cg.replaceMethod(ms[j], mg.getMethod()); } } } return cg.getJavaClass().getBytes(); }
Example 20
Source File: BCELPerfTest.java From annotation-tools with MIT License | 4 votes |
byte[] counterAdaptClass(final InputStream is, final String name) throws Exception { JavaClass jc = new ClassParser(is, name + ".class").parse(); ClassGen cg = new ClassGen(jc); ConstantPoolGen cp = cg.getConstantPool(); if (!cg.isInterface()) { FieldGen fg = new FieldGen(ACC_PUBLIC, Type.getType("I"), "_counter", cp); cg.addField(fg.getField()); } Method[] ms = cg.getMethods(); for (int j = 0; j < ms.length; ++j) { MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp); if (!mg.getName().equals("<init>") && !mg.isStatic() && !mg.isAbstract() && !mg.isNative()) { if (mg.getInstructionList() != null) { InstructionList il = new InstructionList(); il.append(new ALOAD(0)); il.append(new ALOAD(0)); il.append(new GETFIELD(cp.addFieldref(name, "_counter", "I"))); il.append(new ICONST(1)); il.append(new IADD()); il.append(new PUTFIELD(cp.addFieldref(name, "_counter", "I"))); mg.getInstructionList().insert(il); mg.setMaxStack(Math.max(mg.getMaxStack(), 2)); boolean lv = ms[j].getLocalVariableTable() == null; boolean ln = ms[j].getLineNumberTable() == null; if (lv) { mg.removeLocalVariables(); } if (ln) { mg.removeLineNumbers(); } cg.replaceMethod(ms[j], mg.getMethod()); } } } return cg.getJavaClass().getBytes(); }