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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.incubator.code.dialect.java;
27
28 import jdk.incubator.code.TypeElement;
29 import jdk.incubator.code.dialect.java.impl.ConstructorRefImpl;
30 import jdk.incubator.code.dialect.core.FunctionType;
31
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandles;
34 import java.lang.invoke.MethodType;
35 import java.lang.reflect.Constructor;
36 import java.util.List;
37
38 import static jdk.incubator.code.dialect.core.CoreType.functionType;
39
40 /**
41 * The symbolic reference to a Java constructor.
42 */
43 public sealed interface ConstructorRef extends JavaRef, TypeVariableType.Owner
44 permits ConstructorRefImpl {
45
46 FunctionType type();
47
48 default TypeElement refType() {
49 return type().returnType();
50 }
51
52 // Resolutions to constructors and method handles
53
54 // Resolve to constructor on referenced class
55 Constructor<?> resolveToConstructor(MethodHandles.Lookup l) throws ReflectiveOperationException;
56
57 // Resolve to constructor on referenced class
58 MethodHandle resolveToHandle(MethodHandles.Lookup l) throws ReflectiveOperationException;
59
60 // Factories
61
62 static ConstructorRef constructor(Constructor<?> c) {
63 return constructor(c.getDeclaringClass(),
64 c.getParameterTypes());
65 }
66
67 static ConstructorRef constructor(MethodType mt) {
68 return constructor(mt.returnType(), mt.parameterList());
69 }
70
71 static ConstructorRef constructor(Class<?> refType, Class<?>... params) {
72 return constructor(refType, List.of(params));
73 }
74
75 static ConstructorRef constructor(Class<?> refType, List<Class<?>> params) {
76 return constructor(JavaType.type(refType), params.stream().map(JavaType::type).toList());
77 }
78
79 static ConstructorRef constructor(TypeElement refType, List<? extends TypeElement> params) {
80 return constructor(functionType(refType, params));
81 }
82
83 static ConstructorRef constructor(TypeElement refType, TypeElement... params) {
84 return constructor(functionType(refType, params));
85 }
86
87 static ConstructorRef constructor(FunctionType type) {
88 return new ConstructorRefImpl(type);
89 }
90 }