1 /*
2 * Copyright (c) 2024, 2026, 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 void testLocationIsRelative() {
48 CoreOp.FuncOp f = getFuncOp(this.getClass(), "f");
49 Assertions.assertEquals("TestLocation.java", f.location().sourceRef());
50 }
51
52 @Test
53 public void testLocation() {
54 CoreOp.FuncOp f = getFuncOp(ClassWithReflectedMethod.class, "f");
55 f.elements().forEach(ce -> {
56 if (ce instanceof CoreOp.ConstantOp cop) {
57 Op.Location loc = cop.location();
58 Assertions.assertNotNull(loc);
59
60 int actualLine = loc.line();
61 int expectedLine = Integer.parseInt((String) cop.value());
62 Assertions.assertEquals(expectedLine, actualLine);
63 }
64 });
65 }
66
67 @Reflect
68 static int f(int m, int n) {
69 int sum = 0;
70 for (int i = 0; i < m; i++) {
71 for (int j = 0; j < n; j++) {
72 sum++;
73 }
74 }
75 return sum;
76 }
77
78 @Test
79 public void dropLocationTransform() {
80 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f");
81
82 CoreOp.FuncOp tf = f.transform(CodeTransformer.DROP_LOCATION_TRANSFORMER);
83 tf.setLocation(Op.Location.NO_LOCATION);
84 testNoLocations(tf);
85
86 CoreOp.FuncOp tlf = lower(f).transform(CodeTransformer.DROP_LOCATION_TRANSFORMER);
87 tlf.setLocation(Op.Location.NO_LOCATION);
88 testNoLocations(tlf);
89 }
90
91 @Test
92 public void dropLocationWriter() {
93 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f");
94
95 StringWriter w = new StringWriter();
96 OpWriter.writeTo(w, f, OpWriter.LocationOption.DROP_LOCATION);
97 String tfText = w.toString();
98 CoreOp.FuncOp tf = (CoreOp.FuncOp) OpParser.fromText(JavaOp.JAVA_DIALECT_FACTORY, tfText).getFirst();
99 testNoLocations(tf);
100 }
101
102 static CoreOp.FuncOp lower(CoreOp.FuncOp f) {
103 return f.transform(CodeTransformer.LOWERING_TRANSFORMER);
104 }
105
106 static void testNoLocations(Op op) {
107 boolean noLocations = op.elements().filter(ce -> ce instanceof Op)
108 .allMatch(ce -> ((Op) ce).location() == Op.Location.NO_LOCATION);
109 Assertions.assertTrue(noLocations);
110 }
111
112
113 static CoreOp.FuncOp getFuncOp(Class<?> c, String name) {
114 Optional<Method> om = Stream.of(c.getDeclaredMethods())
115 .filter(m -> m.getName().equals(name))
116 .findFirst();
117
118 Method m = om.get();
119 return Op.ofMethod(m).get();
120 }
121 }