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 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.analysis.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 public class CodeReflectionTester {
 38 
 39     public static void main(String[] args) throws ReflectiveOperationException {
 40         if (args.length != 1) {
 41             System.err.println("Usage: ReflectTester <classname>");
 42             System.exit(1);
 43         }
 44         Class<?> clazz = Class.forName(args[0]);
 45         for (Method m : clazz.getDeclaredMethods()) {
 46             check(m);
 47         }
 48     }
 49 
 50     static void check(Method method) throws ReflectiveOperationException {
 51         if (!method.isAnnotationPresent(Reflect.class)) {
 52             return;
 53         }
 54 
 55         LoweredModel lma = method.getAnnotation(LoweredModel.class);
 56         if (lma == null) {
 57             throw new AssertionError("No @IR annotation found on reflective method");
 58         }
 59 
 60         CoreOp.FuncOp f = Op.ofMethod(method).orElseThrow(() ->
 61                 new AssertionError("No code model for reflective method"));
 62         f = lower(f, lma.ssa());
 63 
 64         String actual = canonicalizeModel(method, f);
 65         String expected = canonicalizeModel(method, lma.value());
 66         if (!actual.equals(expected)) {
 67             throw new AssertionError(String.format("Bad code model\nFound:\n%s\n\nExpected:\n%s", actual, expected));
 68         }
 69     }
 70 
 71     static CoreOp.FuncOp lower(CoreOp.FuncOp f, boolean ssa) {
 72         f = f.transform(CodeTransformer.LOWERING_TRANSFORMER);
 73         System.out.println(f.toText());
 74 
 75         if (ssa) {
 76             f = SSA.transform(f);
 77             System.out.println(f.toText());
 78         }
 79 
 80         return f;
 81     }
 82 
 83     // serializes dropping location information, parses, and then serializes, dropping location information
 84     static String canonicalizeModel(Member m, Op o) {
 85         return canonicalizeModel(m, serialize(o));
 86     }
 87 
 88     // parses, and then serializes, dropping location information
 89     static String canonicalizeModel(Member m, String d) {
 90         Op o;
 91         try {
 92             o = OpParser.fromString(JavaOp.JAVA_DIALECT_FACTORY, d).get(0);
 93         } catch (Exception e) {
 94             throw new IllegalStateException(m.toString(), e);
 95         }
 96         return serialize(o);
 97     }
 98 
 99     // serializes, dropping location information
100     static String serialize(Op o) {
101         StringWriter w = new StringWriter();
102         OpWriter.writeTo(w, o, OpWriter.LocationOption.DROP_LOCATION);
103         return w.toString();
104     }
105 }