1 /*
   2  * Copyright 2003 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25 * @test
  26 * @bug 4891872
  27 * @summary Some tests for the generic core reflection api.
  28 * @author Gilad Bracha
  29 * @compile -source 1.5 TestC1.java
  30 * @run main/othervm -ea TestC1
  31 */
  32 
  33 
  34 import java.lang.reflect.*;
  35 
  36 
  37 abstract class C1<T> {
  38 
  39     public T ft;
  40     public C1<T> fc1t;
  41     public C1 fc1;
  42 
  43     public C1(T t) {}
  44 
  45     public abstract  C1<T> mc1t(T t, C1<T> c1t, C1 c1);
  46 
  47     public abstract C1 mc1();
  48 
  49     public abstract T mt(T t);
  50 
  51 }
  52 
  53 public class TestC1 {
  54 
  55     static Class<C1> cls = C1.class;
  56     static {
  57         TestC1.class.getClassLoader().setDefaultAssertionStatus(true);
  58     }
  59 
  60 
  61 
  62     public static void main(String[] args) throws Throwable {
  63         testSuperclass();
  64         testSuperInterfaces();
  65         testTypeParameters();
  66         testMethods();
  67         testConstructor();
  68         testFields();
  69     }
  70 
  71     static void testSuperclass() {
  72         System.out.println("testing superclass");
  73         Type sc = cls.getGenericSuperclass();
  74         assert
  75             (sc == Object.class) :
  76             "The generic superclass of C1 should be Object";
  77     }
  78 
  79     static void testSuperInterfaces() {
  80         System.out.println("testing superinterfaces");
  81         Type[] sis = cls.getGenericInterfaces();
  82         assert
  83             (sis.length == 0) :
  84             "C1 should have no generic superinterfaces";
  85     }
  86 
  87     static void testTypeParameters() {
  88         System.out.println("testing type parameters");
  89         TypeVariable[] tvs = cls.getTypeParameters();
  90         assert
  91             tvs.length == 1 :
  92             "C1 should have one type parameter";
  93         TypeVariable tv = tvs[0];
  94         Type[] bs = tv.getBounds();
  95         assert
  96             bs.length == 1 :
  97             "T should have one bound";
  98         assert
  99             bs[0] == Object.class :
 100             "The default bound of a type variable should be Object";
 101     }
 102 
 103     static void testMethods() throws NoSuchMethodException {
 104         System.out.println("testing methods");
 105         Class[] params1 = new Class[3];
 106         params1[0] = Object.class;
 107         params1[1] = cls;
 108         params1[2] = cls;
 109 
 110         Class[] params3 = new Class[1];
 111         params3[0] = Object.class;
 112 
 113         Method mc1t = cls.getMethod("mc1t", params1);
 114         Method mc1 = cls.getMethod("mc1", new Class[0]);
 115         Method mt = cls.getMethod("mt", params3);
 116 
 117         Type rt_mc1t = mc1t.getGenericReturnType();
 118         Type rt_mc1 = mc1.getGenericReturnType();
 119         Type rt_mt = mt.getGenericReturnType();
 120 
 121         Type[] pt_mc1t = mc1t.getGenericParameterTypes();
 122 
 123         assert
 124             pt_mc1t.length == 3 :
 125             "C1.mc1t has three parameters";
 126         Type p1_mc1t = pt_mc1t[0];
 127         assert p1_mc1t != null;
 128         assert
 129             p1_mc1t instanceof TypeVariable :
 130             "Generic type of the 1st parameter of mc1t(T) is a type variable";
 131         TypeVariable tv = (TypeVariable) p1_mc1t;
 132 
 133         assert
 134             tv.getName().equals("T") :
 135             "Name of 1st type parameter of mc1t is T, not " + tv.getName();
 136         Type[] bs = tv.getBounds();
 137         assert
 138             bs.length == 1 :
 139             "T should have one bound (mc1t)";
 140         assert
 141             bs[0] == Object.class :
 142             "The bound of T should be Object (mc1t)";
 143 
 144         Type p2_mc1t = pt_mc1t[1];
 145 
 146         assert
 147             p2_mc1t instanceof ParameterizedType :
 148             "The type of parameter 2 of mc1t is a parameterized type";
 149         ParameterizedType pt = (ParameterizedType) p2_mc1t;
 150         assert
 151             pt.getRawType() == cls :
 152             "Type of parameter 2 of mc1t is instantiation of C1";
 153         assert
 154             pt.getOwnerType() == null :
 155             "Type of parameter 2 of mc1t is has null owner";
 156 
 157         Type[] tas = pt.getActualTypeArguments();
 158         assert
 159             tas.length == 1 :
 160             "The type of parameter 2 of mc1t has one type argument";
 161         Type ta = tas[0];
 162 
 163         assert
 164             ta instanceof TypeVariable :
 165             "The actual type arg of C1<T> is a type variable (mc1t)";
 166         tv = (TypeVariable) ta;
 167         assert
 168             tv.getName().equals("T") :
 169             "mc1t: Name of the type arg of C1<T> is T, not " + tv.getName();
 170         bs = tv.getBounds();
 171         assert
 172             bs.length == 1 :
 173             "mc1t: The type argument of C1<T>  should have one bound";
 174         assert
 175             bs[0] == Object.class :
 176             "mc1t: The bound of the type arg of C1<T> should be Object";
 177 
 178         Type p3_mc1t = pt_mc1t[2];
 179 
 180         assert
 181             p3_mc1t == cls :
 182             "Type of parameter 3 of mc1t is C1";
 183 
 184         Type[] pt_mc1 = mc1.getGenericParameterTypes();
 185         assert
 186             pt_mc1.length == 0 :
 187             "C1.mc1 has zero parameters";
 188 
 189         Type[] pt_mt = mt.getGenericParameterTypes();
 190         assert
 191             pt_mt.length == 1 :
 192             "C1.mt has one parameter";
 193         Type p_mt = pt_mt[0];
 194         assert
 195             p_mt instanceof TypeVariable :
 196             "The generic type of the parameter of mt(T) is a type variable";
 197         tv = (TypeVariable) p_mt;
 198         assert
 199             tv.getName().equals("T") :
 200             "The name of the type parameter of mt is T, not " + tv.getName();
 201         bs = tv.getBounds();
 202         assert
 203             bs.length == 1 :
 204             "T should have one bound";
 205         assert
 206             bs[0] == Object.class :
 207             "The bound of T should be Object";
 208 
 209         Type[] et_mc1t = mc1t.getGenericExceptionTypes();
 210         assert
 211             et_mc1t.length == 0 :
 212             "Method C1.mc1t should have no generic exception types";
 213 
 214         Type[] et_mc1 = mc1.getGenericExceptionTypes();
 215         assert
 216             et_mc1.length == 0 :
 217             "Method C1.mc1 should have no generic exception types";
 218 
 219         Type[] et_mt = mt.getGenericExceptionTypes();
 220 
 221         assert
 222             et_mt.length == 0 :
 223             "Method C1.mt should have no generic exception types";
 224 
 225 
 226         TypeVariable[] tv_mc1t = mc1t.getTypeParameters();
 227         assert
 228             tv_mc1t.length == 0 :
 229             "Method C1.mc1t should have no type parameters";
 230 
 231         TypeVariable[] tv_mc1 = mc1.getTypeParameters();
 232         assert
 233             tv_mc1.length == 0 :
 234             "Method C1.mc1 should have no type parameters";
 235 
 236         TypeVariable[] tv_mt = mt.getTypeParameters();
 237         assert
 238             tv_mt.length == 0 :
 239             "Method C1.mt should have no type parameters";
 240     }
 241 
 242 
 243     static void testFields() throws NoSuchFieldException{
 244         System.out.println("testing fields");
 245         Field ft = cls. getField("ft");
 246         Field fc1t = cls. getField("fc1t");
 247         Field fc1 = cls. getField("fc1");
 248 
 249         Type gt_ft = ft.getGenericType();
 250         assert
 251             gt_ft instanceof TypeVariable :
 252             "The generic type of C1.ft is a type variable";
 253         TypeVariable tv = (TypeVariable) gt_ft;
 254         assert
 255             tv.getName().equals("T") :
 256             "The name of the type of ft is T, not " + tv.getName();
 257         Type[] bs = tv.getBounds();
 258         assert
 259             bs.length == 1 :
 260             "The type of ft should have one bound";
 261         assert
 262             bs[0] == Object.class :
 263             "The bound of the type of ft should be Object";
 264 
 265         Type gt_fc1t = fc1t.getGenericType();
 266         assert
 267             gt_fc1t instanceof ParameterizedType :
 268             "The generic type of C1.fc1t is a parameterized type";
 269         ParameterizedType pt = (ParameterizedType) gt_fc1t;
 270         assert
 271             pt.getRawType() == cls :
 272             "Type of C1.fc1t is instantiation of C1";
 273         assert
 274             pt.getOwnerType() == null :
 275             "Type of C1.fc1t is has null owner";
 276         Type[] tas = pt.getActualTypeArguments();
 277         assert
 278             tas.length == 1 :
 279             "The type of fc1t has one type argument";
 280         Type ta = tas[0];
 281 
 282         assert
 283             ta instanceof TypeVariable :
 284             "The actual type arg of C1<T> is a type variable";
 285         tv = (TypeVariable) ta;
 286         assert
 287             tv.getName().equals("T") :
 288             "The name of the type arg of C1<T> is T, not " + tv.getName();
 289         bs = tv.getBounds();
 290         assert
 291             bs.length == 1 :
 292             "The type argument of C1<T>  should have one bound";
 293         assert
 294             bs[0] == Object.class :
 295             "The bound of the type arg of C1<T> should be Object";
 296 
 297         Type gt_fc1 = fc1.getGenericType();
 298         assert
 299             gt_fc1 == cls :
 300             " Type of C1.fc1 should be C1";
 301     }
 302 
 303     static void testConstructor() throws NoSuchMethodException {
 304         System.out.println("testing constructors");
 305         Class[] params = new Class[1];
 306         params[0] = Object.class;
 307         Constructor<C1> con = cls.getDeclaredConstructor(params);
 308 
 309         Type[] pt_con = con.getGenericParameterTypes();
 310         assert
 311             pt_con.length == 1 :
 312             "Constructor C1(T) should have one generic parameter type";
 313         Type pt = pt_con[0];
 314         assert
 315             pt instanceof TypeVariable :
 316             "The generic type of the parameter of C1(T) is a type variable";
 317         TypeVariable tv = (TypeVariable) pt;
 318         assert
 319             tv.getName().equals("T") :
 320             "The name of the type parameter of C is T, not " + tv.getName();
 321         Type[] bs = tv.getBounds();
 322         assert
 323             bs.length == 1 :
 324             "T should have one bound";
 325         assert
 326             bs[0] == Object.class :
 327             "The bound of T should be Object";
 328 
 329         Type[] et_con = con.getGenericExceptionTypes();
 330         assert
 331             et_con.length == 0 :
 332             "Constructor C1(T) should have no generic exception types";
 333 
 334         TypeVariable[] tv_con = con.getTypeParameters();
 335         assert
 336             tv_con.length == 0 :
 337             "Constructor C1(T) should have no type parameters";
 338 
 339     }
 340 }