Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Generics/TestC2.java
38828 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 489187226* @summary Some tests for the generic core reflection api.27* @author Gilad Bracha28* @compile TestC2.java29* @run main/othervm -ea TestC230*/313233import java.lang.reflect.*;343536abstract class C0<T> {3738public T ft;39public C0<T> fc1t;40public C0 fc1;4142public C0(){}43public C0(T t) {}4445public abstract C0<T> mc1t(T t, C0<T> c1t, C0 c1);4647public abstract C0 mc1();4849public abstract T mt(T t);5051}5253interface I1<X1, X2> extends I3 {5455X1 foo(X2 x2);56}5758interface I2<E1, E2 extends Throwable, E3> {596061E1 bar(E3 e3) throws E2;6263}6465interface I3 {666768}697071abstract class C2<T1 extends C2<T1, T2, T3>, T2 extends C0<T2>,72T3 extends Throwable>73extends C0<T1>74implements I1<T1, T2>, I2<T1, T3, T2>, I375{7677public T1 ft;78public C0<String> fc1t;79public C0 fc1;80public int fi;8182public C2(T2 t2) {}83public <T> C2(T t) {}84public <T1, T2, T3, T4> C2(T1 t1, T2 t2, T4 t4) {}85public C2() throws T3 {}8687public abstract <T> C0<T> mc1t(T3 t3, C0<T> c1t, C0 c1);8889public abstract <E, R> C0 mc1(E e);9091public abstract T1 mt(T2 t);9293}9495public class TestC2 {9697static Class<C2> cls = C2.class;9899100public static void main(String[] args) throws Throwable {101testSuperclass();102testSuperInterfaces();103testTypeParameters();104testMethods();105testConstructors();106testFields();107}108109static void testSuperclass() {110111System.out.println("testing superclass");112Type sc = cls.getGenericSuperclass();113assert114sc instanceof ParameterizedType :115"Superclass of C2 should be a parameterized type";116ParameterizedType psc = (ParameterizedType) sc;117assert118((psc.getRawType() == C0.class) ) :119"The raw generic superclass of C2 should be C0";120121Type[] tas = psc.getActualTypeArguments();122assert123tas.length == 1 :124"Superclass of C2 should have one type argument";125126Type t = tas[0];127128assert129t instanceof TypeVariable :130"Type argument to superclass of C2 should be a type variable";131132TypeVariable tv = (TypeVariable) t;133assert134tv.getName().equals("T1") :135"Name of type argument to superclass of C2 should be T1";136Type[] bs = tv.getBounds();137assert138bs.length == 1 :139"T1 has one bound (superclass)";140t = bs[0];141assert142t instanceof ParameterizedType :143"Bound of C0 should be a parameterized type";144ParameterizedType pt = (ParameterizedType) t;145146assert147((pt.getRawType() == C2.class) ) :148"The raw bound of T1 should be C2";149150tas = pt.getActualTypeArguments();151assert152tas.length == 3 :153"Bound of T1 should have three type arguments";154assert155tas[0] instanceof TypeVariable :156"First argument to bound of T1 is a type variable";157assert158tas[1] instanceof TypeVariable :159"Second argument to bound of T1 is a type variable";160assert161tas[2] instanceof TypeVariable :162"Third argument to bound of T1 is a type variable";163164TypeVariable tv1 = (TypeVariable) tas[0];165TypeVariable tv2 = (TypeVariable) tas[1];166TypeVariable tv3 = (TypeVariable) tas[2];167168assert169tv1.getName().equals("T1"):170"First type arg to bound of T1 is T1";171assert172tv2.getName().equals("T2"):173"Seconmd type arg to bound of T1 is T2";174assert175tv3.getName().equals("T3"):176"Third type arg to bound of T1 is T3";177178179}180181static void testSuperInterfaces() {182System.out.println("testing superinterfaces");183Type[] sis = cls.getGenericInterfaces();184assert185((sis.length == 3)):186"C2 should have three generic superinterfaces";187188Type t = sis[0];189assert190t instanceof ParameterizedType :191"First superinterface of C2 should be a parameterized type";192ParameterizedType pt = (ParameterizedType) t;193assert194pt.getRawType() == I1.class :195"First super interface of C2 is instantiation of I1";196Type[] tas = pt.getActualTypeArguments();197assert198tas.length == 2 :199"First super interface of C2 has 2 type arguments";200201t = sis[1];202assert203t instanceof ParameterizedType :204"Second superinterface of C2 should be a parameterized type";205pt = (ParameterizedType) t;206assert207pt.getRawType() == I2.class :208"Second super interface of C2 is instantiation of I2";209tas = pt.getActualTypeArguments();210assert211tas.length == 3 :212"Second super interface of C2 has 3 type arguments";213214t = sis[2];215assert216t == I3.class :217"Third superinterface of C2 is I3";218219// Test interfaces themselves220221TypeVariable[] tvs = I1.class.getTypeParameters();222assert223tvs.length == 2 :224"I3 has two formal type parameters";225assert226tvs[0].getName().equals("X1") :227"Name of first formal type arg of I1 is X1";228assert229tvs[1].getName().equals("X2") :230"Name of second formal type arg of I1 is X2";231232assert233I1.class.getGenericSuperclass() == I1.class.getSuperclass() :234"The generic and non-generic superclasses of an interface must be the same";235sis = I1.class.getGenericInterfaces();236assert237sis.length == 1 :238"I1 has one generic superinterface";239assert240sis[0] == I3.class :241"Superinterface of I1 is I3";242243tvs = I2.class.getTypeParameters();244assert245tvs.length == 3 :246"I3 has three formal type parameters";247assert248tvs[0].getName().equals("E1") :249"Name of first formal type arg of I2 is E1";250assert251tvs[1].getName().equals("E2") :252"Name of second formal type arg of I2 is E2";253assert254tvs[2].getName().equals("E3") :255"Name of third formal type arg of I2 is E3";256257assert258I2.class.getGenericSuperclass() == I2.class.getSuperclass() :259"The generic and non-generic superclasses of an interface must be the same";260261tvs = I3.class.getTypeParameters();262assert263tvs.length == 0 :264"I3 has no formal type parameters";265266assert267I3.class.getGenericSuperclass() == I3.class.getSuperclass() :268"The generic and non-generic superclasses of an interface must be the same";269270271}272273static void testTypeParameters() {274System.out.println("testing type parameters");275TypeVariable[] tvs = cls.getTypeParameters();276assert277tvs.length == 3 :278"C2 should have three type parameters";279TypeVariable tv = tvs[0];280Type[] bs = tv.getBounds();281assert282bs.length == 1 :283"T1 should have one bound";284assert285bs[0] instanceof ParameterizedType :286"The bound of T1 should be a parameterized type";287288tv = tvs[1];289bs = tv.getBounds();290assert291bs.length == 1 :292"T2 should have one bound";293assert294bs[0] instanceof ParameterizedType :295"The bound of T2 should be a parameterized type";296297tv = tvs[2];298bs = tv.getBounds();299assert300bs.length == 1 :301"T3 should have one bound";302assert303bs[0] == Throwable.class :304"The bound of T3 should be Throwable";305}306307static void testMethods() throws NoSuchMethodException {308System.out.println("testing methods");309310311312Class[] params1 = new Class[3];313params1[0] = Throwable.class;314params1[1] = C0.class;315params1[2] = C0.class;316317Class[] params2 = new Class[1];318params2[0] = Object.class;319320Class[] params3 = new Class[1];321params3[0] = C0.class;322323Method mc1t = cls.getMethod("mc1t", params1);324Method mc1 = cls.getMethod("mc1", params2);325Method mt = cls.getMethod("mt", params3);326327Type rt_mc1t = mc1t.getGenericReturnType();328assert329rt_mc1t instanceof ParameterizedType :330"The return type of mc1t should be a parameterized type";331ParameterizedType pt = (ParameterizedType) rt_mc1t;332333assert334pt.getRawType() == C0.class :335"The raw return type of mc1t should be C0";336337Type[] tas = pt.getActualTypeArguments();338assert339tas.length == 1 :340"Return type of mc1t should have one type argument";341assert342tas[0] instanceof TypeVariable :343"Type argument of return type of mc1t is a type variable";344345Type rt_mc1 = mc1.getGenericReturnType();346assert347rt_mc1 == C0.class :348"Return type of mc1 is C0";349350Type rt_mt = mt.getGenericReturnType();351assert352rt_mt instanceof TypeVariable :353"Return type of mt is a type variable";354355Type[] pt_mc1t = mc1t.getGenericParameterTypes();356357assert358pt_mc1t.length == 3 :359"C0.mc1t has three parameters";360Type p1_mc1t = pt_mc1t[0];361assert p1_mc1t != null;362assert363p1_mc1t instanceof TypeVariable :364"Generic type of the 1st parameter of mc1t(T) is a type variable";365TypeVariable tv = (TypeVariable) p1_mc1t;366367assert368tv.getName().equals("T3") :369"Name of 1st type parameter of mc1t is T3, not " + tv.getName();370Type[] bs = tv.getBounds();371assert372bs.length == 1 :373"T3 should have one bound (mc1t)";374assert375bs[0] == Throwable.class :376"The bound of T3 should be Throwable(mc1t)";377378Type p2_mc1t = pt_mc1t[1];379assert380p2_mc1t instanceof ParameterizedType :381"The type of parameter 2 of mc1t is a parameterized type";382pt = (ParameterizedType) p2_mc1t;383assert384pt.getRawType() == C0.class :385"Type of parameter 2 of mc1t is instantiation of C0";386assert387pt.getOwnerType() == null :388"Type of parameter 2 of mc1t is has null owner";389390tas = pt.getActualTypeArguments();391assert392tas.length == 1 :393"The type of parameter 2 of mc1t has one type argument";394Type ta = tas[0];395396assert397ta instanceof TypeVariable :398"The actual type arg of C0<T> is a type variable (mc1t)";399tv = (TypeVariable) ta;400assert401tv.getName().equals("T") :402"mc1t: Name of the type arg of C0<T> is T, not " + tv.getName();403bs = tv.getBounds();404assert405bs.length == 1 :406"mc1t: The type argument of C0<T> should have one bound";407assert408bs[0] == Object.class :409"mc1t: The bound of the type arg of C0<T> should be Object";410411Type p3_mc1t = pt_mc1t[2];412assert413p3_mc1t == C0.class :414"Type of parameter 3 of mc1t is C0";415416Type[] pt_mc1 = mc1.getGenericParameterTypes();417assert418pt_mc1.length == 1 :419"C2.mc1 has one parameter";420421Type[] pt_mt = mt.getGenericParameterTypes();422assert423pt_mt.length == 1 :424"C2.mt has one parameter";425Type p_mt = pt_mt[0];426assert427p_mt instanceof TypeVariable :428"The generic type of the parameter of mt(T) is a type variable";429tv = (TypeVariable) p_mt;430assert431tv.getName().equals("T2") :432"The name of the type parameter of mt is T2, not " + tv.getName();433bs = tv.getBounds();434assert435bs.length == 1 :436"T2 should have one bound";437assert438bs[0] instanceof ParameterizedType:439"The bound of T2 should be parameterized type";440441Type[] et_mc1t = mc1t.getGenericExceptionTypes();442assert443et_mc1t.length == 0 :444"Method C0.mc1t should have no generic exception types";445446Type[] et_mc1 = mc1.getGenericExceptionTypes();447assert448et_mc1.length == 0 :449"Method C0.mc1 should have no generic exception types";450451Type[] et_mt = mt.getGenericExceptionTypes();452assert453et_mt.length == 0 :454"Method C0.mt should have no generic exception types";455456457TypeVariable[] tv_mc1t = mc1t.getTypeParameters();458assert459tv_mc1t.length == 1 :460"Method C2.mc1t should have one type parameter";461462TypeVariable[] tv_mc1 = mc1.getTypeParameters();463assert464tv_mc1.length == 2 :465"Method C2.mc1 should have two type parameters";466467TypeVariable[] tv_mt = mt.getTypeParameters();468assert469tv_mt.length == 0 :470"Method C2.mt should have no type parameters";471}472473474static void testFields() throws NoSuchFieldException{475System.out.println("testing fields");476Field ft = cls. getField("ft");477Field fc1t = cls. getField("fc1t");478Field fc1 = cls. getField("fc1");479Field fi = cls. getField("fi");480481Type gt_ft = ft.getGenericType();482assert483gt_ft instanceof TypeVariable :484"The generic type of C0.ft is a type variable";485TypeVariable tv = (TypeVariable) gt_ft;486assert487tv.getName().equals("T1") :488"The name of the type of ft is T1, not " + tv.getName();489Type[] bs = tv.getBounds();490assert491bs.length == 1 :492"The type of ft should have one bound";493494495Type gt_fc1t = fc1t.getGenericType();496assert497gt_fc1t instanceof ParameterizedType :498"The generic type of C0.fc1t is a parameterized type";499ParameterizedType pt = (ParameterizedType) gt_fc1t;500assert501pt.getRawType() == C0.class :502"Type of C2.fc1t is an instantiation of C0";503assert504pt.getOwnerType() == null :505"Type of C2.fc1t is has null owner";506Type[] tas = pt.getActualTypeArguments();507assert508tas.length == 1 :509"The type of fc1t has one type argument";510Type ta = tas[0];511512assert513ta == String.class :514"The actual type arg of C0<String> is String";515516517Type gt_fc1 = fc1.getGenericType();518assert519gt_fc1 == C0.class :520" Type of C2.fc1 should be C0";521522Type gt_fi = fi.getGenericType();523assert524gt_fi == int.class:525" Type of C2.fi should be int";526527}528529static void testConstructors() throws NoSuchMethodException {530System.out.println("testing constructors");531Class[] params1 = new Class[1];532params1[0] = C0.class;533Constructor<C2> con = cls.getDeclaredConstructor(params1);534535Type[] pt_con = con.getGenericParameterTypes();536assert537pt_con.length == 1 :538"Constructor C0(T) should have one generic parameter type";539Type pt = pt_con[0];540assert541pt instanceof TypeVariable :542"The generic type of the parameter of C0(T2) is a type variable";543TypeVariable tv = (TypeVariable) pt;544assert545tv.getName().equals("T2") :546"The name of the type parameter of C2 is T2, not " + tv.getName();547Type[] bs = tv.getBounds();548assert549bs.length == 1 :550"T should have one bound";551552553Type[] et_con = con.getGenericExceptionTypes();554assert555et_con.length == 0 :556"Constructor C2(T2) should have no generic exception types";557558TypeVariable[] tv_con = con.getTypeParameters();559assert560tv_con.length == 0 :561"Constructor C2(T2) should have no type parameters";562563564Class[] params2 = new Class[1];565params2[0] = Object.class;566con = cls.getDeclaredConstructor(params2);567568pt_con = con.getGenericParameterTypes();569assert570pt_con.length == 1 :571"Constructor C0(T) should have one generic parameter type";572pt = pt_con[0];573assert574pt instanceof TypeVariable :575"The generic type of the parameter of C2(T) is a type variable";576tv = (TypeVariable) pt;577assert578tv.getName().equals("T") :579"The name of the type parameter of C2 is T, not " + tv.getName();580bs = tv.getBounds();581assert582bs.length == 1 :583"T should have one bound";584585586et_con = con.getGenericExceptionTypes();587assert588et_con.length == 0 :589"Constructor C2(T) should have no generic exception types";590591tv_con = con.getTypeParameters();592assert593tv_con.length == 1 :594"Constructor C2(T) should have one type parameter";595596Class[] params3 = new Class[3];597params3[0] = Object.class;598params3[1] = Object.class;599params3[2] = Object.class;600601con = cls.getDeclaredConstructor(params3);602603pt_con = con.getGenericParameterTypes();604assert605pt_con.length == 3 :606"Constructor C2(T1,T2,T4) should have three generic parameter types";607pt = pt_con[0];608assert609pt instanceof TypeVariable :610"The generic type of the first parameter of C2(T1,T2,T4) is a type variable";611tv = (TypeVariable) pt;612assert613tv.getName().equals("T1") :614"The name of the type parameter of C2(T1,T2,T4) is T1, not " + tv.getName();615bs = tv.getBounds();616assert617bs.length == 1 :618"T should have one bound";619620621et_con = con.getGenericExceptionTypes();622assert623et_con.length == 0 :624"Constructor C2(T1,T2,T4) should have no generic exception types";625626tv_con = con.getTypeParameters();627assert628tv_con.length == 4 :629"Constructor C2(T1,T2,T4) should have four type parameters";630631Class[] params4 = new Class[0];632con = cls.getDeclaredConstructor(params4);633634pt_con = con.getGenericParameterTypes();635assert636pt_con.length == 0 :637"Constructor C2() should have no generic parameter types";638639640et_con = con.getGenericExceptionTypes();641assert642et_con.length == 1 :643"Constructor C2() should have one generic exception type";644645tv_con = con.getTypeParameters();646assert647tv_con.length == 0 :648"Constructor C2() should have no type parameters";649650651}652}653654655