Example #1
0
 public Assembler defaultValue(String desc) {
   int typeCode = desc.charAt(0);
   switch (typeCode) {
     case '[':
     case 'L':
       mv.visitInsn(ACONST_NULL);
       break;
     case 'Z':
     case 'C':
     case 'B':
     case 'S':
     case 'I':
       mv.visitInsn(ICONST_0);
       break;
     case 'J':
       mv.visitInsn(LCONST_0);
       break;
     case 'F':
       mv.visitInsn(FCONST_0);
       break;
     case 'D':
       mv.visitInsn(DCONST_0);
       break;
   }
   return this;
 }
Example #2
0
 // print the object on the top of the stack
 public Assembler printObject() {
   mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
   mv.visitInsn(SWAP);
   mv.visitMethodInsn(
       INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V", false);
   return this;
 }
Example #3
0
 public Assembler sub(Type t) {
   int opcode = -1;
   switch (t.getSort()) {
     case Type.SHORT:
     case Type.BYTE:
     case Type.INT:
       {
         opcode = ISUB;
         break;
       }
     case Type.LONG:
       {
         opcode = LSUB;
         break;
       }
     case Type.FLOAT:
       {
         opcode = FSUB;
         break;
       }
     case Type.DOUBLE:
       {
         opcode = DSUB;
         break;
       }
   }
   if (opcode != -1) {
     mv.visitInsn(opcode);
   }
   return this;
 }
Example #4
0
 public Assembler dupReturnValue(int returnOpcode) {
   switch (returnOpcode) {
     case IRETURN:
     case FRETURN:
     case ARETURN:
       mv.visitInsn(DUP);
       break;
     case LRETURN:
     case DRETURN:
       mv.visitInsn(DUP2);
       break;
     case RETURN:
       break;
     default:
       throw new IllegalArgumentException("not return");
   }
   return this;
 }
  /** Generate class bytes for java.lang.Runnable implementation and return the same. */
  public byte[] generate(Method method, String className) {
    int modifiers = method.getModifiers();
    // make sure that the method is public static
    // and accepts no arguments
    if (!Modifier.isStatic(modifiers)
        || !Modifier.isPublic(modifiers)
        || method.getParameterTypes().length != 0) {
      throw new IllegalArgumentException();
    }
    Class clazz = method.getDeclaringClass();
    modifiers = clazz.getModifiers();
    // make sure that the class is public as well
    if (!Modifier.isPublic(modifiers)) {
      throw new IllegalArgumentException();
    }

    ClassWriter cw = InstrumentUtils.newClassWriter();
    cw.visit(
        V1_1, ACC_PUBLIC, className, null, "java/lang/Object", new String[] {"java/lang/Runnable"});
    // creates a MethodWriter for the (implicit) constructor
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    // pushes the 'this' variable
    mw.visitVarInsn(ALOAD, 0);
    // invokes the super class constructor
    mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mw.visitInsn(RETURN);
    mw.visitMaxs(1, 1);
    mw.visitEnd();

    // creates a MethodWriter for the 'main' method
    mw = cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null);
    // invokes the given method
    mw.visitMethodInsn(
        INVOKESTATIC,
        Type.getInternalName(method.getDeclaringClass()),
        method.getName(),
        Type.getMethodDescriptor(method));
    mw.visitInsn(RETURN);
    mw.visitMaxs(1, 1);
    mw.visitEnd();
    return cw.toByteArray();
  }
Example #6
0
 public Assembler push(int value) {
   if (value >= -1 && value <= 5) {
     mv.visitInsn(ICONST_0 + value);
   } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
     mv.visitIntInsn(BIPUSH, value);
   } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
     mv.visitIntInsn(SIPUSH, value);
   } else {
     mv.visitLdcInsn(value);
   }
   return this;
 }
Example #7
0
 public Assembler dupValue(String desc) {
   int typeCode = desc.charAt(0);
   switch (typeCode) {
     case '[':
     case 'L':
     case 'Z':
     case 'C':
     case 'B':
     case 'S':
     case 'I':
       mv.visitInsn(DUP);
       break;
     case 'J':
     case 'D':
       mv.visitInsn(DUP2);
       break;
     default:
       throw new RuntimeException("invalid signature");
   }
   return this;
 }
Example #8
0
 public Assembler ldc(Object o) {
   if (o instanceof Integer) {
     int i = (int) o;
     if (i >= -1 && i <= 5) {
       int opcode = -1;
       switch (i) {
         case 0:
           {
             opcode = ICONST_0;
             break;
           }
         case 1:
           {
             opcode = ICONST_1;
             break;
           }
         case 2:
           {
             opcode = ICONST_2;
             break;
           }
         case 3:
           {
             opcode = ICONST_3;
             break;
           }
         case 4:
           {
             opcode = ICONST_4;
             break;
           }
         case 5:
           {
             opcode = ICONST_5;
             break;
           }
         case -1:
           {
             opcode = ICONST_M1;
             break;
           }
       }
       mv.visitInsn(opcode);
       return this;
     }
   }
   if (o instanceof Long) {
     long l = (long) o;
     if (l >= 0 && l <= 1) {
       int opcode = -1;
       switch ((int) l) {
         case 0:
           {
             opcode = LCONST_0;
             break;
           }
         case 1:
           {
             opcode = LCONST_1;
             break;
           }
       }
       mv.visitInsn(opcode);
       return this;
     }
   }
   mv.visitLdcInsn(o);
   return this;
 }
Example #9
0
 public Assembler arrayStore(Type type) {
   mv.visitInsn(type.getOpcode(IASTORE));
   return this;
 }
Example #10
0
 public Assembler arrayLoad(Type type) {
   mv.visitInsn(type.getOpcode(IALOAD));
   return this;
 }
Example #11
0
 public Assembler swap() {
   mv.visitInsn(SWAP);
   return this;
 }
Example #12
0
 public Assembler dup2() {
   mv.visitInsn(DUP2);
   return this;
 }
Example #13
0
 public Assembler pop() {
   mv.visitInsn(POP);
   return this;
 }
Example #14
0
 public Assembler loadNull() {
   mv.visitInsn(ACONST_NULL);
   return this;
 }