1 /*
2  * Copyright (c) 2003, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * 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 TestN1.java
30 * @run main/othervm -ea TestN1
31 */
32 
33 
34 import java.lang.reflect.*;
35 
36 
37 class N1<T1, T2> {
38 
39     public Inner1 i1;
40     public Inner2 i2;
41     public Inner2<? super Character> i2sc;
42 
43     public class Inner1 {
44 
45     }
46 
47     public class Inner2<T1> {
48         public boolean x;
49         public byte b;
50         public short s;
51         public char c;
52         public int i;
53         public long l;
54         public float f;
55         public double d;
56 
57         public boolean[] xa;
58         public byte[] ba;
59         public short[] sa;
60         public char[] ca;
61         public int[] ia;
62         public long[] la;
63         public float[] fa;
64         public double[] da;
65     }
66 
67     public class Inner3<X1, X2, X3> {
68         X1 x1;
69 
Inner3(X1 x1, X2 x2, X3 x3, T1 t1, T2 t2)70         Inner3(X1 x1, X2 x2, X3 x3, T1 t1, T2 t2) {}
71 
Inner3(T t, R r, S s, X1 x1)72         <T, R, S> Inner3(T t, R r, S s, X1 x1) {}
73 
shazam(boolean b, short s, int[] ia, Object[] oa, Inner1 i1, Inner1 i1a, InnerInner<String, Inner3<Object, String, Object[]>> ii)74         int shazam(boolean b, short s, int[] ia, Object[] oa, Inner1 i1,
75                    Inner1 i1a, InnerInner<String,
76                    Inner3<Object, String, Object[]>> ii)
77         { return 3;}
78 
79         public class InnerInner<T2, X2> {
80 
81             boolean b;
82             Inner2<X2> i2x;
83 
foo(X3 x3)84             void foo(X3 x3){}
bar(X1 x1, X3[] x3, T1 t1)85             <X3> X3[] bar(X1 x1, X3[] x3, T1 t1) { return x3;}
baz(N1<X1, X2> n1)86             N1<X1, X2> baz(N1<X1, X2> n1) { return n1;}
bam(N1<T1, X2> n1)87             N1<?, ?> bam(N1<T1, X2> n1) { return n1;}
boom(N1<T1, X2> n1)88             N1<? extends T1, ?> boom(N1<T1, X2> n1) { return n1;}
89 
90         }
91 
92     }
93 
94 
95 
96 
97 }
98 
99 
100 public class TestN1 {
101 
102     static Class<N1> cls = N1.class;
103 
104 
main(String[] args)105     public static void main(String[] args) throws Throwable {
106         testTypeParameters();
107         testInner1();
108         testInner2();
109         testInner3();
110     }
111 
112 
testTypeParameters()113     static void testTypeParameters() {
114 
115         System.out.println("testing type parameters");
116         TypeVariable[] tvs = cls.getTypeParameters();
117         assert
118             tvs.length == 2 :
119             "N1 should have two type parameters";
120     }
121 
122 
testInner1()123     static void testInner1() {
124         System.out.println("testing non-generic inner class");
125         Class in1 = N1.Inner1.class;
126 
127         TypeVariable[] tvs = in1.getTypeParameters();
128         assert
129             tvs.length == 0 :
130             "N1.Inner2 should have no type parameters";
131 
132     }
133 
testInner2()134     static void testInner2() throws NoSuchFieldException {
135         System.out.println("testing generic inner class 1");
136         Class in1 = N1.Inner2.class;
137 
138         TypeVariable[] tvs = in1.getTypeParameters();
139         assert
140             tvs.length == 1 :
141             "N1.Inner2 should have one type parameter";
142 
143 
144         assert
145             in1.getField("x").getGenericType() == boolean.class :
146             "Type of Inner2.x should be boolean";
147 
148         assert
149             in1.getField("b").getGenericType() == byte.class :
150             "Type of Inner2.b should be byte";
151         assert
152             in1.getField("s").getGenericType() == short.class :
153             "Type of Inner2.s should be short";
154         assert
155             in1.getField("c").getGenericType() == char.class :
156             "Type of Inner2.x should be char";
157         assert
158             in1.getField("i").getGenericType() == int.class :
159             "Type of Inner2.i should be int";
160         assert
161             in1.getField("l").getGenericType() == long.class :
162             "Type of Inner2.l should be long";
163         assert
164             in1.getField("f").getGenericType() == float.class :
165             "Type of Inner2.f should be float";
166         assert
167             in1.getField("d").getGenericType() == double.class :
168             "Type of Inner2.d should be double";
169 
170         assert
171             in1.getField("xa").getGenericType() == boolean[].class :
172             "Type of Inner2.xa should be boolean[]";
173 
174         assert
175             in1.getField("ba").getGenericType() == byte[].class :
176             "Type of Inner2.ba should be byte[]";
177         assert
178             in1.getField("sa").getGenericType() == short[].class :
179             "Type of Inner2.sa should be short[]";
180         assert
181             in1.getField("ca").getGenericType() == char[].class :
182             "Type of Inner2.xa should be char[]";
183         assert
184             in1.getField("ia").getGenericType() == int[].class :
185             "Type of Inner2.ia should be int[]";
186         assert
187             in1.getField("la").getGenericType() == long[].class :
188             "Type of Inner2.la should be long[]";
189         assert
190             in1.getField("fa").getGenericType() == float[].class :
191             "Type of Inner2.fa should be float[]";
192         assert
193             in1.getField("da").getGenericType() == double[].class :
194             "Type of Inner2.da should be double[]";
195     }
196 
197 
testInner3()198     static void testInner3() {
199         System.out.println("testing generic inner class 3");
200         Class in1 = N1.Inner3.class;
201 
202         TypeVariable[] tvs = in1.getTypeParameters();
203         assert
204             tvs.length == 3 :
205             "N1.Inner2 should have three type parameters";
206     }
207 }
208