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 java.lang.reflect.code.type;
 27 
 28 import java.lang.constant.ClassDesc;
 29 import java.lang.invoke.MethodHandles.Lookup;
 30 import java.lang.reflect.Type;
 31 import java.util.List;
 32 import java.util.Objects;
 33 
 34 /**
 35  * A wildcard type.
 36  */
 37 public final class WildcardType implements JavaType {
 38 
 39     final BoundKind kind;
 40     final JavaType boundType;
 41 
 42     WildcardType(BoundKind kind, JavaType boundType) {
 43         this.kind = kind;
 44         this.boundType = boundType;
 45     }
 46 
 47     @Override
 48     public Type resolve(Lookup lookup) throws ReflectiveOperationException {
 49         Type[] upperBounds = kind == BoundKind.EXTENDS ?
 50                 new Type[] { boundType.resolve(lookup) } : new Type[] { Object.class };
 51         Type[] lowerBounds = kind == BoundKind.SUPER ?
 52                 new Type[] { boundType.resolve(lookup) } : new Type[0];
 53         return makeReflectiveWildcard(upperBounds, lowerBounds);
 54     }
 55 
 56     // Copied code in jdk.compiler module throws UOE
 57     private static java.lang.reflect.WildcardType makeReflectiveWildcard(Type[] upper, Type[] lower) {
 58 /*__throw new UnsupportedOperationException();__*/        return sun.reflect.generics.reflectiveObjects.WildcardTypeImpl.make(upper, lower);
 59     }
 60 
 61     /**
 62      * {@return the wildcard type's bound type}
 63      */
 64     public JavaType boundType() {
 65         return boundType;
 66     }
 67 
 68     /**
 69      * {@return the wildcard type's bound kind}
 70      */
 71     public BoundKind boundKind() {
 72         return kind;
 73     }
 74 
 75     @Override
 76     public ExternalizedTypeElement externalize() {
 77         String prefix = kind == BoundKind.EXTENDS ? "+" : "-";
 78         return new ExternalizedTypeElement(prefix, List.of(boundType.externalize()));
 79     }
 80 
 81     @Override
 82     public String toString() {
 83         return externalize().toString();
 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,
120         /** A bound kind representing a {@code ? super} wildcard type*/
121         SUPER
122     }
123 }