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 5009360 5055963
  28  * @summary ClassType tests
  29  * @library ../../lib
  30  * @run main/othervm ClassTyp
  31  */
  32 
  33 
  34 import java.util.*;
  35 import com.sun.mirror.declaration.*;
  36 import com.sun.mirror.type.*;
  37 import com.sun.mirror.util.*;
  38 
  39 
  40 public class ClassTyp<T1,T2> extends Tester {
  41 
  42     public static void main(String[] args) {
  43         (new ClassTyp()).run();
  44     }
  45 
  46 
  47     // Declarations used by tests
  48 
  49     static class C1<S> extends AbstractSet<S> implements Set<S> {
  50         class C2<R> {
  51         }
  52 
  53         static class C3<R> {
  54             class C4<Q> {
  55             }
  56         }
  57 
  58         public Iterator<S> iterator() {
  59             return null;
  60         }
  61 
  62         public int size() {
  63             return 0;
  64         }
  65     }
  66 
  67 
  68     // Generate some class types to test.
  69     private C1<T1> f0;
  70     private C1<String> f1;
  71     private C1 f2;
  72     private C1.C3<T2> f3;
  73     private C1<T1>.C2<T2> f4;
  74     private C1.C2 f5;
  75     private C1<T1> f6;
  76     private C1.C3<T2>.C4<T1> f7;
  77     private static final int NUMTYPES = 8;
  78 
  79     // Type mirrors corresponding to the types of the above fields
  80     private ClassType[] t = new ClassType[NUMTYPES];
  81 
  82     // One more type:  our own.
  83     private ClassTyp<T1,T2> me = this;
  84 
  85 
  86     protected void init() {
  87         for (int i = 0; i < t.length; i++) {
  88             t[i] = (ClassType) getField("f"+i).getType();
  89         }
  90     }
  91 
  92 
  93     // TypeMirror methods
  94 
  95     @Test(result="class")
  96     Collection<String> accept() {
  97         final Collection<String> res = new ArrayList<String>();
  98 
  99         t[0].accept(new SimpleTypeVisitor() {
 100             public void visitReferenceType(ReferenceType t) {
 101                 res.add("ref type");
 102             }
 103             public void visitClassType(ClassType t) {
 104                 res.add("class");
 105             }
 106             public void visitInterfaceType(InterfaceType t) {
 107                 res.add("interface");
 108             }
 109         });
 110         return res;
 111     }
 112 
 113     @Test(result="true")
 114     boolean equals1() {
 115         return t[0].equals(t[0]);
 116     }
 117 
 118     @Test(result="false")
 119     boolean equals2() {
 120         return t[0].equals(t[1]);
 121     }
 122 
 123     // Raw type is not same as type instantiated with unbounded type var.
 124     @Test(result="false")
 125     boolean equals3() {
 126         return t[0].equals(t[2]);
 127     }
 128 
 129     // C1<T1> is same type as C1<T1>
 130     @Test(result="true")
 131     boolean equals4() {
 132         return t[0].equals(t[6]);
 133     }
 134 
 135     @Test(result={
 136               "ClassTyp.C1<T1>",
 137               "ClassTyp.C1<java.lang.String>",
 138               "ClassTyp.C1",
 139               "ClassTyp.C1.C3<T2>",
 140               "ClassTyp.C1<T1>.C2<T2>",
 141               "ClassTyp.C1.C2",
 142               "ClassTyp.C1<T1>",
 143               "ClassTyp.C1.C3<T2>.C4<T1>"
 144           },
 145           ordered=true)
 146     Collection<String> toStringTests() {
 147         Collection<String> res = new ArrayList<String>();
 148         for (ClassType c : t) {
 149             res.add(c.toString());
 150         }
 151         return res;
 152     }
 153 
 154 
 155     // DeclaredType methods
 156 
 157     @Test(result={"T1"})
 158     Collection<TypeMirror> getActualTypeArguments1() {
 159         return t[0].getActualTypeArguments();
 160     }
 161 
 162     @Test(result={})
 163     Collection<TypeMirror> getActualTypeArguments2() {
 164         return t[2].getActualTypeArguments();
 165     }
 166 
 167     @Test(result={"T2"})
 168     Collection<TypeMirror> getActualTypeArguments3() {
 169         return t[3].getActualTypeArguments();
 170     }
 171 
 172     @Test(result="null")
 173     DeclaredType getContainingType1() {
 174         ClassType thisType = (ClassType) getField("me").getType();
 175         return thisType.getContainingType();
 176     }
 177 
 178     @Test(result="ClassTyp")
 179     DeclaredType getContainingType2() {
 180         return t[0].getContainingType();
 181     }
 182 
 183     @Test(result="ClassTyp.C1")
 184     DeclaredType getContainingType3() {
 185         return t[3].getContainingType();
 186     }
 187 
 188     @Test(result="ClassTyp.C1<T1>")
 189     DeclaredType getContainingType4() {
 190         return t[4].getContainingType();
 191     }
 192 
 193     @Test(result={"java.util.Set<T1>"})
 194     Collection<InterfaceType> getSuperinterfaces() {
 195         return t[0].getSuperinterfaces();
 196     }
 197 
 198 
 199     // ClassType methods
 200 
 201     @Test(result="ClassTyp.C1<S>")
 202     ClassDeclaration getDeclaration1() {
 203         return t[0].getDeclaration();
 204     }
 205 
 206     @Test(result="ClassTyp.C1.C3<R>")
 207     ClassDeclaration getDeclaration2() {
 208         return t[3].getDeclaration();
 209     }
 210 
 211     @Test(result="ClassTyp.C1<S>.C2<R>")
 212     ClassDeclaration getDeclaration3a() {
 213         return t[4].getDeclaration();
 214     }
 215 
 216     @Test(result="ClassTyp.C1<S>.C2<R>")
 217     ClassDeclaration getDeclaration3b() {
 218         return t[5].getDeclaration();
 219     }
 220 
 221     @Test(result="true")
 222     boolean getDeclarationEq() {
 223         return t[0].getDeclaration() == t[6].getDeclaration();
 224     }
 225 
 226     @Test(result="java.util.AbstractSet<T1>")
 227     ClassType getSuperclass1() {
 228         return t[0].getSuperclass();
 229     }
 230 
 231     @Test(result="java.lang.Object")
 232     ClassType getSuperclass2() {
 233         return t[4].getSuperclass();
 234     }
 235 
 236     @Test(result="null")
 237     ClassType getSuperclassOfObject() {
 238         return t[4].getSuperclass().getSuperclass();
 239     }
 240 }