Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Generics/TestC1.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 TestC1.java29* @run main/othervm -ea TestC130*/313233import java.lang.reflect.*;343536abstract class C1<T> {3738public T ft;39public C1<T> fc1t;40public C1 fc1;4142public C1(T t) {}4344public abstract C1<T> mc1t(T t, C1<T> c1t, C1 c1);4546public abstract C1 mc1();4748public abstract T mt(T t);4950}5152public class TestC1 {5354static Class<C1> cls = C1.class;55static {56TestC1.class.getClassLoader().setDefaultAssertionStatus(true);57}58596061public static void main(String[] args) throws Throwable {62testSuperclass();63testSuperInterfaces();64testTypeParameters();65testMethods();66testConstructor();67testFields();68}6970static void testSuperclass() {71System.out.println("testing superclass");72Type sc = cls.getGenericSuperclass();73assert74(sc == Object.class) :75"The generic superclass of C1 should be Object";76}7778static void testSuperInterfaces() {79System.out.println("testing superinterfaces");80Type[] sis = cls.getGenericInterfaces();81assert82(sis.length == 0) :83"C1 should have no generic superinterfaces";84}8586static void testTypeParameters() {87System.out.println("testing type parameters");88TypeVariable[] tvs = cls.getTypeParameters();89assert90tvs.length == 1 :91"C1 should have one type parameter";92TypeVariable tv = tvs[0];93Type[] bs = tv.getBounds();94assert95bs.length == 1 :96"T should have one bound";97assert98bs[0] == Object.class :99"The default bound of a type variable should be Object";100}101102static void testMethods() throws NoSuchMethodException {103System.out.println("testing methods");104Class[] params1 = new Class[3];105params1[0] = Object.class;106params1[1] = cls;107params1[2] = cls;108109Class[] params3 = new Class[1];110params3[0] = Object.class;111112Method mc1t = cls.getMethod("mc1t", params1);113Method mc1 = cls.getMethod("mc1", new Class[0]);114Method mt = cls.getMethod("mt", params3);115116Type rt_mc1t = mc1t.getGenericReturnType();117Type rt_mc1 = mc1.getGenericReturnType();118Type rt_mt = mt.getGenericReturnType();119120Type[] pt_mc1t = mc1t.getGenericParameterTypes();121122assert123pt_mc1t.length == 3 :124"C1.mc1t has three parameters";125Type p1_mc1t = pt_mc1t[0];126assert p1_mc1t != null;127assert128p1_mc1t instanceof TypeVariable :129"Generic type of the 1st parameter of mc1t(T) is a type variable";130TypeVariable tv = (TypeVariable) p1_mc1t;131132assert133tv.getName().equals("T") :134"Name of 1st type parameter of mc1t is T, not " + tv.getName();135Type[] bs = tv.getBounds();136assert137bs.length == 1 :138"T should have one bound (mc1t)";139assert140bs[0] == Object.class :141"The bound of T should be Object (mc1t)";142143Type p2_mc1t = pt_mc1t[1];144145assert146p2_mc1t instanceof ParameterizedType :147"The type of parameter 2 of mc1t is a parameterized type";148ParameterizedType pt = (ParameterizedType) p2_mc1t;149assert150pt.getRawType() == cls :151"Type of parameter 2 of mc1t is instantiation of C1";152assert153pt.getOwnerType() == null :154"Type of parameter 2 of mc1t is has null owner";155156Type[] tas = pt.getActualTypeArguments();157assert158tas.length == 1 :159"The type of parameter 2 of mc1t has one type argument";160Type ta = tas[0];161162assert163ta instanceof TypeVariable :164"The actual type arg of C1<T> is a type variable (mc1t)";165tv = (TypeVariable) ta;166assert167tv.getName().equals("T") :168"mc1t: Name of the type arg of C1<T> is T, not " + tv.getName();169bs = tv.getBounds();170assert171bs.length == 1 :172"mc1t: The type argument of C1<T> should have one bound";173assert174bs[0] == Object.class :175"mc1t: The bound of the type arg of C1<T> should be Object";176177Type p3_mc1t = pt_mc1t[2];178179assert180p3_mc1t == cls :181"Type of parameter 3 of mc1t is C1";182183Type[] pt_mc1 = mc1.getGenericParameterTypes();184assert185pt_mc1.length == 0 :186"C1.mc1 has zero parameters";187188Type[] pt_mt = mt.getGenericParameterTypes();189assert190pt_mt.length == 1 :191"C1.mt has one parameter";192Type p_mt = pt_mt[0];193assert194p_mt instanceof TypeVariable :195"The generic type of the parameter of mt(T) is a type variable";196tv = (TypeVariable) p_mt;197assert198tv.getName().equals("T") :199"The name of the type parameter of mt is T, not " + tv.getName();200bs = tv.getBounds();201assert202bs.length == 1 :203"T should have one bound";204assert205bs[0] == Object.class :206"The bound of T should be Object";207208Type[] et_mc1t = mc1t.getGenericExceptionTypes();209assert210et_mc1t.length == 0 :211"Method C1.mc1t should have no generic exception types";212213Type[] et_mc1 = mc1.getGenericExceptionTypes();214assert215et_mc1.length == 0 :216"Method C1.mc1 should have no generic exception types";217218Type[] et_mt = mt.getGenericExceptionTypes();219220assert221et_mt.length == 0 :222"Method C1.mt should have no generic exception types";223224225TypeVariable[] tv_mc1t = mc1t.getTypeParameters();226assert227tv_mc1t.length == 0 :228"Method C1.mc1t should have no type parameters";229230TypeVariable[] tv_mc1 = mc1.getTypeParameters();231assert232tv_mc1.length == 0 :233"Method C1.mc1 should have no type parameters";234235TypeVariable[] tv_mt = mt.getTypeParameters();236assert237tv_mt.length == 0 :238"Method C1.mt should have no type parameters";239}240241242static void testFields() throws NoSuchFieldException{243System.out.println("testing fields");244Field ft = cls. getField("ft");245Field fc1t = cls. getField("fc1t");246Field fc1 = cls. getField("fc1");247248Type gt_ft = ft.getGenericType();249assert250gt_ft instanceof TypeVariable :251"The generic type of C1.ft is a type variable";252TypeVariable tv = (TypeVariable) gt_ft;253assert254tv.getName().equals("T") :255"The name of the type of ft is T, not " + tv.getName();256Type[] bs = tv.getBounds();257assert258bs.length == 1 :259"The type of ft should have one bound";260assert261bs[0] == Object.class :262"The bound of the type of ft should be Object";263264Type gt_fc1t = fc1t.getGenericType();265assert266gt_fc1t instanceof ParameterizedType :267"The generic type of C1.fc1t is a parameterized type";268ParameterizedType pt = (ParameterizedType) gt_fc1t;269assert270pt.getRawType() == cls :271"Type of C1.fc1t is instantiation of C1";272assert273pt.getOwnerType() == null :274"Type of C1.fc1t is has null owner";275Type[] tas = pt.getActualTypeArguments();276assert277tas.length == 1 :278"The type of fc1t has one type argument";279Type ta = tas[0];280281assert282ta instanceof TypeVariable :283"The actual type arg of C1<T> is a type variable";284tv = (TypeVariable) ta;285assert286tv.getName().equals("T") :287"The name of the type arg of C1<T> is T, not " + tv.getName();288bs = tv.getBounds();289assert290bs.length == 1 :291"The type argument of C1<T> should have one bound";292assert293bs[0] == Object.class :294"The bound of the type arg of C1<T> should be Object";295296Type gt_fc1 = fc1.getGenericType();297assert298gt_fc1 == cls :299" Type of C1.fc1 should be C1";300}301302static void testConstructor() throws NoSuchMethodException {303System.out.println("testing constructors");304Class[] params = new Class[1];305params[0] = Object.class;306Constructor<C1> con = cls.getDeclaredConstructor(params);307308Type[] pt_con = con.getGenericParameterTypes();309assert310pt_con.length == 1 :311"Constructor C1(T) should have one generic parameter type";312Type pt = pt_con[0];313assert314pt instanceof TypeVariable :315"The generic type of the parameter of C1(T) is a type variable";316TypeVariable tv = (TypeVariable) pt;317assert318tv.getName().equals("T") :319"The name of the type parameter of C is T, not " + tv.getName();320Type[] bs = tv.getBounds();321assert322bs.length == 1 :323"T should have one bound";324assert325bs[0] == Object.class :326"The bound of T should be Object";327328Type[] et_con = con.getGenericExceptionTypes();329assert330et_con.length == 0 :331"Constructor C1(T) should have no generic exception types";332333TypeVariable[] tv_con = con.getTypeParameters();334assert335tv_con.length == 0 :336"Constructor C1(T) should have no type parameters";337338}339}340341342