1 /* 2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import org.testng.Assert; 25 import org.testng.annotations.Test; 26 27 import java.lang.reflect.code.OpTransformer; 28 import java.lang.reflect.code.op.CoreOp; 29 import java.lang.reflect.code.Op; 30 import java.lang.reflect.code.interpreter.Interpreter; 31 import java.lang.invoke.MethodHandles; 32 import java.lang.reflect.Method; 33 import java.lang.runtime.CodeReflection; 34 import java.util.ArrayList; 35 import java.util.List; 36 import java.util.Optional; 37 import java.util.stream.Stream; 38 39 /* 40 * @test 41 * @run testng TestConditionalOp 42 */ 43 44 public class TestConditionalOp { 45 46 @CodeReflection 47 static boolean f(boolean a, boolean b, boolean c, List<String> l) { 48 return F.a(a, l) || (F.b(b, l) && F.c(c, l)); 49 } 50 51 static class F { 52 static boolean a(boolean a, List<String> l) { 53 l.add("a"); 54 return a; 55 } 56 57 static boolean b(boolean b, List<String> l) { 58 l.add("b"); 59 return b; 60 } 61 62 static boolean c(boolean c, List<String> l) { 63 l.add("c"); 64 return c; 65 } 66 } 67 68 static CoreOp.FuncOp getFuncOp(String name) { 69 Optional<Method> om = Stream.of(TestConditionalOp.class.getDeclaredMethods()) 70 .filter(m -> m.getName().equals(name)) 71 .findFirst(); 72 73 Method m = om.get(); 74 return m.getCodeModel().get(); 75 } 76 77 @Test 78 public void testf() { 79 CoreOp.FuncOp f = getFuncOp("f"); 80 81 f.writeTo(System.out); 82 83 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 84 85 lf.writeTo(System.out); 86 87 for (int i = 0; i < 8; i++) { 88 boolean a = (i & 1) != 0; 89 boolean b = (i & 2) != 0; 90 boolean c = (i & 4) != 0; 91 List<String> la = new ArrayList<>(); 92 boolean ra = (boolean) Interpreter.invoke(MethodHandles.lookup(), lf, a, b, c, la); 93 94 List<String> le = new ArrayList<>(); 95 boolean re = f(a, b, c, le); 96 97 Assert.assertEquals(ra, re); 98 Assert.assertEquals(la, le); 99 } 100 } 101 }