org.apache.bcel.generic.InstructionFactory Java Examples

The following examples show how to use org.apache.bcel.generic.InstructionFactory. 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: 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 #2
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 #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: 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 #5
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 #6
Source File: TestArrayAccess03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestArrayAccess03Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess03", "java.lang.Object", "TestArrayAccess03.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
Example #7
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 #8
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 #9
Source File: TestArrayAccess02Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestArrayAccess02Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess02", "java.lang.Object", "TestArrayAccess02.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
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: 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 #12
Source File: TestReturn03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestReturn03Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestReturn03", "java.lang.Object", "TestReturn03.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
Example #13
Source File: LDAPSSLSocketFactoryGenerator.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the LDAPSocketFactoryImpl class (subclass of {@link AbstractLDAPSSLSocketFactory}.
 * A static method #getDefaulta, a static field _sslContent and no-arg constructor are added
 * to the class.
 *
 * @param className
 *
 * @return byte code
 */
private static byte[] createSubClassByteCode(final String className)
{
    ClassGen classGen = new ClassGen(className,
            AbstractLDAPSSLSocketFactory.class.getName(),
            "<generated>",
            ACC_PUBLIC | ACC_SUPER,
            null);
    ConstantPoolGen constantPoolGen = classGen.getConstantPool();
    InstructionFactory factory = new InstructionFactory(classGen);

    createSslContextStaticField(classGen, constantPoolGen);
    createGetDefaultStaticMethod(classGen, constantPoolGen, factory);

    classGen.addEmptyConstructor(ACC_PROTECTED);

    JavaClass javaClass = classGen.getJavaClass();
    ByteArrayOutputStream out = null;
    try
    {
        out = new ByteArrayOutputStream();
        javaClass.dump(out);
        return out.toByteArray();
    }
    catch (IOException ioex)
    {
        throw new IllegalStateException("Could not write to a ByteArrayOutputStream - should not happen", ioex);
    }
    finally
    {
        closeSafely(out);
    }
}
 
Example #14
Source File: TestArrayAccess04Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestArrayAccess04Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestArrayAccess04", "java.lang.Object", "TestArrayAccess04.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
Example #15
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 #16
Source File: TestReturn01Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
public TestReturn01Creator() {
  _cg = new ClassGen(TEST_PACKAGE+".TestReturn01", "java.lang.Object", "TestReturn01.java",
          Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {  });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
}
 
Example #17
Source File: ComplexObjectCreator.java    From cougar with Apache License 2.0 5 votes vote down vote up
public ComplexObjectCreator(String objectType) {
  _cg = new ClassGen(objectType, "java.lang.Object", "ComplexObject.java", ACC_PUBLIC | ACC_SUPER, new String[] { "com.betfair.cougar.api.Result" });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
  this.objectType = objectType;
}
 
Example #18
Source File: ComplexObjectCreator.java    From cougar with Apache License 2.0 5 votes vote down vote up
public ComplexObjectCreator(String objectType) {
  _cg = new ClassGen(objectType, "java.lang.Object", "ComplexObject.java", ACC_PUBLIC | ACC_SUPER, new String[] { "com.betfair.cougar.api.Result" });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
  this.objectType = objectType;
}
 
Example #19
Source File: ComplexObjectCreator.java    From cougar with Apache License 2.0 5 votes vote down vote up
public ComplexObjectCreator(String objectType) {
  _cg = new ClassGen(objectType, "java.lang.Object", "ComplexObject.java", ACC_PUBLIC | ACC_SUPER, new String[] { "com.betfair.cougar.api.Result" });

  _cp = _cg.getConstantPool();
  _factory = new InstructionFactory(_cg, _cp);
  this.objectType = objectType;
}
 
Example #20
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 #21
Source File: ProxyCreator.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Create JavaClass object for a simple proxy for an java.awt.event.ActionListener
 * that just prints the passed arguments, load and use it via the class loader
 * mechanism.
 */
public static void main(final String[] argv) throws Exception {
    final ClassLoader loader = ProxyCreator.class.getClassLoader();

    // instanceof won't work here ...
    // TODO this is broken; cannot ever be true now that ClassLoader has been dropped
    if (loader.getClass().toString().equals("class org.apache.bcel.util.ClassLoader")) {
        // Real class name will be set by the class loader
        final ClassGen cg = new ClassGen("foo", "java.lang.Object", "", Constants.ACC_PUBLIC,
                new String[]{"java.awt.event.ActionListener"});

        // That's important, otherwise newInstance() won't work
        cg.addEmptyConstructor(Constants.ACC_PUBLIC);

        final InstructionList il = new InstructionList();
        final ConstantPoolGen cp = cg.getConstantPool();
        final InstructionFactory factory = new InstructionFactory(cg);

        final int out = cp.addFieldref("java.lang.System", "out",
                "Ljava/io/PrintStream;");
        final int println = cp.addMethodref("java.io.PrintStream", "println",
                "(Ljava/lang/Object;)V");
        final MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, Type.VOID,
                new Type[]{
                        new ObjectType("java.awt.event.ActionEvent")
                }, null, "actionPerformed", "foo", il, cp);

        // System.out.println("actionPerformed:" + event);
        il.append(new GETSTATIC(out));
        il.append(factory.createNew("java.lang.StringBuffer"));
        il.append(InstructionConstants.DUP);
        il.append(new PUSH(cp, "actionPerformed:"));
        il.append(factory.createInvoke("java.lang.StringBuffer", "<init>", Type.VOID,
                new Type[]{Type.STRING}, Constants.INVOKESPECIAL));

        il.append(new ALOAD(1));
        il.append(factory.createAppend(Type.OBJECT));
        il.append(new INVOKEVIRTUAL(println));
        il.append(InstructionConstants.RETURN);

        mg.stripAttributes(true);
        mg.setMaxStack();
        mg.setMaxLocals();
        cg.addMethod(mg.getMethod());

        final byte[] bytes = cg.getJavaClass().getBytes();

        System.out.println("Uncompressed class: " + bytes.length);

        final String s = Utility.encode(bytes, true);
        System.out.println("Encoded class: " + s.length());

        System.out.print("Creating proxy ... ");
        final ActionListener a = (ActionListener) createProxy("foo.bar.", s);
        System.out.println("Done. Now calling actionPerformed()");

        a.actionPerformed(new ActionEvent(a, ActionEvent.ACTION_PERFORMED, "hello"));
    } else {
        System.err.println("Call me with java org.apache.bcel.util.JavaWrapper ProxyCreator");
    }
}
 
Example #22
Source File: JavaBuilder.java    From luaj with MIT License 4 votes vote down vote up
public JavaBuilder(ProtoInfo pi, String classname, String filename) {
	this.pi = pi;
	this.p = pi.prototype;
	this.classname = classname;
	
	// what class to inherit from
	superclassType = p.numparams;
	if ( p.is_vararg != 0 || superclassType >= SUPERTYPE_VARARGS )
		superclassType = SUPERTYPE_VARARGS;
	for ( int i=0, n=p.code.length; i<n; i++ ) {
		int inst = p.code[i];
		int o = Lua.GET_OPCODE(inst);
		if ( (o == Lua.OP_TAILCALL) ||
		     ((o == Lua.OP_RETURN) && (Lua.GETARG_B(inst) < 1 || Lua.GETARG_B(inst) > 2)) ) {
			superclassType = SUPERTYPE_VARARGS;
			break;
		}
	}
	
	// create class generator
	cg = new ClassGen(classname, SUPER_NAME_N[superclassType], filename,
			Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
	cp = cg.getConstantPool(); // cg creates constant pool

	// main instruction lists
	factory = new InstructionFactory(cg);
	init = new InstructionList();
	main = new InstructionList();

	// create the fields
	for ( int i=0; i<p.upvalues.length; i++ ) {
		boolean isrw = pi.isReadWriteUpvalue( pi.upvals[i] ); 
		Type uptype = isrw? (Type) TYPE_LOCALUPVALUE: (Type) TYPE_LUAVALUE;
		FieldGen fg = new FieldGen(0, uptype, upvalueName(i), cp);
		cg.addField(fg.getField());
	}
	
	// create the method
	mg = new MethodGen( Constants.ACC_PUBLIC | Constants.ACC_FINAL, // access flags
			RETURN_TYPE_N[superclassType], // return type
			ARG_TYPES_N[superclassType], // argument types
			ARG_NAMES_N[superclassType], // arg names
			METH_NAME_N[superclassType], 
			STR_LUAVALUE, // method, defining class
			main, cp);
	
	// initialize the values in the slots
	initializeSlots();	

	// initialize branching
	int nc = p.code.length;
	targets = new int[nc];
	branches = new BranchInstruction[nc];
	branchDestHandles = new InstructionHandle[nc];
	lastInstrHandles = new InstructionHandle[nc];
}