Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/Generics/TestN1.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 TestN1.java29* @run main/othervm -ea TestN130*/313233import java.lang.reflect.*;343536class N1<T1, T2> {3738public Inner1 i1;39public Inner2 i2;40public Inner2<? super Character> i2sc;4142public class Inner1 {4344}4546public class Inner2<T1> {47public boolean x;48public byte b;49public short s;50public char c;51public int i;52public long l;53public float f;54public double d;5556public boolean[] xa;57public byte[] ba;58public short[] sa;59public char[] ca;60public int[] ia;61public long[] la;62public float[] fa;63public double[] da;64}6566public class Inner3<X1, X2, X3> {67X1 x1;6869Inner3(X1 x1, X2 x2, X3 x3, T1 t1, T2 t2) {}7071<T, R, S> Inner3(T t, R r, S s, X1 x1) {}7273int shazam(boolean b, short s, int[] ia, Object[] oa, Inner1 i1,74Inner1 i1a, InnerInner<String,75Inner3<Object, String, Object[]>> ii)76{ return 3;}7778public class InnerInner<T2, X2> {7980boolean b;81Inner2<X2> i2x;8283void foo(X3 x3){}84<X3> X3[] bar(X1 x1, X3[] x3, T1 t1) { return x3;}85N1<X1, X2> baz(N1<X1, X2> n1) { return n1;}86N1<?, ?> bam(N1<T1, X2> n1) { return n1;}87N1<? extends T1, ?> boom(N1<T1, X2> n1) { return n1;}8889}9091}9293949596}979899public class TestN1 {100101static Class<N1> cls = N1.class;102103104public static void main(String[] args) throws Throwable {105testTypeParameters();106testInner1();107testInner2();108testInner3();109}110111112static void testTypeParameters() {113114System.out.println("testing type parameters");115TypeVariable[] tvs = cls.getTypeParameters();116assert117tvs.length == 2 :118"N1 should have two type parameters";119}120121122static void testInner1() {123System.out.println("testing non-generic inner class");124Class in1 = N1.Inner1.class;125126TypeVariable[] tvs = in1.getTypeParameters();127assert128tvs.length == 0 :129"N1.Inner2 should have no type parameters";130131}132133static void testInner2() throws NoSuchFieldException {134System.out.println("testing generic inner class 1");135Class in1 = N1.Inner2.class;136137TypeVariable[] tvs = in1.getTypeParameters();138assert139tvs.length == 1 :140"N1.Inner2 should have one type parameter";141142143assert144in1.getField("x").getGenericType() == boolean.class :145"Type of Inner2.x should be boolean";146147assert148in1.getField("b").getGenericType() == byte.class :149"Type of Inner2.b should be byte";150assert151in1.getField("s").getGenericType() == short.class :152"Type of Inner2.s should be short";153assert154in1.getField("c").getGenericType() == char.class :155"Type of Inner2.x should be char";156assert157in1.getField("i").getGenericType() == int.class :158"Type of Inner2.i should be int";159assert160in1.getField("l").getGenericType() == long.class :161"Type of Inner2.l should be long";162assert163in1.getField("f").getGenericType() == float.class :164"Type of Inner2.f should be float";165assert166in1.getField("d").getGenericType() == double.class :167"Type of Inner2.d should be double";168169assert170in1.getField("xa").getGenericType() == boolean[].class :171"Type of Inner2.xa should be boolean[]";172173assert174in1.getField("ba").getGenericType() == byte[].class :175"Type of Inner2.ba should be byte[]";176assert177in1.getField("sa").getGenericType() == short[].class :178"Type of Inner2.sa should be short[]";179assert180in1.getField("ca").getGenericType() == char[].class :181"Type of Inner2.xa should be char[]";182assert183in1.getField("ia").getGenericType() == int[].class :184"Type of Inner2.ia should be int[]";185assert186in1.getField("la").getGenericType() == long[].class :187"Type of Inner2.la should be long[]";188assert189in1.getField("fa").getGenericType() == float[].class :190"Type of Inner2.fa should be float[]";191assert192in1.getField("da").getGenericType() == double[].class :193"Type of Inner2.da should be double[]";194}195196197static void testInner3() {198System.out.println("testing generic inner class 3");199Class in1 = N1.Inner3.class;200201TypeVariable[] tvs = in1.getTypeParameters();202assert203tvs.length == 3 :204"N1.Inner2 should have three type parameters";205}206}207208209