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 import java.io.StringWriter;
25 import java.lang.reflect.Member;
26 import java.lang.reflect.Method;
27
28 import jdk.incubator.code.CodeTransformer;
29 import jdk.incubator.code.Op;
30 import jdk.incubator.code.dialect.core.SSA;
31 import jdk.incubator.code.dialect.core.CoreOp;
32 import jdk.incubator.code.dialect.java.JavaOp;
33 import jdk.incubator.code.extern.OpParser;
34 import jdk.incubator.code.extern.OpWriter;
35 import jdk.incubator.code.Reflect;
36
37 import static jdk.incubator.code.dialect.core.CoreOp.func;
38
39 public class CodeReflectionTester {
40
41 public static void main(String[] args) throws ReflectiveOperationException {
42 if (args.length != 1) {
43 System.err.println("Usage: ReflectTester <classname>");
44 System.exit(1);
45 }
46 Class<?> clazz = Class.forName(args[0]);
47 for (Method m : clazz.getDeclaredMethods()) {
48 check(m);
49 }
50 }
51
52 static void check(Method method) throws ReflectiveOperationException {
53 if (!method.isAnnotationPresent(Reflect.class)) {
54 return;
55 }
56
57 LoweredModel lma = method.getAnnotation(LoweredModel.class);
58 if (lma == null) {
59 throw new AssertionError("No @IR annotation found on reflective method");
60 }
61
62 CoreOp.FuncOp f = Op.ofMethod(method).orElseThrow(() ->
63 new AssertionError("No code model for reflective method"));
64 f = lower(f, lma.ssa());
65
66 // we remove the source attribute, to avoid updating the tests, which we will do later
67 String actual = canonicalizeModel(method, removeSourceAttribute(f));
68 String expected = canonicalizeModel(method, lma.value());
69 if (!actual.equals(expected)) {
70 throw new AssertionError(String.format("Bad code model\nFound:\n%s\n\nExpected:\n%s", actual, expected));
71 }
72 }
73
74 static CoreOp.FuncOp lower(CoreOp.FuncOp f, boolean ssa) {
75 f = f.transform(CodeTransformer.LOWERING_TRANSFORMER);
76 System.out.println(f.toText());
77
78 if (ssa) {
79 f = SSA.transform(f);
80 System.out.println(f.toText());
81 }
82
83 return f;
84 }
85
86 // serializes dropping location information, parses, and then serializes, dropping location information
87 static String canonicalizeModel(Member m, Op o) {
88 return canonicalizeModel(m, serialize(o));
89 }
90
91 // parses, and then serializes, dropping location information
92 static String canonicalizeModel(Member m, String d) {
93 Op o;
94 try {
95 o = OpParser.fromText(JavaOp.JAVA_DIALECT_FACTORY, d).get(0);
96 } catch (Exception e) {
97 throw new IllegalStateException(m.toString(), e);
98 }
99 return serialize(o);
100 }
101
102 // serializes, dropping location information
103 static String serialize(Op o) {
104 StringWriter w = new StringWriter();
105 OpWriter.writeTo(w, o, OpWriter.LocationOption.DROP_LOCATION);
106 return w.toString();
107 }
108
109 static CoreOp.FuncOp removeSourceAttribute(CoreOp.FuncOp fop) {
110 return func(fop.funcName(), fop.invokableSignature()).body(b -> {
111 b.transformBody(fop.body(), b.parameters(), CodeTransformer.COPYING_TRANSFORMER);
112 });
113 }
114 }