Exemplo n.º 1
0
 /**
  * Emits code boxing value of basic type.
  *
  * @param mv output method visitor
  * @param type input data type (must be basic type!)
  */
 public void emitAutoboxing(SpyMethodVisitor mv, Type type) {
   switch (type.getSort()) {
     case Type.INT:
       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
       break;
     case Type.LONG:
       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
       break;
     case Type.FLOAT:
       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
       break;
     case Type.DOUBLE:
       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
       break;
     case Type.SHORT:
       mv.visitInsn(I2S);
       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
       break;
     case Type.BYTE:
       mv.visitInsn(I2B);
       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
       break;
     case Type.BOOLEAN:
       mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
       break;
     case Type.CHAR:
       mv.visitMethodInsn(
           INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
       break;
   }
 }
Exemplo n.º 2
0
  /**
   * Fetches return value or thrown exception. If return value is of basic type, it is automatically
   * boxed.
   *
   * @param mv output method visitor
   * @param type return data type
   * @return number of JVM stack slots emitted code consumes
   */
  public int emitFetchRetVal(SpyMethodVisitor mv, Type type) {

    if (Type.VOID == type.getSort()) {
      mv.visitInsn(ACONST_NULL);
      return 1;
    }

    mv.visitInsn(DUP);
    emitAutoboxing(mv, type);
    mv.visitVarInsn(ASTORE, mv.getRetValProbeSlot());

    return type.getSize();
  }