Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefineSubtractLambdaExpression.java
41155 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 819352426* @summary Redefine a class' public static method that contains a lambda expression27* @requires vm.jvmti28* @library /test/lib29* @modules java.base/jdk.internal.misc30* @modules java.compiler31* java.instrument32* jdk.jartool/sun.tools.jar33* @run main RedefineClassHelper34* @run main/othervm -javaagent:redefineagent.jar -XX:+AllowRedefinitionToAddDeleteMethods -Xlog:redefine+class*=trace RedefineSubtractLambdaExpression35*/3637interface MathOperation {38public int operation(int a, int b);39}4041class B {42public static int operate(int a, int b, MathOperation mathOperation) {43return mathOperation.operation(a, b);44}45static int test_math(String p) {46System.out.println(p + " from class B's test_math method");47MathOperation subtraction = (int a, int b) -> a - b;48MathOperation addition = (int a, int b) -> a + b;49return operate(10, 5, addition);50}51}5253public class RedefineSubtractLambdaExpression {5455public static String newB =56"class B {" +57" public static int operate(int a, int b, MathOperation mathOperation) {" +58" return mathOperation.operation(a, b);" +59" }" +60" static int test_math(String p) {" +61" MathOperation subtraction = (int a, int b) -> a - b;" +62" return operate(10, 5, subtraction);" +63" }" +64"}";6566public static void main(String[] args) throws Exception {67int res = B.test_math("Hello");68System.out.println("Result = " + res);69if (res != 15) {70throw new Error("test_math returned " + res + " expected " + 15);71}72RedefineClassHelper.redefineClass(B.class, newB);7374res = B.test_math("Hello");75if (res != 5)76throw new Error("test_math returned " + res + " expected " + 5);77}78}798081