Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.InstructionFactory


    JavaClass clazz;
    Field loggerAttribute;
    public void init(JavaClass clazz, Field logger, ConstantPoolGen cp) {
        this.clazz = clazz;
        this.loggerAttribute = logger;
        this.instFact = new InstructionFactory(cp);
    }
View Full Code Here


        InstructionList il = new InstructionList();

        MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, Type.VOID,
                Type.NO_ARGS, null, "<init>", stubClassName, il, cp);

        InstructionFactory fac = new InstructionFactory(stubClass, cp);

        // call super-constructor
        il.append(InstructionFactory.createThis());
        il.append(fac.createInvoke(stubClass.getSuperclassName(), "<init>",
                Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));

        // push "this"
        il.append(InstructionFactory.createThis());

        // get static initializer
        il.append(fac.createGetStatic(stubClassName,
                initializerField.getName(), initializerField.getType()));

        emitInvoke(il, fac, getStubHandlerRef);

        // checkCast
        il.append(fac.createCast(Type.OBJECT, handlerField.getType()));

        // put handlerField
        il.append(new PUTFIELD(cp.addFieldref(stubClassName, handlerField
                .getName(), handlerField.getSignature())));
View Full Code Here

        Type[] args = new Type[] { handlerField.getType() };

        MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, args,
                null, "<init>", stubClassName, il, cp);

        InstructionFactory fac = new InstructionFactory(stubClass, cp);

        // call super-constructor
        il.append(InstructionFactory.createThis());
        il.append(fac.createInvoke(stubClass.getSuperclassName(), "<init>",
                Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));

        // push this again...
        il.append(InstructionFactory.createThis());
View Full Code Here

        }
        String handlerGetName = stub_methods[0].getName();

        ConstantPoolGen cp = clazz.getConstantPool();
        InstructionList il = new InstructionList();
        InstructionFactory fac = new InstructionFactory(clazz, cp);

        Type methodReturnType = translate(Object.class);
        Type[] methodArgTypes = new Type[0];

        MethodGen mg = new MethodGen(
                Constants.ACC_FINAL | Constants.ACC_PUBLIC, methodReturnType,
                methodArgTypes, null, // arg names
                handlerGetName, clazz.getClassName(), il, cp);

        mg.addAttribute(new Synthetic(cp.addUtf8("Synthetic"), 0, null, cp
                .getConstantPool()));

        //
        // construct method body
        //

        il.append(InstructionFactory.createThis());

        il.append(fac.createGetField(clazz.getClassName(), handlerField
                .getName(), handlerField.getType()));

        emitReturn(il, methodReturnType);

        //
View Full Code Here

        InstructionList il;

        cp = clazz.getConstantPool();
        il = new InstructionList();

        InstructionFactory fac = new InstructionFactory(clazz, cp);

        Type methodReturnType = translate(method.getReturnType());
        Type[] methodArgTypes = translate(method.getParameterTypes());

        MethodGen mg = new MethodGen(
                Constants.ACC_FINAL | Constants.ACC_PUBLIC, methodReturnType,
                methodArgTypes, null, // arg names
                method.getName(), clazz.getClassName(), il, cp);

        mg.addAttribute(new Synthetic(cp.addUtf8("Synthetic"), 0, null, cp
                .getConstantPool()));

        Class[] throwsException = method.getExceptionTypes();
        for (int i = 0; i < throwsException.length; i++) {
            mg.addException(throwsException[i].getName());
        }

        //
        // BODY
        //

        il.append(InstructionFactory.createThis());

        il.append(fac.createGetField(clazz.getClassName(), handlerField
                .getName(), handlerField.getType()));

        // push "this" as invoke's first argument
        il.append(InstructionFactory.createThis());

        // load data value
        if (dataField.isStatic()) {
            il.append(fac.createGetStatic(clazz.getClassName(), dataField
                    .getName(), dataField.getType()));
        } else {
            il.append(InstructionFactory.createThis());
            il.append(fac.createGetField(clazz.getClassName(), dataField
                    .getName(), dataField.getType()));
        }

        il.append(new PUSH(cp, methodArgTypes.length));
        il.append((Instruction) fac.createNewArray(Type.OBJECT, (short) 1));

        int index = 1;
        for (int i = 0; i < methodArgTypes.length; i++) {
            // dup array ref
            il.append(InstructionConstants.DUP);
View Full Code Here

        InstructionList il;

        cp = clazz.getConstantPool();
        il = new InstructionList();

        InstructionFactory fac = new InstructionFactory(clazz, cp);

        Type methodReturnType = translate(method.getReturnType());
        Type[] methodArgTypes = translate(method.getParameterTypes());

        MethodGen mg = new MethodGen(
View Full Code Here

                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(Constants.ACC_PROTECTED);
View Full Code Here

           new ArrayType(Type.STRING, 1)
               },
               new String[] { "argv" }, // arg names
               "main", "HelloWorld",    // method, class
               il, cp);
    InstructionFactory factory = new InstructionFactory(cg);

    ObjectType i_stream = new ObjectType("java.io.InputStream");
    ObjectType p_stream = new ObjectType("java.io.PrintStream");

    /* Create BufferedReader object and store it in local variable `in'.
     */
    il.append(factory.createNew("java.io.BufferedReader"));
    il.append(InstructionConstants.DUP); // Use predefined constant, i.e. flyweight
    il.append(factory.createNew("java.io.InputStreamReader"));
    il.append(InstructionConstants.DUP);
    il.append(factory.createFieldAccess("java.lang.System", "in", i_stream,
          Constants.GETSTATIC));

    /* Call constructors, i.e. BufferedReader(InputStreamReader())
     */
    il.append(factory.createInvoke("java.io.InputStreamReader", "<init>",
           Type.VOID, new Type[] { i_stream },
           Constants.INVOKESPECIAL));
    il.append(factory.createInvoke("java.io.BufferedReader", "<init>", Type.VOID,
           new Type[] { new ObjectType("java.io.Reader") },
           Constants.INVOKESPECIAL));

    /* Create local variable `in'
     */
    LocalVariableGen lg = mg.addLocalVariable("in",
                new ObjectType("java.io.BufferedReader"),
                null, null);
    int in = lg.getIndex();
    lg.setStart(il.append(new ASTORE(in))); // `i' valid from here

    /* Create local variable `name'
     */
    lg = mg.addLocalVariable("name", Type.STRING, null, null);
    int name = lg.getIndex();
    il.append(InstructionConstants.ACONST_NULL);
    lg.setStart(il.append(new ASTORE(name))); // `name' valid from here

    /* try { ...
     */
    InstructionHandle try_start =
      il.append(factory.createFieldAccess("java.lang.System", "out", p_stream,
            Constants.GETSTATIC));

    il.append(new PUSH(cp, "Please enter your name> "));
    il.append(factory.createInvoke("java.io.PrintStream", "print", Type.VOID,
           new Type[] { Type.STRING }, Constants.INVOKEVIRTUAL));
    il.append(new ALOAD(in));
    il.append(factory.createInvoke("java.io.BufferedReader", "readLine",
           Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
    il.append(new ASTORE(name));

    /* Upon normal execution we jump behind exception handler,
     * the target address is not known yet.
     */
    GOTO g = new GOTO(null);
    InstructionHandle try_end = il.append(g);

    /* } catch() { ... }
     * Add exception handler: print exception and return from method
     */
    InstructionHandle handler =
      il.append(factory.createFieldAccess("java.lang.System", "out", p_stream,
            Constants.GETSTATIC));
    // Little trick in order not to save exception object temporarily
    il.append(InstructionConstants.SWAP);

    il.append(factory.createInvoke("java.io.PrintStream", "println", Type.VOID,
     new Type[] { Type.OBJECT }, Constants.INVOKEVIRTUAL));
    il.append(InstructionConstants.RETURN);
    mg.addExceptionHandler(try_start, try_end, handler,
         new ObjectType("java.io.IOException"));

    /* Normal code continues, now we can set the branch target of the GOTO
     * that jumps over the handler code.
     */
    InstructionHandle ih =
      il.append(factory.createFieldAccess("java.lang.System", "out", p_stream,
            Constants.GETSTATIC));
    g.setTarget(ih);

    /* String concatenation compiles to StringBuffer operations.
     */
    il.append(factory.createNew(Type.STRINGBUFFER));
    il.append(InstructionConstants.DUP);
    il.append(new PUSH(cp, "Hello, "));
    il.append(factory.createInvoke("java.lang.StringBuffer", "<init>",
           Type.VOID, new Type[] { Type.STRING },
           Constants.INVOKESPECIAL));
    il.append(new ALOAD(name));
   
    /* Concatenate strings using a StringBuffer and print them.
     */
    il.append(factory.createInvoke("java.lang.StringBuffer", "append",
           Type.STRINGBUFFER, new Type[] { Type.STRING },
           Constants.INVOKEVIRTUAL));
    il.append(factory.createInvoke("java.lang.StringBuffer", "toString",
           Type.STRING, Type.NO_ARGS,
           Constants.INVOKEVIRTUAL));
   
    il.append(factory.createInvoke("java.io.PrintStream", "println",
           Type.VOID, new Type[] { Type.STRING },
           Constants.INVOKEVIRTUAL));

    il.append(InstructionConstants.RETURN);

View Full Code Here

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

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

      int out     = cp.addFieldref("java.lang.System", "out",
           "Ljava/io/PrintStream;");
      int println = cp.addMethodref("java.io.PrintStream", "println",
          "(Ljava/lang/Object;)V");
      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();
View Full Code Here

                                     implementation, // Implementation
                                     classGen.getConstantPool()); // Pool
      mthd.addException("java.lang.Throwable");

      // Now I should create the implementation
      InstructionFactory factory = new InstructionFactory(classGen);

      Method[] methods = metadata.getMBeanInterface().getMethods();
      List tests = new ArrayList();
      List catches = new ArrayList();
      for (int i = 0; i < methods.length; ++i)
      {
         Method method = methods[i];
         catches.addAll(generateDirectInvokeBranch(classGen, mthd, implementation, factory, metadata.getMBeanInterface().getName(), method, tests));
      }

      // To close the last branch, I must jump to super.invokeImpl(), so I need its first instruction here
      InstructionHandle invokeSuper = implementation.append(factory.createThis());
      for (int i = 0; i < tests.size(); ++i)
      {
         BranchInstruction branch = (BranchInstruction)tests.get(i);
         branch.setTarget(invokeSuper);
      }
      tests.clear();
      for (int i = 0; i < catches.size(); ++i)
      {
         BranchInstruction branch = (BranchInstruction)catches.get(i);
         branch.setTarget(invokeSuper);
      }
      catches.clear();

      //
      // return super.invokeImpl(metadata, method, params, args);
      //
      // Again, it's invokeImpl(super, args) instead of super.invokeImpl(args)
      // Use 'this' as first argument, and invokespecial instead of invokevirtual to call super
      // 'this' is created above, to close the last branch
      implementation.append(factory.createLoad(metadataType, 1));
      implementation.append(factory.createLoad(Type.STRING, 2));
      implementation.append(factory.createLoad(new ArrayType(Type.STRING, 1), 3));
      implementation.append(factory.createLoad(new ArrayType(Type.OBJECT, 1), 4));
      implementation.append(factory.createInvoke(BCELMBeanInvoker.class.getName(), "invokeImpl", Type.OBJECT, signature, Constants.INVOKESPECIAL));
      implementation.append(factory.createReturn(Type.OBJECT));

      mthd.setMaxStack();

      org.apache.bcel.classfile.Method method = mthd.getMethod();
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.InstructionFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.