duke@1: /* duke@1: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: duke@1: /* duke@1: * @test duke@1: * @bug 4853450 5009396 5010636 5031156 duke@1: * @summary WildcardType tests duke@1: * @library ../../lib duke@1: * @compile -source 1.5 WildcardTyp.java duke@1: * @run main WildcardTyp duke@1: */ duke@1: duke@1: duke@1: import java.util.*; duke@1: import com.sun.mirror.declaration.*; duke@1: import com.sun.mirror.type.*; duke@1: import com.sun.mirror.util.*; duke@1: duke@1: duke@1: public class WildcardTyp extends Tester { duke@1: duke@1: public static void main(String[] args) { duke@1: (new WildcardTyp()).run(); duke@1: } duke@1: duke@1: duke@1: // Declarations to use for testing duke@1: duke@1: interface G { duke@1: } duke@1: duke@1: interface G1 { duke@1: } duke@1: duke@1: interface G2> { duke@1: } duke@1: duke@1: // Some wildcard types to test. duke@1: private G f0; // unbound duke@1: private G f1; // covariant duke@1: private G f2; // contravariant duke@1: private G f3; // duke@1: private G1 f4; // "true" upper bound is an intersection type duke@1: private G2 f5; // 'true" upper bound is a recursive F-bound and duke@1: // not expressible duke@1: private static final int NUMTYPES = 6; duke@1: duke@1: // Type mirrors corresponding to the wildcard types of the above fields duke@1: private WildcardType[] t = new WildcardType[NUMTYPES]; duke@1: duke@1: duke@1: protected void init() { duke@1: for (int i = 0; i < t.length; i++) { duke@1: DeclaredType type = (DeclaredType) getField("f"+i).getType(); duke@1: t[i] = (WildcardType) duke@1: type.getActualTypeArguments().iterator().next(); duke@1: } duke@1: } duke@1: duke@1: private WildcardType wildcardFor(String field) { duke@1: DeclaredType d = (DeclaredType) getField(field).getType(); duke@1: return (WildcardType) d.getActualTypeArguments().iterator().next(); duke@1: } duke@1: duke@1: duke@1: // TypeMirror methods duke@1: duke@1: @Test(result="wild thing") duke@1: Collection accept() { duke@1: final Collection res = new ArrayList(); duke@1: duke@1: t[0].accept(new SimpleTypeVisitor() { duke@1: public void visitTypeMirror(TypeMirror t) { duke@1: res.add("type"); duke@1: } duke@1: public void visitReferenceType(ReferenceType t) { duke@1: res.add("ref type"); duke@1: } duke@1: public void visitWildcardType(WildcardType t) { duke@1: res.add("wild thing"); duke@1: } duke@1: }); duke@1: return res; duke@1: } duke@1: duke@1: @Test(result={ duke@1: "?", duke@1: "? extends java.lang.Number", duke@1: "? super java.lang.Number", duke@1: "? extends java.lang.Object", duke@1: "?", duke@1: "?" duke@1: }, duke@1: ordered=true) duke@1: Collection toStringTests() { duke@1: Collection res = new ArrayList(); duke@1: for (WildcardType w : t) { duke@1: res.add(w.toString()); duke@1: } duke@1: return res; duke@1: } duke@1: duke@1: duke@1: // WildcardType methods duke@1: duke@1: @Test(result={ duke@1: "null", duke@1: "null", duke@1: "java.lang.Number", duke@1: "null", duke@1: "null", duke@1: "null" duke@1: }, duke@1: ordered=true) duke@1: Collection getLowerBounds() { duke@1: Collection res = new ArrayList(); duke@1: for (WildcardType w : t) { duke@1: Collection bounds = w.getLowerBounds(); duke@1: int num = bounds.size(); duke@1: if (num > 1) { duke@1: throw new AssertionError("Bounds abound"); duke@1: } duke@1: res.add((num > 0) ? bounds.iterator().next() : null); duke@1: } duke@1: return res; duke@1: } duke@1: duke@1: @Test(result={ duke@1: "null", duke@1: "java.lang.Number", duke@1: "null", duke@1: "java.lang.Object", duke@1: "null", duke@1: "null" duke@1: }, duke@1: ordered=true) duke@1: Collection getUpperBounds() { duke@1: Collection res = new ArrayList(); duke@1: for (WildcardType w : t) { duke@1: Collection bounds = w.getUpperBounds(); duke@1: int num = bounds.size(); duke@1: if (num > 1) { duke@1: throw new AssertionError("Bounds abound"); duke@1: } duke@1: res.add((num > 0) ? bounds.iterator().next() : null); duke@1: } duke@1: return res; duke@1: } duke@1: }