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.dialect.java.impl.JavaTypeUtils;
29 import jdk.incubator.code.extern.ExternalizedTypeElement;
30
31 import java.lang.constant.ClassDesc;
32 import java.lang.invoke.MethodHandles.Lookup;
33 import java.lang.reflect.Type;
34 import java.util.Objects;
35
36 /**
37 * A wildcard type.
38 */
39 public final class WildcardType implements JavaType {
40
41 final BoundKind kind;
42 final JavaType boundType;
43
44 WildcardType(BoundKind kind, JavaType boundType) {
45 this.kind = kind;
46 this.boundType = boundType;
47 }
48
49 @Override
50 public Type resolve(Lookup lookup) throws ReflectiveOperationException {
51 Type[] upperBounds = kind == BoundKind.EXTENDS ?
52 new Type[] { boundType.resolve(lookup) } : new Type[] { Object.class };
53 Type[] lowerBounds = kind == BoundKind.SUPER ?
54 new Type[] { boundType.resolve(lookup) } : new Type[0];
55 return makeReflectiveWildcard(upperBounds, lowerBounds);
56 }
57
58 private static java.lang.reflect.WildcardType makeReflectiveWildcard(Type[] upper, Type[] lower) {
59 return sun.reflect.generics.reflectiveObjects.WildcardTypeImpl.make(upper, lower);
60 }
61
62 /**
63 * {@return the wildcard type's bound type}
64 */
65 public JavaType boundType() {
66 return boundType;
67 }
68
69 /**
70 * {@return the wildcard type's bound kind}
71 */
72 public BoundKind boundKind() {
73 return kind;
74 }
75
76 @Override
77 public ExternalizedTypeElement externalize() {
78 return JavaTypeUtils.wildcardType(boundKind(), boundType.externalize());
79 }
80
81 @Override
82 public String toString() {
83 return JavaTypeUtils.toExternalTypeString(externalize());
84 }
85
86 @Override
87 public boolean equals(Object o) {
88 if (this == o) return true;
89 return o instanceof WildcardType that &&
90 kind.equals(that.kind) &&
91 boundType.equals(that.boundType);
92 }
93
94 @Override
95 public int hashCode() {
96 return Objects.hash(boundType, kind);
97 }
98
99 @Override
100 public JavaType erasure() {
101 throw new UnsupportedOperationException("Wildcard type");
102 }
103
104 @Override
105 public JavaType toBasicType() {
106 throw new UnsupportedOperationException("Wildcard type");
107 }
108
109 @Override
110 public ClassDesc toNominalDescriptor() {
111 throw new UnsupportedOperationException("Wildcard type");
112 }
113
114 /**
115 * The bound kind of a wildcard type.
116 */
117 public enum BoundKind {
118 /** A bound kind representing a {@code ? extends} wildcard type*/
119 EXTENDS("? extends "),
120 /** A bound kind representing a {@code ? super} wildcard type*/
121 SUPER("? super ");
122
123 final String boundStr;
124
125 BoundKind(String boundStr) {
126 this.boundStr = boundStr;
127 }
128 }
129 }