< prev index next >

src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/WildcardTypeImpl.java

Print this page




  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 sun.reflect.generics.reflectiveObjects;
  27 
  28 
  29 import java.lang.reflect.Type;
  30 import java.lang.reflect.WildcardType;
  31 import sun.reflect.generics.factory.GenericsFactory;
  32 import sun.reflect.generics.tree.FieldTypeSignature;
  33 import sun.reflect.generics.visitor.Reifier;
  34 import java.util.Arrays;

  35 
  36 
  37 /**
  38  * Implementation of WildcardType interface for core reflection.
  39  */
  40 public class WildcardTypeImpl extends LazyReflectiveObjectGenerator
  41     implements WildcardType {
  42 
  43     /*
  44      * We are required to evaluate the bounds lazily, so we store them as ASTs
  45      * until we are first asked for them.  This also neatly solves the problem
  46      * with F-bounds - you can't reify them before the formal is defined.
  47      */
  48 
  49     /** The upper bounds.  Lazily converted from FieldTypeSignature[] to Type[]. */
  50     private volatile Object[] upperBounds;
  51 
  52     /** The lower bounds.  Lazily converted from FieldTypeSignature[] to Type[]. */
  53     private volatile Object[] lowerBounds;
  54 


 139     }
 140 
 141     public String toString() {
 142         Type[] lowerBounds = getLowerBounds();
 143         Type[] bounds = lowerBounds;
 144         StringBuilder sb = new StringBuilder();
 145 
 146         if (lowerBounds.length > 0)
 147             sb.append("? super ");
 148         else {
 149             Type[] upperBounds = getUpperBounds();
 150             if (upperBounds.length > 0 && !upperBounds[0].equals(Object.class) ) {
 151                 bounds = upperBounds;
 152                 sb.append("? extends ");
 153             } else
 154                 return "?";
 155         }
 156 
 157         assert bounds.length > 0;
 158 
 159         boolean first = true;
 160         for(Type bound: bounds) {
 161             if (!first)
 162                 sb.append(" & ");
 163 
 164             first = false;
 165             sb.append(bound.getTypeName());
 166         }


 167         return sb.toString();
 168     }
 169 
 170     @Override
 171     public boolean equals(Object o) {
 172         if (o instanceof WildcardType) {
 173             WildcardType that = (WildcardType) o;
 174             return
 175                 Arrays.equals(this.getLowerBounds(),
 176                               that.getLowerBounds()) &&
 177                 Arrays.equals(this.getUpperBounds(),
 178                               that.getUpperBounds());
 179         } else
 180             return false;
 181     }
 182 
 183     @Override
 184     public int hashCode() {
 185         Type [] lowerBounds = getLowerBounds();
 186         Type [] upperBounds = getUpperBounds();


  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 sun.reflect.generics.reflectiveObjects;
  27 
  28 
  29 import java.lang.reflect.Type;
  30 import java.lang.reflect.WildcardType;
  31 import sun.reflect.generics.factory.GenericsFactory;
  32 import sun.reflect.generics.tree.FieldTypeSignature;
  33 import sun.reflect.generics.visitor.Reifier;
  34 import java.util.Arrays;
  35 import java.util.StringJoiner;
  36 
  37 
  38 /**
  39  * Implementation of WildcardType interface for core reflection.
  40  */
  41 public class WildcardTypeImpl extends LazyReflectiveObjectGenerator
  42     implements WildcardType {
  43 
  44     /*
  45      * We are required to evaluate the bounds lazily, so we store them as ASTs
  46      * until we are first asked for them.  This also neatly solves the problem
  47      * with F-bounds - you can't reify them before the formal is defined.
  48      */
  49 
  50     /** The upper bounds.  Lazily converted from FieldTypeSignature[] to Type[]. */
  51     private volatile Object[] upperBounds;
  52 
  53     /** The lower bounds.  Lazily converted from FieldTypeSignature[] to Type[]. */
  54     private volatile Object[] lowerBounds;
  55 


 140     }
 141 
 142     public String toString() {
 143         Type[] lowerBounds = getLowerBounds();
 144         Type[] bounds = lowerBounds;
 145         StringBuilder sb = new StringBuilder();
 146 
 147         if (lowerBounds.length > 0)
 148             sb.append("? super ");
 149         else {
 150             Type[] upperBounds = getUpperBounds();
 151             if (upperBounds.length > 0 && !upperBounds[0].equals(Object.class) ) {
 152                 bounds = upperBounds;
 153                 sb.append("? extends ");
 154             } else
 155                 return "?";
 156         }
 157 
 158         assert bounds.length > 0;
 159 
 160         StringJoiner sj = new StringJoiner(" & ");
 161         for(Type bound: bounds) {
 162             sj.add(bound.getTypeName());




 163         }
 164         sb.append(sj.toString());
 165 
 166         return sb.toString();
 167     }
 168 
 169     @Override
 170     public boolean equals(Object o) {
 171         if (o instanceof WildcardType) {
 172             WildcardType that = (WildcardType) o;
 173             return
 174                 Arrays.equals(this.getLowerBounds(),
 175                               that.getLowerBounds()) &&
 176                 Arrays.equals(this.getUpperBounds(),
 177                               that.getUpperBounds());
 178         } else
 179             return false;
 180     }
 181 
 182     @Override
 183     public int hashCode() {
 184         Type [] lowerBounds = getLowerBounds();
 185         Type [] upperBounds = getUpperBounds();
< prev index next >