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 org.testng.Assert;
 25 import org.testng.annotations.Test;
 26 
 27 import java.lang.invoke.MethodHandles;
 28 import java.lang.reflect.Method;
 29 import java.lang.reflect.code.Op;
 30 import java.lang.reflect.code.OpTransformer;
 31 import java.lang.reflect.code.analysis.SSA;
 32 import java.lang.reflect.code.interpreter.Interpreter;
 33 import java.lang.reflect.code.op.CoreOp;
 34 import java.lang.reflect.code.op.ExtendedOp;
 35 import java.lang.reflect.code.type.CoreTypeFactory;
 36 import java.lang.reflect.code.writer.OpBuilder;
 37 import java.lang.runtime.CodeReflection;
 38 import java.util.Optional;
 39 import java.util.stream.Stream;
 40 
 41 /*
 42  * @test
 43  * @run testng TestCodeBuilder
 44  */
 45 
 46 public class TestCodeBuilder {
 47 
 48     @CodeReflection
 49     static void constants() {
 50         boolean bool = false;
 51         byte b = 1;
 52         char c = 'a';
 53         short s = 1;
 54         int i = 1;
 55         long l = 1L;
 56         float f = 1.0f;
 57         double d = 1.0;
 58         String str = "1";
 59         Object obj = null;
 60         Class<?> klass = Object.class;
 61     }
 62 
 63     @Test
 64     public void testConstants() {
 65         testWithTransforms(getFuncOp("constants"));
 66     }
 67 
 68     static record X(int f) {
 69         void m() {}
 70     }
 71 
 72     @CodeReflection
 73     static void reflect() {
 74         X x = new X(1);
 75         int i = x.f;
 76         x.m();
 77         X[] ax = new X[1];
 78         int l = ax.length;
 79         x = ax[0];
 80 
 81         Object o = x;
 82         x = (X) o;
 83         if (o instanceof X) {
 84             return;
 85         }
 86         if (o instanceof X(var a)) {
 87             return;
 88         }
 89     }
 90 
 91     @Test
 92     public void testReflect() {
 93         testWithTransforms(getFuncOp("reflect"));
 94     }
 95 
 96     @CodeReflection
 97     static int bodies(int m, int n) {
 98         int sum = 0;
 99         for (int i = 0; i < m; i++) {
100             for (int j = 0; j < n; j++) {
101                 sum += i + j;
102             }
103         }
104         return m > 10 ? sum : 0;
105     }
106 
107     @Test
108     public void testBodies() {
109         testWithTransforms(getFuncOp("bodies"));
110     }
111 
112     public void testWithTransforms(CoreOp.FuncOp f) {
113         test(f);
114 
115         f = f.transform(OpTransformer.LOWERING_TRANSFORMER);
116         test(f);
117 
118         f = SSA.transform(f);
119         test(f);
120     }
121 
122     static void test(CoreOp.FuncOp fExpected) {
123         CoreOp.FuncOp fb = OpBuilder.createBuilderFunction(fExpected);
124         CoreOp.FuncOp fActual = (CoreOp.FuncOp) Interpreter.invoke(MethodHandles.lookup(),
125                 fb, ExtendedOp.FACTORY, CoreTypeFactory.CORE_TYPE_FACTORY);
126         Assert.assertEquals(fActual.toText(), fExpected.toText());
127     }
128 
129     static CoreOp.FuncOp getFuncOp(String name) {
130         Optional<Method> om = Stream.of(TestCodeBuilder.class.getDeclaredMethods())
131                 .filter(m -> m.getName().equals(name))
132                 .findFirst();
133 
134         Method m = om.get();
135         return m.getCodeModel().get();
136     }
137 }