Package org.objectweb.asm

Examples of org.objectweb.asm.CodeVisitor


      String name,
      String desc,
      String[] exceptions,
      Attribute attrs)
    {
      CodeVisitor cv = super.visitMethod(access, name, desc, exceptions, attrs);
      if (!name.equals("<init>") &&
          (access & (ACC_STATIC + ACC_NATIVE + ACC_ABSTRACT)) == 0)
      {
        cv.visitVarInsn(ALOAD, 0);
        cv.visitVarInsn(ALOAD, 0);
        cv.visitFieldInsn(GETFIELD, owner, "_counter", "I");
        cv.visitInsn(ICONST_1);
        cv.visitInsn(IADD);
        cv.visitFieldInsn(PUTFIELD, owner, "_counter", "I");
        return new CounterCodeAdapter(cv);
      }
      return cv;
    }
View Full Code Here


    final String name,
    final String desc,
    final String[] exceptions,
    final Attribute attrs)
  {
    CodeVisitor mv = cv.visitMethod(
      access,
      name,
      desc,
      exceptions,
      new CommentAttribute("this is a method comment", attrs));
    if (mv != null) {
      mv.visitAttribute(new CommentAttribute("this is a code comment"));
    }
    return mv;
  }
View Full Code Here

   */

  public void accept (final ClassVisitor cv) {
    String[] exceptions = new String[this.exceptions.size()];
    this.exceptions.toArray(exceptions);
    CodeVisitor mv = cv.visitMethod(access, name, desc, exceptions, attrs);
    if (mv != null && instructions.size() > 0) {
      int i;
      // visits instructions
      for (i = 0; i < instructions.size(); ++i) {
        Object insn = instructions.get(i);
        if (insn instanceof Label) {
          mv.visitLabel((Label)insn);
        } else {
          ((AbstractInsnNode)insn).accept(mv);
        }
      }
      // visits try catch blocks
      for (i = 0; i < tryCatchBlocks.size(); ++i) {
        ((TryCatchBlockNode)tryCatchBlocks.get(i)).accept(mv);
      }
      // visits maxs
      mv.visitMaxs(maxStack, maxLocals);
      // visits local variables
      for (i = 0; i < localVariables.size(); ++i) {
        ((LocalVariableNode)localVariables.get(i)).accept(mv);
      }
      // visits line numbers
      for (i = 0; i < lineNumbers.size(); ++i) {
        ((LineNumberNode)lineNumbers.get(i)).accept(mv);
      }
      // visits the code attributes
      Attribute attrs = codeAttrs;
      while (attrs != null) {
        mv.visitAttribute(attrs);
        attrs = attrs.next;
      }
    }
  }
View Full Code Here

      Type t = Type.getType(desc);
      int size = t.getSize();

      // generates getter method
      String gDesc = "()" + desc;
      CodeVisitor gv =
        cv.visitMethod(ACC_PRIVATE, "_get" + name, gDesc, null, null);
      gv.visitFieldInsn(GETSTATIC,
        "java/lang/System", "err", "Ljava/io/PrintStream;");
      gv.visitLdcInsn("_get" + name + " called");
      gv.visitMethodInsn(INVOKEVIRTUAL,
        "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
      gv.visitVarInsn(ALOAD, 0);
      gv.visitFieldInsn(GETFIELD, owner, name, desc);
      gv.visitInsn(t.getOpcode(IRETURN));
      gv.visitMaxs(1 + size, 1);

      // generates setter method
      String sDesc = "(" + desc + ")V";
      CodeVisitor sv =
        cv.visitMethod(ACC_PRIVATE, "_set" + name, sDesc, null, null);
      sv.visitFieldInsn(GETSTATIC,
        "java/lang/System", "err", "Ljava/io/PrintStream;");
      sv.visitLdcInsn("_set" + name + " called");
      sv.visitMethodInsn(INVOKEVIRTUAL,
        "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
      sv.visitVarInsn(ALOAD, 0);
      sv.visitIntInsn(t.getOpcode(ILOAD), 1);
      sv.visitFieldInsn(PUTFIELD, owner, name, desc);
      sv.visitInsn(RETURN);
      sv.visitMaxs(1 + size, 1 + size);
    }
  }
View Full Code Here

    final String name,
    final String desc,
    final String[] exceptions,
    final Attribute attrs)
  {
    CodeVisitor mv = cv.visitMethod(access, name, desc, exceptions, attrs);
    return mv == null ? null : new TraceFieldCodeAdapter(mv, owner);
  }
View Full Code Here

    String[] itfs = {Expression.class.getName()};
    ClassWriter cw = new ClassWriter(true);
    cw.visit(V1_1, ACC_PUBLIC, name, "java/lang/Object", itfs, null);

    // default public constructor
    CodeVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mw.visitVarInsn(ALOAD, 0);
    mw.visitMethodInsn(
      INVOKESPECIAL,
      "java/lang/Object", "<init>", "()V");
    mw.visitInsn(RETURN);
    mw.visitMaxs(1, 1);

    // eval method
    mw = cw.visitMethod(ACC_PUBLIC, "eval", "(II)I", null, null);
    compile(mw);
    mw.visitInsn(IRETURN);
    // max stack and max locals automatically computed
    mw.visitMaxs(0, 0);

    return cw.toByteArray();
  }
View Full Code Here

        .append(attr.toString()).append("\n");
      text.add(buf.toString());
      attr = attr.next;
    }

    CodeVisitor cv;
    if (this.cv != null) {
      cv = this.cv.visitMethod(access, name, desc, exceptions, attrs);
    } else {
      cv = null;
    }
View Full Code Here

       

    }
   
    protected void generateNoArgConstructor(HomeContext gc) {
        CodeVisitor mv = gc.cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, gc.superClassJCN, "<init>", "()V");
       
      String listenclass = (String) gc.ctx.get("listenClass");
      if (listenclass != null) {
          //this.eventListener = new MyListener()
            mv.visitVarInsn(ALOAD, 0);
            mv.visitTypeInsn(NEW, getJVMClassName(listenclass));
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, getJVMClassName(listenclass),
                "<init>", "()V");
            mv.visitFieldInsn(PUTFIELD, gc.xHomeJCN, LISTENER_FIELD,
              getJVMType(listenclass));
      }         
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
    }
View Full Code Here

    "org/objectweb/speedo/mapper/lib/Object2StringSerializer", //superName
    new String[0], //interfaces
    str // sourcefile
    );
    //implement an empty public constructor
    CodeVisitor c = cw.visitMethod(Constants.ACC_PUBLIC, "<init>", "()V", null, null);
    c.visitVarInsn(Constants.ALOAD, 0);
    c.visitMethodInsn(
      Constants.INVOKESPECIAL,
      "org/objectweb/speedo/mapper/lib/Object2StringSerializer",
      "<init>",
      "()V");
    c.visitInsn(Constants.RETURN);
    c.visitMaxs(0, 0);

    //implement the getSerializedObject method
    c = cw.visitMethod(Constants.ACC_PUBLIC, "getSerializedObject", "()Ljava/lang/String;", null, null);
    c.visitTypeInsn(Constants.NEW, "java/lang/StringBuffer");
    c.visitInsn(Constants.DUP);
    c.visitMethodInsn(Constants.INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "()V");
    final int SPLIT_SIZE = 5000;
    int nbSplit = value.length() / SPLIT_SIZE;
    for (int i = 0; i < nbSplit; i++) {
      int j = i * SPLIT_SIZE;
      String s = value.substring(j, j + SPLIT_SIZE);
      c.visitLdcInsn(s);
      c.visitMethodInsn(
        Constants.INVOKEVIRTUAL,
        "java/lang/StringBuffer",
        "append",
        "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
    }
    int last = value.length() % SPLIT_SIZE;
    if (last > 0) {
      c.visitLdcInsn(value.substring(SPLIT_SIZE * nbSplit));
      c.visitMethodInsn(
        Constants.INVOKEVIRTUAL,
        "java/lang/StringBuffer",
        "append",
        "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
    }
    c.visitMethodInsn(Constants.INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
    c.visitInsn(Constants.ARETURN);
    c.visitMaxs(1, 1);
   
    return cw;
  }
View Full Code Here

      if (gc.sc.callBacks.isEmpty()) {
        return;
      }
     
      // public void sendEvt...
        CodeVisitor mv = gc.cv.visitMethod(ACC_PUBLIC, "sendEvent",
                "(ILjava/lang/Object;Ljava/lang/Object;Z)V", null, null);
        int nbCase = gc.sc.callBacks.size();
       
      int[] keys = new int[nbCase];
      Label[] keyLabels = new Label[nbCase];
      int i = 0;
      List keysList = new ArrayList((Set<Integer>) gc.sc.callBacks.keySet());
      Collections.sort(keysList);
        for (int cbid : (List<Integer>) keysList) {
        keys[i] = cbid;
        keyLabels[i] = new Label();
        i++;
      }
        Label switchEndLabel = new Label();
        mv.visitVarInsn(ILOAD, 1);
        mv.visitLookupSwitchInsn(switchEndLabel, keys, keyLabels);
       
      String listenclass = null;
      // switch (evt.type) {
      for (int j = 0; j < nbCase; j++) {
        // case [assoc EJB_CB(cbn) <-> evt.type]:
        mv.visitLabel(keyLabels[j]);
        ArrayList<SpeedoCallback> cbl = (ArrayList<SpeedoCallback>) gc.sc.callBacks.get(keys[j]);
        if (cbl == null) {
          continue;
        }
        for (SpeedoCallback actualcbn : cbl) {
          if (actualcbn.listenerClassName == null) { // this a callback from the persistent class
            // CALL THE CALLBACK METHOD INTO THE PERSISTENT CLASS
               mv.visitVarInsn(ALOAD, 2);
               mv.visitTypeInsn(CHECKCAST, gc.xJCN);
               mv.visitMethodInsn(INVOKEVIRTUAL, gc.xJCN,
                   actualcbn.callbackName,
                   actualcbn.methodByteCodeSignature);
          } else { // this is a callback from the listener class
            if (listenclass == null) {
              listenclass = actualcbn.listenerClassName;
            }
            // CALL THE CALLBACK METHOD INTO THE LISTENER CLASS
            //this.eventListener.myMethod(param1)
                mv.visitVarInsn(ALOAD, 0);
                mv.visitFieldInsn(GETFIELD, gc.xHomeJCN, LISTENER_FIELD,
                     getJVMType(actualcbn.listenerClassName));
                mv.visitVarInsn(ALOAD, 2);
                mv.visitTypeInsn(CHECKCAST, gc.xJCN);
                mv.visitMethodInsn(INVOKEVIRTUAL,
                    getJVMClassName(actualcbn.listenerClassName),
                    actualcbn.callbackName,
                    actualcbn.methodByteCodeSignature);
          }
        }
        // break;
          mv.visitJumpInsn(GOTO, switchEndLabel);
      }
      mv.visitLabel(switchEndLabel);
      // }
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
     
      if (listenclass != null) {
        // ADD THE DEFINITION OF THE LISTENER CLASS VARIABLE
            gc.cv.visitField(ACC_PUBLIC + ACC_FINAL,
                LISTENER_FIELD, getJVMType(listenclass), null, null);
          gc.ctx.put("listenClass", listenclass);
      }
       
        //public boolean hasInstanceLifeCycleListeners() {
        mv = gc.cv.visitMethod(ACC_PUBLIC, "hasInstanceLifeCycleListeners",
                "()Z", null, null);
        //return true;
        mv.visitInsn(ICONST_1);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        //}
       

    }
View Full Code Here

TOP

Related Classes of org.objectweb.asm.CodeVisitor

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.