0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A* @test
0N/A* @bug 4891872
0N/A* @summary Some tests for the generic core reflection api.
0N/A* @author Gilad Bracha
1952N/A* @compile TestC2.java
0N/A* @run main/othervm -ea TestC2
0N/A*/
0N/A
0N/A
0N/Aimport java.lang.reflect.*;
0N/A
0N/A
0N/Aabstract class C0<T> {
0N/A
0N/A public T ft;
0N/A public C0<T> fc1t;
0N/A public C0 fc1;
0N/A
0N/A public C0(){}
0N/A public C0(T t) {}
0N/A
0N/A public abstract C0<T> mc1t(T t, C0<T> c1t, C0 c1);
0N/A
0N/A public abstract C0 mc1();
0N/A
0N/A public abstract T mt(T t);
0N/A
0N/A}
0N/A
0N/Ainterface I1<X1, X2> extends I3 {
0N/A
0N/A X1 foo(X2 x2);
0N/A}
0N/A
0N/Ainterface I2<E1, E2 extends Throwable, E3> {
0N/A
0N/A
0N/A E1 bar(E3 e3) throws E2;
0N/A
0N/A}
0N/A
0N/Ainterface I3 {
0N/A
0N/A
0N/A}
0N/A
0N/A
0N/Aabstract class C2<T1 extends C2<T1, T2, T3>, T2 extends C0<T2>,
0N/A T3 extends Throwable>
0N/A extends C0<T1>
0N/A implements I1<T1, T2>, I2<T1, T3, T2>, I3
0N/A{
0N/A
0N/A public T1 ft;
0N/A public C0<String> fc1t;
0N/A public C0 fc1;
0N/A public int fi;
0N/A
0N/A public C2(T2 t2) {}
0N/A public <T> C2(T t) {}
0N/A public <T1, T2, T3, T4> C2(T1 t1, T2 t2, T4 t4) {}
0N/A public C2() throws T3 {}
0N/A
0N/A public abstract <T> C0<T> mc1t(T3 t3, C0<T> c1t, C0 c1);
0N/A
0N/A public abstract <E, R> C0 mc1(E e);
0N/A
0N/A public abstract T1 mt(T2 t);
0N/A
0N/A}
0N/A
0N/Apublic class TestC2 {
0N/A
0N/A static Class<C2> cls = C2.class;
0N/A
0N/A
0N/A public static void main(String[] args) throws Throwable {
0N/A testSuperclass();
0N/A testSuperInterfaces();
0N/A testTypeParameters();
0N/A testMethods();
0N/A testConstructors();
0N/A testFields();
0N/A }
0N/A
0N/A static void testSuperclass() {
0N/A
0N/A System.out.println("testing superclass");
0N/A Type sc = cls.getGenericSuperclass();
0N/A assert
0N/A sc instanceof ParameterizedType :
0N/A "Superclass of C2 should be a parameterized type";
0N/A ParameterizedType psc = (ParameterizedType) sc;
0N/A assert
0N/A ((psc.getRawType() == C0.class) ) :
0N/A "The raw generic superclass of C2 should be C0";
0N/A
0N/A Type[] tas = psc.getActualTypeArguments();
0N/A assert
0N/A tas.length == 1 :
0N/A "Superclass of C2 should have one type argument";
0N/A
0N/A Type t = tas[0];
0N/A
0N/A assert
0N/A t instanceof TypeVariable :
0N/A "Type argument to superclass of C2 should be a type variable";
0N/A
0N/A TypeVariable tv = (TypeVariable) t;
0N/A assert
0N/A tv.getName().equals("T1") :
0N/A "Name of type argument to superclass of C2 should be T1";
0N/A Type[] bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T1 has one bound (superclass)";
0N/A t = bs[0];
0N/A assert
0N/A t instanceof ParameterizedType :
0N/A "Bound of C0 should be a parameterized type";
0N/A ParameterizedType pt = (ParameterizedType) t;
0N/A
0N/A assert
0N/A ((pt.getRawType() == C2.class) ) :
0N/A "The raw bound of T1 should be C2";
0N/A
0N/A tas = pt.getActualTypeArguments();
0N/A assert
0N/A tas.length == 3 :
0N/A "Bound of T1 should have three type arguments";
0N/A assert
0N/A tas[0] instanceof TypeVariable :
0N/A "First argument to bound of T1 is a type variable";
0N/A assert
0N/A tas[1] instanceof TypeVariable :
0N/A "Second argument to bound of T1 is a type variable";
0N/A assert
0N/A tas[2] instanceof TypeVariable :
0N/A "Third argument to bound of T1 is a type variable";
0N/A
0N/A TypeVariable tv1 = (TypeVariable) tas[0];
0N/A TypeVariable tv2 = (TypeVariable) tas[1];
0N/A TypeVariable tv3 = (TypeVariable) tas[2];
0N/A
0N/A assert
0N/A tv1.getName().equals("T1"):
0N/A "First type arg to bound of T1 is T1";
0N/A assert
0N/A tv2.getName().equals("T2"):
0N/A "Seconmd type arg to bound of T1 is T2";
0N/A assert
0N/A tv3.getName().equals("T3"):
0N/A "Third type arg to bound of T1 is T3";
0N/A
0N/A
0N/A }
0N/A
0N/A static void testSuperInterfaces() {
0N/A System.out.println("testing superinterfaces");
0N/A Type[] sis = cls.getGenericInterfaces();
0N/A assert
0N/A ((sis.length == 3)):
0N/A "C2 should have three generic superinterfaces";
0N/A
0N/A Type t = sis[0];
0N/A assert
0N/A t instanceof ParameterizedType :
0N/A "First superinterface of C2 should be a parameterized type";
0N/A ParameterizedType pt = (ParameterizedType) t;
0N/A assert
0N/A pt.getRawType() == I1.class :
0N/A "First super interface of C2 is instantiation of I1";
0N/A Type[] tas = pt.getActualTypeArguments();
0N/A assert
0N/A tas.length == 2 :
0N/A "First super interface of C2 has 2 type arguments";
0N/A
0N/A t = sis[1];
0N/A assert
0N/A t instanceof ParameterizedType :
0N/A "Second superinterface of C2 should be a parameterized type";
0N/A pt = (ParameterizedType) t;
0N/A assert
0N/A pt.getRawType() == I2.class :
0N/A "Second super interface of C2 is instantiation of I2";
0N/A tas = pt.getActualTypeArguments();
0N/A assert
0N/A tas.length == 3 :
0N/A "Second super interface of C2 has 3 type arguments";
0N/A
0N/A t = sis[2];
0N/A assert
0N/A t == I3.class :
0N/A "Third superinterface of C2 is I3";
0N/A
0N/A // Test interfaces themselves
0N/A
0N/A TypeVariable[] tvs = I1.class.getTypeParameters();
0N/A assert
0N/A tvs.length == 2 :
0N/A "I3 has two formal type parameters";
0N/A assert
0N/A tvs[0].getName().equals("X1") :
0N/A "Name of first formal type arg of I1 is X1";
0N/A assert
0N/A tvs[1].getName().equals("X2") :
0N/A "Name of second formal type arg of I1 is X2";
0N/A
0N/A assert
0N/A I1.class.getGenericSuperclass() == I1.class.getSuperclass() :
0N/A "The generic and non-generic superclasses of an interface must be the same";
0N/A sis = I1.class.getGenericInterfaces();
0N/A assert
0N/A sis.length == 1 :
0N/A "I1 has one generic superinterface";
0N/A assert
0N/A sis[0] == I3.class :
0N/A "Superinterface of I1 is I3";
0N/A
0N/A tvs = I2.class.getTypeParameters();
0N/A assert
0N/A tvs.length == 3 :
0N/A "I3 has three formal type parameters";
0N/A assert
0N/A tvs[0].getName().equals("E1") :
0N/A "Name of first formal type arg of I2 is E1";
0N/A assert
0N/A tvs[1].getName().equals("E2") :
0N/A "Name of second formal type arg of I2 is E2";
0N/A assert
0N/A tvs[2].getName().equals("E3") :
0N/A "Name of third formal type arg of I2 is E3";
0N/A
0N/A assert
0N/A I2.class.getGenericSuperclass() == I2.class.getSuperclass() :
0N/A "The generic and non-generic superclasses of an interface must be the same";
0N/A
0N/A tvs = I3.class.getTypeParameters();
0N/A assert
0N/A tvs.length == 0 :
0N/A "I3 has no formal type parameters";
0N/A
0N/A assert
0N/A I3.class.getGenericSuperclass() == I3.class.getSuperclass() :
0N/A "The generic and non-generic superclasses of an interface must be the same";
0N/A
0N/A
0N/A }
0N/A
0N/A static void testTypeParameters() {
0N/A System.out.println("testing type parameters");
0N/A TypeVariable[] tvs = cls.getTypeParameters();
0N/A assert
0N/A tvs.length == 3 :
0N/A "C2 should have three type parameters";
0N/A TypeVariable tv = tvs[0];
0N/A Type[] bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T1 should have one bound";
0N/A assert
0N/A bs[0] instanceof ParameterizedType :
0N/A "The bound of T1 should be a parameterized type";
0N/A
0N/A tv = tvs[1];
0N/A bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T2 should have one bound";
0N/A assert
0N/A bs[0] instanceof ParameterizedType :
0N/A "The bound of T2 should be a parameterized type";
0N/A
0N/A tv = tvs[2];
0N/A bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T3 should have one bound";
0N/A assert
0N/A bs[0] == Throwable.class :
0N/A "The bound of T3 should be Throwable";
0N/A }
0N/A
0N/A static void testMethods() throws NoSuchMethodException {
0N/A System.out.println("testing methods");
0N/A
0N/A
0N/A
0N/A Class[] params1 = new Class[3];
0N/A params1[0] = Throwable.class;
0N/A params1[1] = C0.class;
0N/A params1[2] = C0.class;
0N/A
0N/A Class[] params2 = new Class[1];
0N/A params2[0] = Object.class;
0N/A
0N/A Class[] params3 = new Class[1];
0N/A params3[0] = C0.class;
0N/A
0N/A Method mc1t = cls.getMethod("mc1t", params1);
0N/A Method mc1 = cls.getMethod("mc1", params2);
0N/A Method mt = cls.getMethod("mt", params3);
0N/A
0N/A Type rt_mc1t = mc1t.getGenericReturnType();
0N/A assert
0N/A rt_mc1t instanceof ParameterizedType :
0N/A "The return type of mc1t should be a parameterized type";
0N/A ParameterizedType pt = (ParameterizedType) rt_mc1t;
0N/A
0N/A assert
0N/A pt.getRawType() == C0.class :
0N/A "The raw return type of mc1t should be C0";
0N/A
0N/A Type[] tas = pt.getActualTypeArguments();
0N/A assert
0N/A tas.length == 1 :
0N/A "Return type of mc1t should have one type argument";
0N/A assert
0N/A tas[0] instanceof TypeVariable :
0N/A "Type argument of return type of mc1t is a type variable";
0N/A
0N/A Type rt_mc1 = mc1.getGenericReturnType();
0N/A assert
0N/A rt_mc1 == C0.class :
0N/A "Return type of mc1 is C0";
0N/A
0N/A Type rt_mt = mt.getGenericReturnType();
0N/A assert
0N/A rt_mt instanceof TypeVariable :
0N/A "Return type of mt is a type variable";
0N/A
0N/A Type[] pt_mc1t = mc1t.getGenericParameterTypes();
0N/A
0N/A assert
0N/A pt_mc1t.length == 3 :
0N/A "C0.mc1t has three parameters";
0N/A Type p1_mc1t = pt_mc1t[0];
0N/A assert p1_mc1t != null;
0N/A assert
0N/A p1_mc1t instanceof TypeVariable :
0N/A "Generic type of the 1st parameter of mc1t(T) is a type variable";
0N/A TypeVariable tv = (TypeVariable) p1_mc1t;
0N/A
0N/A assert
0N/A tv.getName().equals("T3") :
0N/A "Name of 1st type parameter of mc1t is T3, not " + tv.getName();
0N/A Type[] bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T3 should have one bound (mc1t)";
0N/A assert
0N/A bs[0] == Throwable.class :
0N/A "The bound of T3 should be Throwable(mc1t)";
0N/A
0N/A Type p2_mc1t = pt_mc1t[1];
0N/A assert
0N/A p2_mc1t instanceof ParameterizedType :
0N/A "The type of parameter 2 of mc1t is a parameterized type";
0N/A pt = (ParameterizedType) p2_mc1t;
0N/A assert
0N/A pt.getRawType() == C0.class :
0N/A "Type of parameter 2 of mc1t is instantiation of C0";
0N/A assert
0N/A pt.getOwnerType() == null :
0N/A "Type of parameter 2 of mc1t is has null owner";
0N/A
0N/A tas = pt.getActualTypeArguments();
0N/A assert
0N/A tas.length == 1 :
0N/A "The type of parameter 2 of mc1t has one type argument";
0N/A Type ta = tas[0];
0N/A
0N/A assert
0N/A ta instanceof TypeVariable :
0N/A "The actual type arg of C0<T> is a type variable (mc1t)";
0N/A tv = (TypeVariable) ta;
0N/A assert
0N/A tv.getName().equals("T") :
0N/A "mc1t: Name of the type arg of C0<T> is T, not " + tv.getName();
0N/A bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "mc1t: The type argument of C0<T> should have one bound";
0N/A assert
0N/A bs[0] == Object.class :
0N/A "mc1t: The bound of the type arg of C0<T> should be Object";
0N/A
0N/A Type p3_mc1t = pt_mc1t[2];
0N/A assert
0N/A p3_mc1t == C0.class :
0N/A "Type of parameter 3 of mc1t is C0";
0N/A
0N/A Type[] pt_mc1 = mc1.getGenericParameterTypes();
0N/A assert
0N/A pt_mc1.length == 1 :
0N/A "C2.mc1 has one parameter";
0N/A
0N/A Type[] pt_mt = mt.getGenericParameterTypes();
0N/A assert
0N/A pt_mt.length == 1 :
0N/A "C2.mt has one parameter";
0N/A Type p_mt = pt_mt[0];
0N/A assert
0N/A p_mt instanceof TypeVariable :
0N/A "The generic type of the parameter of mt(T) is a type variable";
0N/A tv = (TypeVariable) p_mt;
0N/A assert
0N/A tv.getName().equals("T2") :
0N/A "The name of the type parameter of mt is T2, not " + tv.getName();
0N/A bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T2 should have one bound";
0N/A assert
0N/A bs[0] instanceof ParameterizedType:
0N/A "The bound of T2 should be parameterized type";
0N/A
0N/A Type[] et_mc1t = mc1t.getGenericExceptionTypes();
0N/A assert
0N/A et_mc1t.length == 0 :
0N/A "Method C0.mc1t should have no generic exception types";
0N/A
0N/A Type[] et_mc1 = mc1.getGenericExceptionTypes();
0N/A assert
0N/A et_mc1.length == 0 :
0N/A "Method C0.mc1 should have no generic exception types";
0N/A
0N/A Type[] et_mt = mt.getGenericExceptionTypes();
0N/A assert
0N/A et_mt.length == 0 :
0N/A "Method C0.mt should have no generic exception types";
0N/A
0N/A
0N/A TypeVariable[] tv_mc1t = mc1t.getTypeParameters();
0N/A assert
0N/A tv_mc1t.length == 1 :
0N/A "Method C2.mc1t should have one type parameter";
0N/A
0N/A TypeVariable[] tv_mc1 = mc1.getTypeParameters();
0N/A assert
0N/A tv_mc1.length == 2 :
0N/A "Method C2.mc1 should have two type parameters";
0N/A
0N/A TypeVariable[] tv_mt = mt.getTypeParameters();
0N/A assert
0N/A tv_mt.length == 0 :
0N/A "Method C2.mt should have no type parameters";
0N/A }
0N/A
0N/A
0N/A static void testFields() throws NoSuchFieldException{
0N/A System.out.println("testing fields");
0N/A Field ft = cls. getField("ft");
0N/A Field fc1t = cls. getField("fc1t");
0N/A Field fc1 = cls. getField("fc1");
0N/A Field fi = cls. getField("fi");
0N/A
0N/A Type gt_ft = ft.getGenericType();
0N/A assert
0N/A gt_ft instanceof TypeVariable :
0N/A "The generic type of C0.ft is a type variable";
0N/A TypeVariable tv = (TypeVariable) gt_ft;
0N/A assert
0N/A tv.getName().equals("T1") :
0N/A "The name of the type of ft is T1, not " + tv.getName();
0N/A Type[] bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "The type of ft should have one bound";
0N/A
0N/A
0N/A Type gt_fc1t = fc1t.getGenericType();
0N/A assert
0N/A gt_fc1t instanceof ParameterizedType :
0N/A "The generic type of C0.fc1t is a parameterized type";
0N/A ParameterizedType pt = (ParameterizedType) gt_fc1t;
0N/A assert
0N/A pt.getRawType() == C0.class :
0N/A "Type of C2.fc1t is an instantiation of C0";
0N/A assert
0N/A pt.getOwnerType() == null :
0N/A "Type of C2.fc1t is has null owner";
0N/A Type[] tas = pt.getActualTypeArguments();
0N/A assert
0N/A tas.length == 1 :
0N/A "The type of fc1t has one type argument";
0N/A Type ta = tas[0];
0N/A
0N/A assert
0N/A ta == String.class :
0N/A "The actual type arg of C0<String> is String";
0N/A
0N/A
0N/A Type gt_fc1 = fc1.getGenericType();
0N/A assert
0N/A gt_fc1 == C0.class :
0N/A " Type of C2.fc1 should be C0";
0N/A
0N/A Type gt_fi = fi.getGenericType();
0N/A assert
0N/A gt_fi == int.class:
0N/A " Type of C2.fi should be int";
0N/A
0N/A }
0N/A
0N/A static void testConstructors() throws NoSuchMethodException {
0N/A System.out.println("testing constructors");
0N/A Class[] params1 = new Class[1];
0N/A params1[0] = C0.class;
0N/A Constructor<C2> con = cls.getDeclaredConstructor(params1);
0N/A
0N/A Type[] pt_con = con.getGenericParameterTypes();
0N/A assert
0N/A pt_con.length == 1 :
0N/A "Constructor C0(T) should have one generic parameter type";
0N/A Type pt = pt_con[0];
0N/A assert
0N/A pt instanceof TypeVariable :
0N/A "The generic type of the parameter of C0(T2) is a type variable";
0N/A TypeVariable tv = (TypeVariable) pt;
0N/A assert
0N/A tv.getName().equals("T2") :
0N/A "The name of the type parameter of C2 is T2, not " + tv.getName();
0N/A Type[] bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T should have one bound";
0N/A
0N/A
0N/A Type[] et_con = con.getGenericExceptionTypes();
0N/A assert
0N/A et_con.length == 0 :
0N/A "Constructor C2(T2) should have no generic exception types";
0N/A
0N/A TypeVariable[] tv_con = con.getTypeParameters();
0N/A assert
0N/A tv_con.length == 0 :
0N/A "Constructor C2(T2) should have no type parameters";
0N/A
0N/A
0N/A Class[] params2 = new Class[1];
0N/A params2[0] = Object.class;
0N/A con = cls.getDeclaredConstructor(params2);
0N/A
0N/A pt_con = con.getGenericParameterTypes();
0N/A assert
0N/A pt_con.length == 1 :
0N/A "Constructor C0(T) should have one generic parameter type";
0N/A pt = pt_con[0];
0N/A assert
0N/A pt instanceof TypeVariable :
0N/A "The generic type of the parameter of C2(T) is a type variable";
0N/A tv = (TypeVariable) pt;
0N/A assert
0N/A tv.getName().equals("T") :
0N/A "The name of the type parameter of C2 is T, not " + tv.getName();
0N/A bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T should have one bound";
0N/A
0N/A
0N/A et_con = con.getGenericExceptionTypes();
0N/A assert
0N/A et_con.length == 0 :
0N/A "Constructor C2(T) should have no generic exception types";
0N/A
0N/A tv_con = con.getTypeParameters();
0N/A assert
0N/A tv_con.length == 1 :
0N/A "Constructor C2(T) should have one type parameter";
0N/A
0N/A Class[] params3 = new Class[3];
0N/A params3[0] = Object.class;
0N/A params3[1] = Object.class;
0N/A params3[2] = Object.class;
0N/A
0N/A con = cls.getDeclaredConstructor(params3);
0N/A
0N/A pt_con = con.getGenericParameterTypes();
0N/A assert
0N/A pt_con.length == 3 :
0N/A "Constructor C2(T1,T2,T4) should have three generic parameter types";
0N/A pt = pt_con[0];
0N/A assert
0N/A pt instanceof TypeVariable :
0N/A "The generic type of the first parameter of C2(T1,T2,T4) is a type variable";
0N/A tv = (TypeVariable) pt;
0N/A assert
0N/A tv.getName().equals("T1") :
0N/A "The name of the type parameter of C2(T1,T2,T4) is T1, not " + tv.getName();
0N/A bs = tv.getBounds();
0N/A assert
0N/A bs.length == 1 :
0N/A "T should have one bound";
0N/A
0N/A
0N/A et_con = con.getGenericExceptionTypes();
0N/A assert
0N/A et_con.length == 0 :
0N/A "Constructor C2(T1,T2,T4) should have no generic exception types";
0N/A
0N/A tv_con = con.getTypeParameters();
0N/A assert
0N/A tv_con.length == 4 :
0N/A "Constructor C2(T1,T2,T4) should have four type parameters";
0N/A
0N/A Class[] params4 = new Class[0];
0N/A con = cls.getDeclaredConstructor(params4);
0N/A
0N/A pt_con = con.getGenericParameterTypes();
0N/A assert
0N/A pt_con.length == 0 :
0N/A "Constructor C2() should have no generic parameter types";
0N/A
0N/A
0N/A et_con = con.getGenericExceptionTypes();
0N/A assert
0N/A et_con.length == 1 :
0N/A "Constructor C2() should have one generic exception type";
0N/A
0N/A tv_con = con.getTypeParameters();
0N/A assert
0N/A tv_con.length == 0 :
0N/A "Constructor C2() should have no type parameters";
0N/A
0N/A
0N/A }
0N/A}