org.apache.bcel.generic.InstructionList Java Examples

The following examples show how to use org.apache.bcel.generic.InstructionList. 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: VisitorSet.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @see org.apache.bcel.classfile.Visitor#visitCode
 */
public void visitCode(Code aCode)
{   
    for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
        IDeepVisitor visitor = (IDeepVisitor) iter.next();
        Visitor v = visitor.getClassFileVisitor();
        aCode.accept(v);
    }
    
    // perform a deep visit
    final byte[] code = aCode.getCode();
    final InstructionList list = new InstructionList(code);
    final Iterator it = list.iterator();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        InstructionHandle instruction = (InstructionHandle) iter.next();
        visitInstructionHandle(instruction);
    }
}
 
Example #2
Source File: ASTIfExpr.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #3
Source File: TestArrayAccess04Creator.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: TestArrayAccess03Creator.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
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 #5
Source File: TestReturn01Creator.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
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 #6
Source File: PLSETestCase.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * BCEL-262:
 */
public void testB262() throws ClassNotFoundException
{
    final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestEnum");
    final ClassGen gen = new ClassGen(clazz);
    final ConstantPoolGen pool = gen.getConstantPool();
    // get the values() method
    final Method m = gen.getMethodAt(0);
    final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
    final InstructionList il = mg.getInstructionList();
    // get the invokevirtual instruction
    final InstructionHandle ih = il.findHandle(3);
    final InvokeInstruction ii = (InvokeInstruction)(ih.getInstruction());
    // without fix, the getClassName() will throw:
    //   java.lang.IllegalArgumentException: Cannot be used on an array type
    final String cn = ii.getClassName(pool);
    assertEquals("[Lorg.apache.bcel.data.PLSETestEnum;", cn);
}
 
Example #7
Source File: VisitorSet.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @see org.apache.bcel.classfile.Visitor#visitCode
 */
public void visitCode(Code aCode)
{   
    for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
        IDeepVisitor visitor = (IDeepVisitor) iter.next();
        Visitor v = visitor.getClassFileVisitor();
        aCode.accept(v);
    }
    
    // perform a deep visit
    final byte[] code = aCode.getCode();
    final InstructionList list = new InstructionList(code);
    final Iterator it = list.iterator();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        InstructionHandle instruction = (InstructionHandle) iter.next();
        visitInstructionHandle(instruction);
    }
}
 
Example #8
Source File: BCELBenchmark.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void generator(Blackhole bh) throws IOException {
    JarFile jar = getJarFile();

    for (JarEntry entry : getClasses(jar)) {
        byte[] bytes = IOUtils.toByteArray(jar.getInputStream(entry));

        JavaClass clazz = new ClassParser(new ByteArrayInputStream(bytes), entry.getName()).parse();

        ClassGen cg = new ClassGen(clazz);

        for (Method m : cg.getMethods()) {
            MethodGen mg = new MethodGen(m, cg.getClassName(), cg.getConstantPool());
            InstructionList il = mg.getInstructionList();

            if (il != null) {
                mg.getInstructionList().setPositions();
                mg.setMaxLocals();
                mg.setMaxStack();
            }
            cg.replaceMethod(m, mg.getMethod());
        }

        bh.consume(cg.getJavaClass().getBytes());
    }

    jar.close();
}
 
Example #9
Source File: LocalVariableTypeTableTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: TestReturn03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_1() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.INT, Type.NO_ARGS,
          new String[] {  }, "test3", TEST_PACKAGE+".TestReturn03", il, _cp);

  final InstructionHandle ih_0 = il.append(InstructionConst.ACONST_NULL);
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(InstructionFactory.createReturn(Type.OBJECT));
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example #11
Source File: LocalVariableTypeTableTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public Method injection(final JavaClass clazz, Method method, final ConstantPoolGen cp, final int firstStringOffset) {
    final MethodGen methodGen = new MethodGen(method, clazz.getClassName(), cp);

    final InstructionList instructionList = methodGen.getInstructionList();
    instructionList.insert(instructionList.getStart(), makeWillBeAddedInstructionList(methodGen, firstStringOffset));

    methodGen.setMaxStack();
    methodGen.setMaxLocals();

    method = methodGen.getMethod();
    instructionList.dispose();

    return method;
}
 
Example #12
Source File: HandleTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #13
Source File: HandleTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that branch handles can be added an instruction list, without
 * corrupting the list.
 */
static void branchHandles() {
    for (int i = 0; i < MAXI; i++) {
        final InstructionList list = new InstructionList();
        final InstructionHandle start = list.append(new NOP());
        try {
            for (int j = 0; j < MAXJ; j++) {
                list.append(new GOTO(start));
            }
            final InstructionHandle[] instructionHandles = list.getInstructionHandles();
            for (int j = 0; j < instructionHandles.length; j++) {
                final InstructionHandle handle = instructionHandles[j];
                if (j > 0) {
                    checkLinkage(handle, j);
                    if (start != ((GOTO) handle.getInstruction()).getTarget()) {
                        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 #14
Source File: TestArrayAccess04Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
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 #15
Source File: InstructionFinderTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public void testSearchAll() throws Exception
{
    final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".util.InstructionFinder");
    final Method[] methods = clazz.getMethods();
    Method searchM = null;
    for (final Method m : methods)
    {
        if (m.getName().equals("search") && (m.getArgumentTypes().length == 3))
        {
            searchM = m;
            break;
        }
    }

    if (searchM == null) {
        throw new Exception("search method not found");
    }

    final byte[] bytes = searchM.getCode().getCode();
    final InstructionList il = new InstructionList(bytes);
    final InstructionFinder finder = new InstructionFinder(il);
    final Iterator<?> it = finder.search(".*", il.getStart(), null);

    final InstructionHandle[] ihs = (InstructionHandle[])it.next();
    int size = 0;
    for (final InstructionHandle ih : ihs)
    {
        size += ih.getInstruction().getLength();
    }
    assertEquals(bytes.length, size);

}
 
Example #16
Source File: LDAPSSLSocketFactoryGenerator.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
/**
 * Create a static method 'getDefault' returning {@link SocketFactory}
 * that creates a new instance of the sub-class and calls its no-argument
 * constructor, the newly created is returned to the caller.
 *
 * @param classGen
 * @param constantPoolGen
 * @param instructionFactory
 */
private static void createGetDefaultStaticMethod(ClassGen classGen,
        ConstantPoolGen constantPoolGen, InstructionFactory instructionFactory)
{
    InstructionList il = new InstructionList();

    String methodName = "getDefault";
    MethodGen mg = new MethodGen(ACC_STATIC | ACC_PUBLIC, // access flags
                        Type.getType(SSLSocketFactory.class),  // return type
                        new Type[0],   // argument types - no args
                        new String[0], // arg names - no args
                        methodName,
                        classGen.getClassName(),    // method, class
                        il,
                        constantPoolGen);

    il.append(instructionFactory.createNew(classGen.getClassName()));
    il.append(InstructionConst.DUP);

    il.append(instructionFactory.createInvoke(classGen.getClassName(), "<init>", Type.VOID,
                                   new Type[] {},
                                   INVOKESPECIAL));

    il.append(InstructionConst.ARETURN);

    mg.setMaxStack();
    classGen.addMethod(mg.getMethod());
    il.dispose();
}
 
Example #17
Source File: ASTIdent.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #18
Source File: ASTFunDecl.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces instruction sequences (typically generated by ASTIfExpr) of the form
 *
 * IF_ICMP__, ICONST_1, GOTO, ICONST_0, IFEQ, Instruction
 *
 * where the IF_ICMP__ branches to the ICONST_0 (else part) and the GOTO branches to the IFEQ with the simpler
 * expression
 *
 * IF_ICMP__, Instruction
 *
 * where the IF_ICMP__ now branches to the target of the previous IFEQ instruction.
 */
private static void optimizeIFs(final InstructionList il) {
    final InstructionFinder f = new InstructionFinder(il);
    final String pat = "IF_ICMP ICONST_1 GOTO ICONST_0 IFEQ Instruction";

    for (final Iterator<InstructionHandle[]> it = f.search(pat, my_constraint); it.hasNext();) {
        final InstructionHandle[] match = it.next();
        // Everything ok, update code
        final BranchInstruction ifeq = (BranchInstruction) (match[4].getInstruction());
        final BranchHandle if_icmp = (BranchHandle) match[0];

        if_icmp.setTarget(ifeq.getTarget());

        try {
            il.delete(match[1], match[4]);
        } catch (final TargetLostException e) {
            final InstructionHandle[] targets = e.getTargets();

            System.err.println(targets[0]);

            for (InstructionHandle target : targets) {
                final InstructionTargeter[] targeters = target.getTargeters();

                for (InstructionTargeter targeter : targeters) {
                    if ((target != match[4]) || (targeter != match[2])) {
                        System.err.println("Unexpected: " + e);
                    }
                }
            }
        }
    }
}
 
Example #19
Source File: TestReturn01Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
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 #20
Source File: ASTLetExpr.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #21
Source File: TestReturn03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
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+".TestReturn03", 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 #22
Source File: TestArrayAccess02Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
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 #23
Source File: TestArrayAccess02Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
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 #24
Source File: TestArrayAccess03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
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+".TestArrayAccess03", 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 #25
Source File: ComplexObjectCreator.java    From cougar with Apache License 2.0 5 votes vote down vote up
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 #26
Source File: ComplexObjectCreator.java    From cougar with Apache License 2.0 5 votes vote down vote up
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 #27
Source File: ComplexObjectCreator.java    From cougar with Apache License 2.0 5 votes vote down vote up
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 #28
Source File: BCELPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] nullAdaptClass(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();
    Method[] ms = cg.getMethods();
    for (int j = 0; j < ms.length; ++j) {
        MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp);
        boolean lv = ms[j].getLocalVariableTable() == null;
        boolean ln = ms[j].getLineNumberTable() == null;
        if (lv) {
            mg.removeLocalVariables();
        }
        if (ln) {
            mg.removeLineNumbers();
        }
        mg.stripAttributes(skipDebug);
        InstructionList il = mg.getInstructionList();
        if (il != null) {
            InstructionHandle ih = il.getStart();
            while (ih != null) {
                ih = ih.getNext();
            }
            if (compute) {
                mg.setMaxStack();
                mg.setMaxLocals();
            }
        }
        cg.replaceMethod(ms[j], mg.getMethod());
    }
    return cg.getJavaClass().getBytes();
}
 
Example #29
Source File: InstructionFinderTest.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public void testSearch() {
    final InstructionList il = new InstructionList();
    il.append(new ILOAD(1));
    il.append(new ILOAD(2));
    il.append(new IADD());
    il.append(new ISTORE(3));
    final InstructionFinder finder = new InstructionFinder(il);

    final Iterator<?> it = finder.search("ILOAD IADD", il.getInstructionHandles()[0], null );
    final InstructionHandle[] ihs = (InstructionHandle[])it.next();
    assertEquals(2, ihs.length);
    assertEquals(ihs[0].getInstruction(), new ILOAD(2));
    assertEquals(ihs[1].getInstruction(), new IADD());
}
 
Example #30
Source File: BCELPerfTest.java    From annotation-tools with MIT License 5 votes vote down vote up
byte[] nullAdaptClass(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();
    Method[] ms = cg.getMethods();
    for (int j = 0; j < ms.length; ++j) {
        MethodGen mg = new MethodGen(ms[j], cg.getClassName(), cp);
        boolean lv = ms[j].getLocalVariableTable() == null;
        boolean ln = ms[j].getLineNumberTable() == null;
        if (lv) {
            mg.removeLocalVariables();
        }
        if (ln) {
            mg.removeLineNumbers();
        }
        mg.stripAttributes(skipDebug);
        InstructionList il = mg.getInstructionList();
        if (il != null) {
            InstructionHandle ih = il.getStart();
            while (ih != null) {
                ih = ih.getNext();
            }
            if (compute) {
                mg.setMaxStack();
                mg.setMaxLocals();
            }
        }
        cg.replaceMethod(ms[j], mg.getMethod());
    }
    return cg.getJavaClass().getBytes();
}