1 /*
 2  * Copyright (c) 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.PrintWriter;
25 import java.io.StringWriter;
26 import java.lang.invoke.MethodHandle;
27 import java.lang.invoke.MethodHandles;
28 import java.lang.reflect.Method;
29 import jdk.incubator.code.Reflect;
30 import jdk.incubator.code.Op;
31 import jdk.incubator.code.bytecode.BytecodeGenerator;
32 import jdk.incubator.code.dialect.core.CoreOp;
33 import jdk.incubator.code.dialect.core.CoreType;
34 import jdk.incubator.code.dialect.java.JavaOp;
35 import jdk.incubator.code.dialect.java.JavaType;
36 import jdk.incubator.code.dialect.java.MethodRef;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39 
40 /*
41  * @test
42  * @modules jdk.incubator.code
43  * @enablePreview
44  * @run junit TestProtectedAccess
45  */
46 public class TestProtectedAccess extends PrintWriter {
47 
48     public TestProtectedAccess() {
49         super(new StringWriter());
50         print("hello");
51     }
52 
53     @Reflect
54     public String accessProtectedField() {
55         return out.toString();
56     }
57 
58     @Reflect
59     public boolean accessProtectedMethod() {
60         setError();
61         return checkError();
62     }
63 
64     @Test
65     public void testProtectedFieldAccess() throws Throwable {
66         Method accessMethod = TestProtectedAccess.class.getDeclaredMethod("accessProtectedField");
67         MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(),
68                                                          Op.ofMethod(accessMethod).orElseThrow());
69         Assertions.assertEquals(new TestProtectedAccess().accessProtectedField(),
70                                 handle.invoke(new TestProtectedAccess()));
71     }
72 
73     @Test
74     public void testProtectedMethodAccess() throws Throwable {
75         Method accessMethod = TestProtectedAccess.class.getDeclaredMethod("accessProtectedMethod");
76         MethodHandle handle = BytecodeGenerator.generate(MethodHandles.lookup(),
77                                                          Op.ofMethod(accessMethod).orElseThrow());
78         Assertions.assertEquals(new TestProtectedAccess().accessProtectedMethod(),
79                                 (boolean) handle.invoke(new TestProtectedAccess()));
80     }
81 
82     @Test
83     public void testArrayCloneAccess() throws Throwable {
84         var handle = BytecodeGenerator.generate(MethodHandles.lookup(), CoreOp.func("arrayCloneTest",
85                 CoreType.functionType(JavaType.J_L_OBJECT, JavaType.array(JavaType.J_L_STRING))).body(bb ->
86                         bb.add(CoreOp.return_(
87                                 bb.add(JavaOp.invoke(
88                                         MethodRef.method(JavaType.array(JavaType.J_L_STRING),
89                                                          "clone",
90                                                          JavaType.J_L_OBJECT),
91                                         bb.parameters().getFirst()))))));
92         String[] input = { "a", "b" };
93         Assertions.assertArrayEquals(input, (String[])handle.invoke(input));
94     }
95 }