private String getReplicaMethodDelegationCode(Class<?> clazz, CtMethod ctMethod, String classOrInstanceToDelegateTo)
            throws NotFoundException {
        StringBuilder builder = new StringBuilder();
        builder.append("{java.lang.reflect.Method originalMethod = ");
        builder.append(clazz.getName());
        builder.append(".class.getDeclaredMethod(\"");
        builder.append(ctMethod.getName());
        builder.append("\", ");
        final String parametersAsString = getParametersAsString(getParameterTypes(ctMethod));
        if ("".equals(parametersAsString)) {
            builder.append("null");
        } else {
            builder.append(parametersAsString);
        }
        builder.append(");\n");
        builder.append("originalMethod.setAccessible(true);\n");
        final CtClass returnType = ctMethod.getReturnType();
        final boolean isVoid = returnType.equals(CtClass.voidType);
        if (!isVoid) {
            builder.append("return (");
            builder.append(returnType.getName());
            builder.append(") ");
        }
        builder.append("originalMethod.invoke(");
        if (Modifier.isStatic(ctMethod.getModifiers()) || classOrInstanceToDelegateTo == null) {
            builder.append(clazz.getName());
            builder.append(".class");
        } else {
            builder.append(classOrInstanceToDelegateTo);
        }
        builder.append(", $args);}");
        return builder.toString();
    }
private String getReplicaMethodDelegationCode ( Class < ? > clazz , CtMethod ctMethod , String classOrInstanceToDelegateTo ) throws NotFoundException { StringBuilder builder = new StringBuilder ( ) ; builder . append ( "{java.lang.reflect.Method originalMethod = " ) ; builder . append ( clazz . getName ( ) ) ; builder . append ( ".class.getDeclaredMethod(\"" ) ; builder . append ( ctMethod . getName ( ) ) ; builder . append ( "\", " ) ; final String parametersAsString = getParametersAsString ( getParameterTypes ( ctMethod ) ) ; if ( "" . equals ( parametersAsString ) ) { builder . append ( "null" ) ; } else { builder . append ( parametersAsString ) ; } builder . append ( ");\n" ) ; builder . append ( "originalMethod.setAccessible(true);\n" ) ; final CtClass returnType = ctMethod . getReturnType ( ) ; final boolean isVoid = returnType . equals ( CtClass . voidType ) ; if ( ! isVoid ) { builder . append ( "return (" ) ; builder . append ( returnType . getName ( ) ) ; builder . append ( ") " ) ; } builder . append ( "originalMethod.invoke(" ) ; if ( Modifier . isStatic ( ctMethod . getModifiers ( ) ) || classOrInstanceToDelegateTo == null ) { builder . append ( clazz . getName ( ) ) ; builder . append ( ".class" ) ; } else { builder . append ( classOrInstanceToDelegateTo ) ; } builder . append ( ", $args);}" ) ; return builder . toString ( ) ; }
/*
Invokes a instance method of the original instance. This enables partial
mocking of system classes.