1 /*
 2  * Copyright (c) 2025, 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 jdk.incubator.code.dialect.java.JavaOp.InvokeOp.InvokeKind;
25 import jdk.incubator.code.dialect.java.MethodRef;
26 import org.junit.jupiter.api.Test;
27 
28 import java.lang.invoke.MethodHandle;
29 import java.lang.invoke.MethodHandles;
30 import java.lang.invoke.MethodHandles.Lookup;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.Modifier;
33 
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertThrows;
36 
37 /*
38  * @test
39  * @modules jdk.incubator.code
40  * @run junit TestConstructorResolution
41  */
42 public class TestConstructorResolution {
43     public static class C {
44         C() { }
45         public C(int x) { }
46     }
47 
48     public static class C_Sub extends C {
49         private C_Sub(int x, int y, int z) { } // no default constructor
50     }
51 
52     @Test
53     public void testClassDeclaredFieldsPrivateLookup() throws ReflectiveOperationException {
54         lookupInternal(C.class, C.class, MethodHandles.lookup());
55     }
56 
57     @Test
58     public void testClassDeclaredFieldsPublicLookup() throws ReflectiveOperationException {
59         lookupInternal(C.class, C.class, publicLookup());
60     }
61 
62     @Test
63     public void testClassInheritedFieldsPrivateLookup() throws ReflectiveOperationException {
64         lookupInternal(C_Sub.class, C.class, MethodHandles.lookup());
65     }
66 
67     @Test
68     public void testClassInheritedFieldsPublicLookup() throws ReflectiveOperationException {
69         lookupInternal(C_Sub.class, C.class, publicLookup());
70     }
71 
72     static void lookupInternal(Class<?> refC, Class<?> cl, Lookup lookup) throws ReflectiveOperationException {
73         for (Constructor<?> c : cl.getDeclaredConstructors()) {
74             MethodRef constructorRef = MethodRef.constructor(refC, c.getParameterTypes());
75             if (refC.equals(cl) && (Modifier.isPublic(c.getModifiers()) || (lookup.lookupModes() & Lookup.ORIGINAL) != 0)) {
76                 Constructor<?> resolvedC = constructorRef.resolveToConstructor(lookup);
77                 assertEquals(c, resolvedC);
78                 MethodHandle resolvedMH = constructorRef.resolveToHandle(lookup, InvokeKind.SUPER);
79                 Constructor<?> targetC = lookup.revealDirect(resolvedMH).reflectAs(Constructor.class, lookup);
80                 assertEquals(targetC, c);
81             } else {
82                 assertThrows(ReflectiveOperationException.class, () -> constructorRef.resolveToConstructor(lookup));
83             }
84         }
85     }
86 
87     static Lookup publicLookup() {
88         return MethodHandles.publicLookup().in(TestConstructorResolution.class);
89     }
90 }