Showing the changes to java/pkg/com/sun/beans/WildcardTypeImpl.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,161 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2003, 2006, 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 | package com.sun.beans; | ||
| 26 | |||
| 27 | import java.lang.reflect.Type; | ||
| 28 | import java.lang.reflect.WildcardType; | ||
| 29 | import java.util.Arrays; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * This class implements {@link WildcardType WildcardType} compatibly with the JDK's | ||
| 33 | * {@link sun.reflect.generics.reflectiveObjects.WildcardTypeImpl WildcardTypeImpl}. | ||
| 34 | * Unfortunately we can't use the JDK's | ||
| 35 | * {@link sun.reflect.generics.reflectiveObjects.WildcardTypeImpl WildcardTypeImpl} here as we do for | ||
| 36 | * {@link sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl ParameterizedTypeImpl} and | ||
| 37 | * {@link sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl GenericArrayTypeImpl}, | ||
| 38 | * because {@link sun.reflect.generics.reflectiveObjects.WildcardTypeImpl WildcardTypeImpl}'s | ||
| 39 | * constructor takes parameters representing intermediate structures obtained during class-file parsing. | ||
| 40 | * We could reconstruct versions of those structures but it would be more trouble than it's worth. | ||
| 41 | * | ||
| 42 | * @since 1.7 | ||
| 43 | * | ||
| 44 | * @author Eamonn McManus | ||
| 45 | * @author Sergey Malenkov | ||
| 46 | */ | ||
| 47 | final class WildcardTypeImpl implements WildcardType { | ||
| 48 | private final Type[] upperBounds; | ||
| 49 | private final Type[] lowerBounds; | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Creates a wildcard type with the requested bounds. | ||
| 53 | * Note that the array arguments are not cloned | ||
| 54 | * because instances of this class are never constructed | ||
| 55 | * from outside the containing package. | ||
| 56 | * | ||
| 57 | * @param upperBounds the array of types representing | ||
| 58 | * the upper bound(s) of this type variable | ||
| 59 | * @param lowerBounds the array of types representing | ||
| 60 | * the lower bound(s) of this type variable | ||
| 61 | */ | ||
| 62 | WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) { | ||
| 63 | this.upperBounds = upperBounds; | ||
| 64 | this.lowerBounds = lowerBounds; | ||
| 65 | } | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Returns an array of {@link Type Type} objects | ||
| 69 | * representing the upper bound(s) of this type variable. | ||
| 70 | * Note that if no upper bound is explicitly declared, | ||
| 71 | * the upper bound is {@link Object Object}. | ||
| 72 | * | ||
| 73 | * @return an array of types representing | ||
| 74 | * the upper bound(s) of this type variable | ||
| 75 | */ | ||
| 76 | public Type[] getUpperBounds() { | ||
| 77 | return this.upperBounds.clone(); | ||
| 78 | } | ||
| 79 | |||
| 80 | /** | ||
| 81 | * Returns an array of {@link Type Type} objects | ||
| 82 | * representing the lower bound(s) of this type variable. | ||
| 83 | * Note that if no lower bound is explicitly declared, | ||
| 84 | * the lower bound is the type of {@code null}. | ||
| 85 | * In this case, a zero length array is returned. | ||
| 86 | * | ||
| 87 | * @return an array of types representing | ||
| 88 | * the lower bound(s) of this type variable | ||
| 89 | */ | ||
| 90 | public Type[] getLowerBounds() { | ||
| 91 | return this.lowerBounds.clone(); | ||
| 92 | } | ||
| 93 | |||
| 94 | /** | ||
| 95 | * Indicates whether some other object is "equal to" this one. | ||
| 96 | * It is implemented compatibly with the JDK's | ||
| 97 | * {@link sun.reflect.generics.reflectiveObjects.WildcardTypeImpl WildcardTypeImpl}. | ||
| 98 | * | ||
| 99 | * @param object the reference object with which to compare | ||
| 100 | * @return {@code true} if this object is the same as the object argument; | ||
| 101 | * {@code false} otherwise | ||
| 102 | * @see sun.reflect.generics.reflectiveObjects.WildcardTypeImpl#equals | ||
| 103 | */ | ||
| 104 | @Override | ||
| 105 | public boolean equals(Object object) { | ||
| 106 | if (object instanceof WildcardType) { | ||
| 107 | WildcardType type = (WildcardType) object; | ||
| 108 | return Arrays.equals(this.upperBounds, type.getUpperBounds()) | ||
| 109 | && Arrays.equals(this.lowerBounds, type.getLowerBounds()); | ||
| 110 | } | ||
| 111 | return false; | ||
| 112 | } | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Returns a hash code value for the object. | ||
| 116 | * It is implemented compatibly with the JDK's | ||
| 117 | * {@link sun.reflect.generics.reflectiveObjects.WildcardTypeImpl WildcardTypeImpl}. | ||
| 118 | * | ||
| 119 | * @return a hash code value for this object | ||
| 120 | * @see sun.reflect.generics.reflectiveObjects.WildcardTypeImpl#hashCode | ||
| 121 | */ | ||
| 122 | @Override | ||
| 123 | public int hashCode() { | ||
| 124 | return Arrays.hashCode(this.upperBounds) | ||
| 125 | ^ Arrays.hashCode(this.lowerBounds); | ||
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Returns a string representation of the object. | ||
| 130 | * It is implemented compatibly with the JDK's | ||
| 131 | * {@link sun.reflect.generics.reflectiveObjects.WildcardTypeImpl WildcardTypeImpl}. | ||
| 132 | * | ||
| 133 | * @return a string representation of the object | ||
| 134 | * @see sun.reflect.generics.reflectiveObjects.WildcardTypeImpl#toString | ||
| 135 | */ | ||
| 136 | @Override | ||
| 137 | public String toString() { | ||
| 138 | StringBuilder sb; | ||
| 139 | Type[] bounds; | ||
| 140 | if (this.lowerBounds.length == 0) { | ||
| 141 | if (this.upperBounds.length == 0 || Object.class == this.upperBounds[0]) { | ||
| 142 | return "?"; | ||
| 143 | } | ||
| 144 | bounds = this.upperBounds; | ||
| 145 | sb = new StringBuilder("? extends "); | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | bounds = this.lowerBounds; | ||
| 149 | sb = new StringBuilder("? super "); | ||
| 150 | } | ||
| 151 | for (int i = 0; i < bounds.length; i++) { | ||
| 152 | if (i > 0) { | ||
| 153 | sb.append(" & "); | ||
| 154 | } | ||
| 155 | sb.append((bounds[i] instanceof Class) | ||
| 156 | ? ((Class) bounds[i]).getName() | ||
| 157 | : bounds[i].toString()); | ||
| 158 | } | ||
| 159 | return sb.toString(); | ||
| 160 | } | ||
| 161 | } | ||