Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ConstantPoolGen


        clazz.addMethod(mg.getMethod());
    }

    static void generate(ClassGen clazz, MethodRef method, FieldGen dataField,
            FieldGen handlerField, MethodRef handlerMethodRef) {
        ConstantPoolGen cp;
        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);

            // push index
            il.append(new PUSH(cp, i));

            // transform parameter
            il.append(InstructionFactory.createLoad(methodArgTypes[i], index));
            emitCoerceToObject(il, fac, methodArgTypes[i]);

            // and store into array
            il.append(InstructionFactory.createArrayStore(Type.OBJECT));

            index += methodArgTypes[i].getSize();
        }

        //
        // invoke handler's method
        //
        InstructionHandle tryStart = emitInvoke(il, fac, handlerMethodRef);

        // convert to primitive type
        emitCoerceFromObject(il, fac, methodReturnType);

        // and return

        InstructionHandle tryEnd = emitReturn(il, methodReturnType);

        //
        // catch...
        //
        InstructionHandle rethrowLocation = il.append(new ATHROW());

        Class[] exceptions = method.getExceptionTypes();
        boolean handle_throwable_exception = true;
        boolean handle_runtime_exception = true;
        if (exceptions != null) {
            for (int i = 0; i < exceptions.length; i++) {
                Class ex = exceptions[i];

                if (ex == java.lang.Throwable.class)
                    handle_throwable_exception = false;

                if (ex == java.lang.RuntimeException.class
                        || ex == java.lang.Exception.class)
                    handle_runtime_exception = false;

                mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
                        (ObjectType) translate(ex));
            }
        }

        // A RuntimeException should not cause an
        // UndeclaredThrowableException, so we catch and re-throw it
        // that before throwable.
        if (handle_throwable_exception && handle_runtime_exception) {
            mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
                    new ObjectType("java.lang.RuntimeException"));
        }

        // If anything else is thrown, it is wrapped in an
        // UndeclaredThrowable
        if (handle_throwable_exception) {
            InstructionHandle handlerStart = il.append(new ASTORE(1));

            il
                    .append(new NEW(
                            cp
                                    .addClass("java.lang.reflect.UndeclaredThrowableException")));
            il.append(InstructionConstants.DUP);
            il.append(new ALOAD(1));
            il.append(new INVOKESPECIAL(cp.addMethodref(
                    "java.lang.reflect.UndeclaredThrowableException", "<init>",
                    "(Ljava/lang/Throwable;)V")));

            il.append(new ATHROW());
View Full Code Here


        clazz.addMethod(mg.getMethod());
    }

    static void generateSuperMethod(ClassGen clazz, MethodRef method) {
        ConstantPoolGen cp;
        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());
        }

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

        // push arguments
        int index = 1;
        for (int i = 0; i < methodArgTypes.length; i++) {
            emitLoad(il, index, methodArgTypes[i]);
            index += methodArgTypes[i].getSize();
        }

        // call method
        il.append(new INVOKESPECIAL(cp.addMethodref(method.getDeclaringClass()
                .getName(), method.getName(), method.getSignature())));

        emitReturn(il, methodReturnType);

        //
View Full Code Here

    public void translateFrom(ClassGenerator classGen,
  MethodGenerator methodGen, Class clazz)
    {
   
    InstructionList il = methodGen.getInstructionList();
  ConstantPoolGen cpg = classGen.getConstantPool();
  if (clazz.getName().equals("org.w3c.dom.NodeList")) {
     // w3c NodeList is on the stack from the external Java function call.
     // call BasisFunction to consume NodeList and leave Iterator on
     //    the stack.
     il.append(classGen.loadTranslet());   // push translet onto stack
     il.append(methodGen.loadDOM());      // push DOM onto stack
     final int convert = cpg.addMethodref(BASIS_LIBRARY_CLASS,
          "nodeList2Iterator",
          "("   
           + "Lorg/w3c/dom/NodeList;"
           + TRANSLET_INTF_SIG
           + DOM_INTF_SIG
           + ")" + NODE_ITERATOR_SIG );
     il.append(new INVOKESTATIC(convert));
  }
  else if (clazz.getName().equals("org.w3c.dom.Node")) {
     // w3c Node is on the stack from the external Java function call.
     // call BasisLibrary.node2Iterator() to consume Node and leave
     // Iterator on the stack.
     il.append(classGen.loadTranslet());   // push translet onto stack
     il.append(methodGen.loadDOM());      // push DOM onto stack
     final int convert = cpg.addMethodref(BASIS_LIBRARY_CLASS,
          "node2Iterator",
          "("   
           + "Lorg/w3c/dom/Node;"
           + TRANSLET_INTF_SIG
           + DOM_INTF_SIG
View Full Code Here

     * Expects a node-set on the stack and pushes an object of the appropriate
     * type after coercion.
     */
    public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
          Class clazz) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();
  final String className = clazz.getName();

  il.append(methodGen.loadDOM());
  il.append(SWAP);

  if (className.equals("org.w3c.dom.Node")) {
      int index = cpg.addInterfaceMethodref(DOM_INTF,
              MAKE_NODE,
              MAKE_NODE_SIG2);
      il.append(new INVOKEINTERFACE(index, 2));
  }
        else if (className.equals("org.w3c.dom.NodeList") ||
                 className.equals("java.lang.Object")) {
      int index = cpg.addInterfaceMethodref(DOM_INTF,
              MAKE_NODE_LIST,
              MAKE_NODE_LIST_SIG2);
      il.append(new INVOKEINTERFACE(index, 2));
  }
        else if (className.equals("java.lang.String")) {
            int next = cpg.addInterfaceMethodref(NODE_ITERATOR,
                                                 "next", "()I");
            int index = cpg.addInterfaceMethodref(DOM_INTF,
                                                 GET_NODE_VALUE,
                                                 "(I)"+STRING_SIG);

            // Get next node from the iterator
            il.append(new INVOKEINTERFACE(next, 1));
            // Get the node's string value (from the DOM)
            il.append(new INVOKEINTERFACE(index, 2));
                      
        }
  else if (className.equals("int")) {
      int next = cpg.addInterfaceMethodref(NODE_ITERATOR,
              "next", "()I");
      int index = cpg.addInterfaceMethodref(DOM_INTF,
              GET_NODE_VALUE,
              "(I)"+STRING_SIG);
      int str = cpg.addMethodref(BASIS_LIBRARY_CLASS,
          STRING_TO_INT,
          STRING_TO_INT_SIG);

      // Get next node from the iterator
      il.append(new INVOKEINTERFACE(next, 1));
View Full Code Here

    /**
     * Some type conversions require gettting the first node from the node-set.
     * This function is defined to avoid code repetition.
     */
    private void getFirstNode(ClassGenerator classGen, MethodGenerator methodGen) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();
  il.append(new INVOKEINTERFACE(cpg.addInterfaceMethodref(NODE_ITERATOR,
                NEXT,
                NEXT_SIG), 1));
    }
View Full Code Here

     *
     * @see  org.apache.xalan.xsltc.compiler.util.Type#translateTo
     */
    public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
          RealType type) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();
  il.append(new INVOKESTATIC(cpg.addMethodref(BASIS_LIBRARY_CLASS,
                STRING_TO_REAL,
                STRING_TO_REAL_SIG)));
    }
View Full Code Here

     * @see  org.apache.xalan.xsltc.compiler.util.Type#translateToDesynthesized
     */
    public FlowList translateToDesynthesized(ClassGenerator classGen,
               MethodGenerator methodGen,
               BooleanType type) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();

  il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_CLASS,
                 "length", "()I")));
  return new FlowList(il.append(new IFEQ(null)));
    }
View Full Code Here

     * @see  org.apache.xalan.xsltc.compiler.util.Type#translateFrom
     */
    public void translateFrom(ClassGenerator classGen,
  MethodGenerator methodGen, Class clazz)
    {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();

  if (clazz.getName().equals("java.lang.String")) {
      // same internal representation, convert null to ""
      il.append(DUP);
View Full Code Here

     * @see  org.apache.xalan.xsltc.compiler.util.Type#translateTo
     */
    public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
          StringType type) {
  final int current = methodGen.getLocalIndex("current");
  ConstantPoolGen cpg = classGen.getConstantPool();
  InstructionList il = methodGen.getInstructionList();

  il.append(new ILOAD(current));
  il.append(methodGen.loadDOM());
  final int stringF = cpg.addMethodref(BASIS_LIBRARY_CLASS,
               "stringF",
               "("
               + OBJECT_SIG
               + NODE_SIG
               + DOM_INTF_SIG
View Full Code Here

     *
     * @see  org.apache.xalan.xsltc.compiler.util.Type#translateTo
     */
    public void translateTo(ClassGenerator classGen, MethodGenerator methodGen,
          RealType type) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();

  il.append(methodGen.loadDOM());
  int index = cpg.addMethodref(BASIS_LIBRARY_CLASS, "numberF",
             "("
             + OBJECT_SIG
             + DOM_INTF_SIG
             + ")D");
  il.append(new INVOKESTATIC(index));
View Full Code Here

TOP

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

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.