/*
 * @test
 * @summary Redefine a class' public static method that contains a lambda expression
 * @library /test/lib
 * @modules java.base/jdk.internal.misc
 * @modules java.compiler
 *          java.instrument
 *          jdk.jartool/sun.tools.jar
 * @run main RedefineClassHelper
 * @run main/othervm -javaagent:redefineagent.jar -Xlog:redefine+class*=trace RedefineAddLambdaExpression
 */

interface MathOperation {
    public int operation(int a, int b);
}

class B {
    public static int operate(int a, int b, MathOperation mathOperation) {
        return mathOperation.operation(a, b);
    }
    static int test_math(String p) {
        MathOperation addition = (int a, int b) -> a + b;
        return operate(10, 5, addition);
    }
}

public class RedefineAddLambdaExpression {

    public static String newB =
        "class B {" +
        "    public static int operate(int a, int b, MathOperation mathOperation) {" +
        "        return mathOperation.operation(a, b);" +
        "    }" +
        "    static int test_math(String p) {" +
        "        MathOperation addition = (int a, int b) -> a + b;" +
        "        System.out.println(p + \" from class B's test_math method\");" +
        "        MathOperation subtraction = (int a, int b) -> a - b;" +
        "        return operate(10, 5, subtraction);" +
        "    }" +
        "}";

    public static void main(String[] args) throws Exception {
        int res = B.test_math("Hello");
        System.out.println("Result = " + res);
        if (res != 15) {
            throw new Error("test_math returned " + res + " expected " + 15);
        }
        RedefineClassHelper.redefineClass(B.class, newB);

        res = B.test_math("Hello");
        if (res != 5)
            throw new Error("test_math returned " + res + " expected " + 5);
    }
}
