Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.ObjectType


               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 =
View Full Code Here


           "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"));
View Full Code Here

   
    out.print("\n");

    for(int i=0; i < ehs.length; i++) {
      CodeExceptionGen c = ehs[i];
      ObjectType caught = c.getCatchType();
      String class_name = (caught == null)// catch any exception, used when compiling finally
  "all" : caught.getClassName().replace('.', '/');

      out.println(".catch " + class_name + " from " +
      get(c.getStartPC()) + " to " + get(c.getEndPC()) +
      " using " + get(c.getHandlerPC()));
    }
View Full Code Here

   private static org.apache.bcel.classfile.Method createInvokeImpl(MBeanMetaData metadata, ClassGen classGen, String clsName)
   {
      InstructionList implementation = new InstructionList();

      ObjectType metadataType = new ObjectType(MBeanMetaData.class.getName());
      Type[] signature = new Type[]{metadataType, Type.STRING, new ArrayType(Type.STRING, 1), new ArrayType(Type.OBJECT, 1)};

      // Method definition
      MethodGen mthd = new MethodGen(Constants.ACC_PROTECTED, // Modifiers
                                     Type.OBJECT, // Return type
View Full Code Here

      // via direct call, otherwise we will have a class cast exception and go via reflection

      // Cast and invoke
      // Put the metadata on the stack, to access its 'mbean' field, that will be put on the stack
      // It's also the start of the try block
      InstructionHandle tryStart = implementation.append(factory.createLoad(new ObjectType(MBeanMetaData.class.getName()), 1));
      implementation.append(factory.createInvoke(MBeanMetaData.class.getName(), "getMBean", Type.OBJECT, new Type[0], Constants.INVOKEVIRTUAL));
      // Cast the 'mbean' field to the proper type, the stack will contain the casted mbean
      implementation.append(factory.createCheckCast(new ObjectType(management)));

      // Now add all the arguments to the stack
      Class[] signature = method.getParameterTypes();
      Type[] invokeSignature = new Type[signature.length];
      for (int i = 0; i < signature.length; ++i)
      {
         Class param = signature[i];

         // Load all args on the stack
         implementation.append(factory.createLoad(new ArrayType(Type.OBJECT, 1), 4));
         // I want index 'i'
         implementation.append(new PUSH(classGen.getConstantPool(), i));
         // Now on the stack there is args[i]
         implementation.append(factory.createArrayLoad(Type.OBJECT));

         // Save the signature for the invocation
         invokeSignature[i] = convertClassToType(param);

         if (param.isPrimitive())
         {
            // On the stack I have the wrapper object, I have to convert them to primitive
            replaceObjectWithPrimitive(param, implementation, factory);
         }
         else if (param.isArray())
         {
            // Cast args[i] to the proper class
            implementation.append(factory.createCheckCast((ReferenceType)invokeSignature[i]));
         }
         else
         {
            // Cast args[i] to the proper class
            implementation.append(factory.createCheckCast((ReferenceType)invokeSignature[i]));
         }
      }

      Class returnClass = method.getReturnType();
      Type returnType = convertClassToType(returnClass);

      // On the stack we now have the casted mbean and all the casted arguments, invoke
      implementation.append(factory.createInvoke(management, method.getName(), returnType, invokeSignature, Constants.INVOKEINTERFACE));

      if (returnClass == Void.TYPE)
      {
         implementation.append(InstructionConstants.ACONST_NULL);
      }
      else if (returnClass.isArray())
      {
         // Thanks to the fact that we can assign any array to Object, we do nothing here
      }
      else if (returnClass.isPrimitive())
      {
         replacePrimitiveWithObject(returnClass, methodGen, implementation, factory);
      }

      InstructionHandle tryEnd = implementation.append(factory.createReturn(Type.OBJECT));

      // In case of class cast exception, eat the exception and call super (hence using reflection)
      // catch (ClassCastException x) {/* do nothing */}
      // On the stack there is the exception, we assign it to local variable 'x'
      ObjectType exceptionTypeCCE = new ObjectType("java.lang.ClassCastException");
      LocalVariableGen x = methodGen.addLocalVariable("x", exceptionTypeCCE, null, null);
      InstructionHandle handler = implementation.append(factory.createStore(exceptionTypeCCE, x.getIndex()));
      x.setStart(handler);
      x.setEnd(handler);
      methodGen.addExceptionHandler(tryStart, tryEnd, handler, exceptionTypeCCE);
      // This catch block is followed by another one, and I don't exit with a throw or a return
      BranchInstruction skip = factory.createBranchInstruction(Constants.GOTO, null);
      catches.add(skip);
      implementation.append(skip);

      // An IllegalAccessError is thrown if the MBean interface or a parameter class is not public
      // We eat it and fall back to call super (hence using reflection)
      // catch (IllegalAccessError x) {/* do nothing */}
      ObjectType errorTypeIAE = new ObjectType("java.lang.IllegalAccessError");
      x = methodGen.addLocalVariable("x", errorTypeIAE, null, null);
      handler = implementation.append(factory.createStore(errorTypeIAE, x.getIndex()));
      x.setStart(handler);
      x.setEnd(handler);
      methodGen.addExceptionHandler(tryStart, tryEnd, handler, errorTypeIAE);
View Full Code Here

            cls = c;
         }
         Type t = convertClassToType(cls);
         return new ArrayType(t, dimensions);
      }
      return new ObjectType(cls.getName());
   }
View Full Code Here

   {
      // Put as first the most common ones
      if (type == int.class)
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Integer.class.getName())));
         implementation.append(factory.createInvoke(Integer.class.getName(), "intValue", Type.INT, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
      else if (type == boolean.class)
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Boolean.class.getName())));
         implementation.append(factory.createInvoke(Boolean.class.getName(), "booleanValue", Type.BOOLEAN, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
      else if (type == long.class)
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Long.class.getName())));
         implementation.append(factory.createInvoke(Long.class.getName(), "longValue", Type.LONG, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
      else if (type == byte.class)
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Byte.class.getName())));
         implementation.append(factory.createInvoke(Byte.class.getName(), "byteValue", Type.BYTE, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
      else if (type == char.class)
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Character.class.getName())));
         implementation.append(factory.createInvoke(Character.class.getName(), "charValue", Type.CHAR, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
      else if (type == short.class)
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Short.class.getName())));
         implementation.append(factory.createInvoke(Short.class.getName(), "shortValue", Type.SHORT, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
      else if (type == float.class)
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Float.class.getName())));
         implementation.append(factory.createInvoke(Float.class.getName(), "floatValue", Type.FLOAT, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
      else
      {
         // Cast the operand in the stack and get the value
         implementation.append(factory.createCheckCast(new ObjectType(Double.class.getName())));
         implementation.append(factory.createInvoke(Double.class.getName(), "doubleValue", Type.DOUBLE, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
      }
   }
View Full Code Here

      if (type == int.class)
      {
         // Create a new instance of the wrapper
         LocalVariableGen i = methodGen.addLocalVariable("i", Type.INT, null, null);
         i.setStart(implementation.append(factory.createStore(Type.INT, i.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Integer.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.INT, i.getIndex()));
         i.setEnd(implementation.append(factory.createInvoke(Integer.class.getName(), "<init>", Type.VOID, new Type[]{Type.INT}, Constants.INVOKESPECIAL)));
      }
      else if (type == boolean.class)
      {
         // Create a new instance of the wrapper
         LocalVariableGen b = methodGen.addLocalVariable("b", Type.BOOLEAN, null, null);
         b.setStart(implementation.append(factory.createStore(Type.BOOLEAN, b.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Boolean.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.BOOLEAN, b.getIndex()));
         b.setEnd(implementation.append(factory.createInvoke(Boolean.class.getName(), "<init>", Type.VOID, new Type[]{Type.BOOLEAN}, Constants.INVOKESPECIAL)));
      }
      else if (type == long.class)
      {
         // Create a new instance of the wrapper
         LocalVariableGen l = methodGen.addLocalVariable("l", Type.LONG, null, null);
         l.setStart(implementation.append(factory.createStore(Type.LONG, l.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Long.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.LONG, l.getIndex()));
         l.setEnd(implementation.append(factory.createInvoke(Long.class.getName(), "<init>", Type.VOID, new Type[]{Type.LONG}, Constants.INVOKESPECIAL)));
      }
      else if (type == byte.class)
      {
         // Create a new instance of the wrapper
         LocalVariableGen b = methodGen.addLocalVariable("b", Type.BYTE, null, null);
         b.setStart(implementation.append(factory.createStore(Type.BYTE, b.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Byte.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.BYTE, b.getIndex()));
         b.setEnd(implementation.append(factory.createInvoke(Byte.class.getName(), "<init>", Type.VOID, new Type[]{Type.BYTE}, Constants.INVOKESPECIAL)));
      }
      else if (type == char.class)
      {
         // Create a new instance of the wrapper
         LocalVariableGen c = methodGen.addLocalVariable("c", Type.CHAR, null, null);
         c.setStart(implementation.append(factory.createStore(Type.CHAR, c.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Character.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.CHAR, c.getIndex()));
         c.setEnd(implementation.append(factory.createInvoke(Character.class.getName(), "<init>", Type.VOID, new Type[]{Type.CHAR}, Constants.INVOKESPECIAL)));
      }
      else if (type == short.class)
      {
         // Create a new instance of the wrapper
         LocalVariableGen s = methodGen.addLocalVariable("s", Type.SHORT, null, null);
         s.setStart(implementation.append(factory.createStore(Type.SHORT, s.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Short.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.SHORT, s.getIndex()));
         s.setEnd(implementation.append(factory.createInvoke(Short.class.getName(), "<init>", Type.VOID, new Type[]{Type.SHORT}, Constants.INVOKESPECIAL)));
      }
      else if (type == float.class)
      {
         // Create a new instance of the wrapper
         LocalVariableGen f = methodGen.addLocalVariable("f", Type.FLOAT, null, null);
         f.setStart(implementation.append(factory.createStore(Type.FLOAT, f.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Float.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.FLOAT, f.getIndex()));
         f.setEnd(implementation.append(factory.createInvoke(Float.class.getName(), "<init>", Type.VOID, new Type[]{Type.FLOAT}, Constants.INVOKESPECIAL)));
      }
      else
      {
         // Create a new instance of the wrapper
         LocalVariableGen d = methodGen.addLocalVariable("d", Type.DOUBLE, null, null);
         d.setStart(implementation.append(factory.createStore(Type.DOUBLE, d.getIndex())));
         implementation.append(factory.createNew(new ObjectType(Double.class.getName())));
         implementation.append(InstructionConstants.DUP);
         implementation.append(factory.createLoad(Type.DOUBLE, d.getIndex()));
         d.setEnd(implementation.append(factory.createInvoke(Double.class.getName(), "<init>", Type.VOID, new Type[]{Type.DOUBLE}, Constants.INVOKESPECIAL)));
      }
   }
View Full Code Here

        // Build the initial frame situation for this method.
        Frame f = new Frame(mg.getMaxLocals(),mg.getMaxStack());
        if ( !mg.isStatic() ){
          if (mg.getName().equals(Constants.CONSTRUCTOR_NAME)){
            Frame._this = new UninitializedObjectType(new ObjectType(jc.getClassName()));
            f.getLocals().set(0, Frame._this);
          }
          else{
            Frame._this = null;
            f.getLocals().set(0, new ObjectType(jc.getClassName()));
          }
        }
        Type[] argtypes = mg.getArgumentTypes();
        int twoslotoffset = 0;
        for (int j=0; j<argtypes.length; j++){
View Full Code Here

            // cast the target instance to its original type
      iList.insert(
          insertPoint,
          factory.createCast(
            Type.OBJECT,
            new ObjectType(
              callInfo.getClassName() ) ) );
    }
   
    // get number of arguments for the method
    int paramCount = callInfo.getArgTypes().length;
View Full Code Here

TOP

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

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.