Ejemplo n.º 1
0
  public void insertConstructorCall(
      MethodCallReplacementMethodAdapter mv, MethodCallReplacement replacement, boolean isSelf) {
    Label origCallLabel = new Label();
    Label afterOrigCallLabel = new Label();

    if (!isSelf) {
      mv.visitMethodInsn(
          Opcodes.INVOKESTATIC,
          MockFramework.class.getCanonicalName().replace('.', '/'),
          "isEnabled",
          "()Z",
          false);
      Label annotationStartTag = new AnnotatedLabel(true, true);
      annotationStartTag.info = Boolean.TRUE;
      mv.visitLabel(annotationStartTag);
      mv.visitJumpInsn(Opcodes.IFEQ, origCallLabel);
      Label annotationEndTag = new AnnotatedLabel(true, false);
      annotationEndTag.info = Boolean.FALSE;
      mv.visitLabel(annotationEndTag);

      Type[] args = Type.getArgumentTypes(desc);
      Map<Integer, Integer> to = new HashMap<Integer, Integer>();
      for (int i = args.length - 1; i >= 0; i--) {
        int loc = mv.newLocal(args[i]);
        mv.storeLocal(loc);
        to.put(i, loc);
      }

      mv.pop2(); // uninitialized reference (which is duplicated)
      mv.newInstance(Type.getType(replacement.replacementClassName));
      mv.dup();

      for (int i = 0; i < args.length; i++) {
        mv.loadLocal(to.get(i));
      }
    }
    mv.visitMethodInsn(
        Opcodes.INVOKESPECIAL, replacementClassName, replacementMethodName, replacementDesc, false);
    if (!isSelf) {
      mv.visitJumpInsn(Opcodes.GOTO, afterOrigCallLabel);
      mv.visitLabel(origCallLabel);
      mv.getNextVisitor()
          .visitMethodInsn(Opcodes.INVOKESPECIAL, className, methodName, desc, false);
      mv.visitLabel(afterOrigCallLabel);
    }
  }
Ejemplo n.º 2
0
  public void insertMethodCall(MethodCallReplacementMethodAdapter mv, int opcode) {
    mv.visitMethodInsn(
        Opcodes.INVOKESTATIC,
        MockFramework.class.getCanonicalName().replace('.', '/'),
        "isEnabled",
        "()Z",
        false);
    Label origCallLabel = new Label();
    Label afterOrigCallLabel = new Label();

    Label annotationStartTag = new AnnotatedLabel(true, true);
    annotationStartTag.info = Boolean.TRUE;
    mv.visitLabel(annotationStartTag);
    mv.visitJumpInsn(Opcodes.IFEQ, origCallLabel);
    Label annotationEndTag = new AnnotatedLabel(true, false);
    annotationEndTag.info = Boolean.FALSE;
    mv.visitLabel(annotationEndTag);

    if (popCallee) {
      Type[] args = Type.getArgumentTypes(desc);
      Map<Integer, Integer> to = new HashMap<Integer, Integer>();
      for (int i = args.length - 1; i >= 0; i--) {
        int loc = mv.newLocal(args[i]);
        mv.storeLocal(loc);
        to.put(i, loc);
      }

      mv.pop(); // callee
      if (popUninitialisedReference) mv.pop();

      for (int i = 0; i < args.length; i++) {
        mv.loadLocal(to.get(i));
      }
    }
    if (opcode == Opcodes.INVOKESPECIAL && MockList.shouldBeMocked(className.replace('/', '.'))) {
      insertInvokeSpecialForMockedSuperclass(mv);
    } else {
      if (opcode == Opcodes.INVOKESPECIAL) {
        logger.info(
            "Not mocking invokespecial: " + replacementMethodName + " for class " + className);
      }
      mv.visitMethodInsn(
          opcode, replacementClassName, replacementMethodName, replacementDesc, false);
    }
    mv.visitJumpInsn(Opcodes.GOTO, afterOrigCallLabel);
    mv.visitLabel(origCallLabel);
    mv.getNextVisitor()
        .visitMethodInsn(origOpcode, className, methodName, desc, false); // TODO: What is itf here?
    mv.visitLabel(afterOrigCallLabel);
  }
Ejemplo n.º 3
0
  public void insertInvokeSpecialForMockedSuperclass(MethodCallReplacementMethodAdapter mv) {
    int numArguments = Type.getArgumentTypes(replacementDesc).length;
    mv.push(numArguments);
    mv.newArray(Type.getType(Object.class));
    for (int i = 0; i < numArguments; i++) {
      // param, array
      mv.dupX1(); // array, param, array
      mv.swap(); // array, array, param
      mv.push(numArguments - i - 1); // array, array, param, index
      mv.swap(); // array, array, index, param
      mv.arrayStore(Type.getType(Object.class));
      // array
    }
    mv.push(methodName);
    mv.push(desc);
    Method invokeSpecialMethod = InvokeSpecialMock.class.getDeclaredMethods()[0];

    mv.visitMethodInsn(
        Opcodes.INVOKESTATIC,
        InvokeSpecialMock.class.getCanonicalName().replace('.', '/'),
        "invokeSpecial",
        Type.getMethodDescriptor(invokeSpecialMethod),
        false);

    if (Type.getReturnType(desc).equals(Type.VOID_TYPE)) {
      mv.pop();
    } else {
      mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getReturnType(desc).getInternalName());
    }
  }