1 /*
   2  * Copyright (c) 2004, 2008, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @bug 4853450 5009396 5010636 5031156
  28  * @summary WildcardType tests
  29  * @library ../../lib
  30  * @compile -source 1.5 WildcardTyp.java
  31  * @run main/othervm WildcardTyp
  32  */
  33 
  34 
  35 import java.util.*;
  36 import com.sun.mirror.declaration.*;
  37 import com.sun.mirror.type.*;
  38 import com.sun.mirror.util.*;
  39 
  40 
  41 public class WildcardTyp extends Tester {
  42 
  43     public static void main(String[] args) {
  44         (new WildcardTyp()).run();
  45     }
  46 
  47 
  48     // Declarations to use for testing
  49 
  50     interface G<T> {
  51     }
  52 
  53     interface G1<N extends Number & Runnable> {
  54     }
  55 
  56     interface G2<T extends G2<T>> {
  57     }
  58 
  59     // Some wildcard types to test.
  60     private G<?> f0;                    // unbound
  61     private G<? extends Number> f1;     // covariant
  62     private G<? super Number> f2;       // contravariant
  63     private G<? extends Object> f3;     // <sigh>
  64     private G1<?> f4;   // "true" upper bound is an intersection type
  65     private G2<?> f5;   // 'true" upper bound is a recursive F-bound and
  66                         // not expressible
  67     private static final int NUMTYPES = 6;
  68 
  69     // Type mirrors corresponding to the wildcard types of the above fields
  70     private WildcardType[] t = new WildcardType[NUMTYPES];
  71 
  72 
  73     protected void init() {
  74         for (int i = 0; i < t.length; i++) {
  75             DeclaredType type = (DeclaredType) getField("f"+i).getType();
  76             t[i] = (WildcardType)
  77                 type.getActualTypeArguments().iterator().next();
  78         }
  79     }
  80 
  81     private WildcardType wildcardFor(String field) {
  82         DeclaredType d = (DeclaredType) getField(field).getType();
  83         return (WildcardType) d.getActualTypeArguments().iterator().next();
  84     }
  85 
  86 
  87     // TypeMirror methods
  88 
  89     @Test(result="wild thing")
  90     Collection<String> accept() {
  91         final Collection<String> res = new ArrayList<String>();
  92 
  93         t[0].accept(new SimpleTypeVisitor() {
  94             public void visitTypeMirror(TypeMirror t) {
  95                 res.add("type");
  96             }
  97             public void visitReferenceType(ReferenceType t) {
  98                 res.add("ref type");
  99             }
 100             public void visitWildcardType(WildcardType t) {
 101                 res.add("wild thing");
 102             }
 103         });
 104         return res;
 105     }
 106 
 107     @Test(result={
 108                 "?",
 109                 "? extends java.lang.Number",
 110                 "? super java.lang.Number",
 111                 "? extends java.lang.Object",
 112                 "?",
 113                 "?"
 114           },
 115           ordered=true)
 116     Collection<String> toStringTests() {
 117         Collection<String> res = new ArrayList<String>();
 118         for (WildcardType w : t) {
 119             res.add(w.toString());
 120         }
 121         return res;
 122     }
 123 
 124 
 125     // WildcardType methods
 126 
 127     @Test(result={
 128                 "null",
 129                 "null",
 130                 "java.lang.Number",
 131                 "null",
 132                 "null",
 133                 "null"
 134           },
 135           ordered=true)
 136     Collection<ReferenceType> getLowerBounds() {
 137         Collection<ReferenceType> res = new ArrayList<ReferenceType>();
 138         for (WildcardType w : t) {
 139             Collection<ReferenceType> bounds = w.getLowerBounds();
 140             int num = bounds.size();
 141             if (num > 1) {
 142                 throw new AssertionError("Bounds abound");
 143             }
 144             res.add((num > 0) ? bounds.iterator().next() : null);
 145         }
 146         return res;
 147     }
 148 
 149     @Test(result={
 150                 "null",
 151                 "java.lang.Number",
 152                 "null",
 153                 "java.lang.Object",
 154                 "null",
 155                 "null"
 156           },
 157           ordered=true)
 158     Collection<ReferenceType> getUpperBounds() {
 159         Collection<ReferenceType> res = new ArrayList<ReferenceType>();
 160         for (WildcardType w : t) {
 161             Collection<ReferenceType> bounds = w.getUpperBounds();
 162             int num = bounds.size();
 163             if (num > 1) {
 164                 throw new AssertionError("Bounds abound");
 165             }
 166             res.add((num > 0) ? bounds.iterator().next() : null);
 167         }
 168         return res;
 169     }
 170 }