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 /* 25 * @test 26 * @run testng TestLocation 27 */ 28 29 import org.testng.Assert; 30 import org.testng.annotations.Test; 31 32 import java.io.StringWriter; 33 import java.lang.reflect.Method; 34 import java.lang.reflect.code.Location; 35 import java.lang.reflect.code.Op; 36 import java.lang.reflect.code.OpTransformer; 37 import java.lang.reflect.code.op.CoreOp; 38 import java.lang.reflect.code.op.ExtendedOp; 39 import java.lang.reflect.code.parser.OpParser; 40 import java.lang.reflect.code.writer.OpWriter; 41 import java.lang.runtime.CodeReflection; 42 import java.util.Optional; 43 import java.util.stream.Stream; 44 45 public class TestLocation { 46 @Test 47 public void testLocation() { 48 CoreOp.FuncOp f = getFuncOp(ClassWithReflectedMethod.class, "f"); 49 f.traverse(null, (o, ce) -> { 50 if (ce instanceof CoreOp.ConstantOp cop) { 51 Location loc = cop.location(); 52 Assert.assertNotNull(loc); 53 54 int actualLine = loc.line(); 55 int expectedLine = Integer.parseInt((String) cop.value()); 56 Assert.assertEquals(actualLine, expectedLine); 57 } 58 return null; 59 }); 60 } 61 62 @CodeReflection 63 static int f(int m, int n) { 64 int sum = 0; 65 for (int i = 0; i < m; i++) { 66 for (int j = 0; j < n; j++) { 67 sum++; 68 } 69 } 70 return sum; 71 } 72 73 @Test 74 public void dropLocationTransform() { 75 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f"); 76 77 CoreOp.FuncOp tf = f.transform(OpTransformer.DROP_LOCATION_TRANSFORMER); 78 tf.setLocation(Location.NO_LOCATION); 79 testNoLocations(tf); 80 81 CoreOp.FuncOp tlf = lower(f).transform(OpTransformer.DROP_LOCATION_TRANSFORMER); 82 tlf.setLocation(Location.NO_LOCATION); 83 testNoLocations(tlf); 84 } 85 86 @Test 87 public void dropLocationWriter() { 88 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f"); 89 90 StringWriter w = new StringWriter(); 91 OpWriter.writeTo(w, f, OpWriter.LocationOption.DROP_LOCATION); 92 String tfText = w.toString(); 93 CoreOp.FuncOp tf = (CoreOp.FuncOp) OpParser.fromString(ExtendedOp.FACTORY, tfText).getFirst(); 94 testNoLocations(tf); 95 } 96 97 static CoreOp.FuncOp lower(CoreOp.FuncOp f) { 98 return f.transform(OpTransformer.LOWERING_TRANSFORMER); 99 } 100 101 static void testNoLocations(Op op) { 102 boolean noLocations = op.elements().filter(ce -> ce instanceof Op) 103 .allMatch(ce -> ((Op) ce).location() == Location.NO_LOCATION); 104 Assert.assertTrue(noLocations); 105 } 106 107 108 static CoreOp.FuncOp getFuncOp(Class<?> c, String name) { 109 Optional<Method> om = Stream.of(c.getDeclaredMethods()) 110 .filter(m -> m.getName().equals(name)) 111 .findFirst(); 112 113 Method m = om.get(); 114 return m.getCodeModel().get(); 115 } 116 }