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