summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/tests/api/java/lang/reflect/TypeVariableTest.java
blob: cc22852bb7ffe9976e40a8b1d0beedf3b81d45b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tests.api.java.lang.reflect;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

/**
 * Tests type variables and their properties.
 */
public class TypeVariableTest extends GenericReflectionTestsBase {

    static class A<T>{}
    public void testSimpleTypeVariableOnClass(){
        Class<? extends A> clazz = A.class;
        TypeVariable[] typeParameters = clazz.getTypeParameters();
        assertLenghtOne(typeParameters);
        TypeVariable<Class> typeVariable = typeParameters[0];
        assertEquals(clazz, typeVariable.getGenericDeclaration());
        assertEquals("T", typeVariable.getName());
        Type[] bounds = typeVariable.getBounds();
        assertLenghtOne(bounds);
        assertEquals(Object.class, bounds[0]);
    }

    static class B{
        <T> void b(){};
    }
    public void testSimpleTypeVariableOnMethod() throws Exception{
        Class<? extends B> clazz = B.class;
        Method method = clazz.getDeclaredMethod("b");
        TypeVariable<Method>[] typeParameters = method.getTypeParameters();
        assertLenghtOne(typeParameters);
        TypeVariable<Method> typeVariable = typeParameters[0];
        assertEquals(method, typeVariable.getGenericDeclaration());
        assertEquals("T", typeVariable.getName());
        Type[] bounds = typeVariable.getBounds();
        assertLenghtOne(bounds);
        assertEquals(Object.class, bounds[0]);
    }

    static class C {
        <T>C(){}
    }
    public void testSimpleTypeVariableOnConstructor() throws Exception{
        Class<? extends C> clazz = C.class;
        Constructor<?> constructor = clazz.getDeclaredConstructor();
        TypeVariable<?>[] typeParameters = constructor.getTypeParameters();
        assertLenghtOne(typeParameters);
        TypeVariable<?> typeVariable = typeParameters[0];
        assertEquals(constructor, typeVariable.getGenericDeclaration());
        assertEquals("T", typeVariable.getName());
        Type[] bounds = typeVariable.getBounds();
        assertLenghtOne(bounds);
        assertEquals(Object.class, bounds[0]);
    }

    static class D<Q,R,S>{}
    public void testMultipleTypeVariablesOnClass() throws Exception {
        Class<? extends D> clazz = D.class;
        TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
        assertEquals(3, typeParameters.length);
        assertEquals("Q", typeParameters[0].getName());
        assertEquals(clazz, typeParameters[0].getGenericDeclaration());

        assertEquals("R", typeParameters[1].getName());
        assertEquals(clazz, typeParameters[1].getGenericDeclaration());

        assertEquals("S", typeParameters[2].getName());
        assertEquals(clazz, typeParameters[2].getGenericDeclaration());

    }

    static class E {
        <Q,R,S> void e(){}
    }
    public void testMultipleTypeVariablesOnMethod() throws Exception {
        Class<? extends E> clazz = E.class;
        Method method = clazz.getDeclaredMethod("e");

        TypeVariable<?>[] typeParameters = method.getTypeParameters();
        assertEquals(3, typeParameters.length);
        assertEquals("Q", typeParameters[0].getName());
        assertEquals(method, typeParameters[0].getGenericDeclaration());

        assertEquals("R", typeParameters[1].getName());
        assertEquals(method, typeParameters[1].getGenericDeclaration());

        assertEquals("S", typeParameters[2].getName());
        assertEquals(method, typeParameters[2].getGenericDeclaration());
    }

    static class F {
        <Q,R,S> F(){}
    }
    public void testMultipleTypeVariablesOnConstructor() throws Exception {
        Class<? extends F> clazz = F.class;
        Constructor<?> constructor = clazz.getDeclaredConstructor();

        TypeVariable<?>[] typeParameters = constructor.getTypeParameters();
        assertEquals(3, typeParameters.length);
        assertEquals("Q", typeParameters[0].getName());
        assertEquals(constructor, typeParameters[0].getGenericDeclaration());

        assertEquals("R", typeParameters[1].getName());
        assertEquals(constructor, typeParameters[1].getGenericDeclaration());

        assertEquals("S", typeParameters[2].getName());
        assertEquals(constructor, typeParameters[2].getGenericDeclaration());
    }

    static class G <T extends Number>{}

    public void testSingleBound() throws Exception {
        Class<? extends G> clazz = G.class;
        TypeVariable[] typeParameters = clazz.getTypeParameters();
        TypeVariable<Class> typeVariable = typeParameters[0];
        Type[] bounds = typeVariable.getBounds();
        assertLenghtOne(bounds);
        assertEquals(Number.class, bounds[0]);
    }

    static class H <T extends Number & Serializable >{}
    public void testMultipleBound() throws Exception {
        Class<? extends H> clazz = H.class;
        TypeVariable[] typeParameters = clazz.getTypeParameters();
        TypeVariable<Class> typeVariable = typeParameters[0];
        Type[] bounds = typeVariable.getBounds();
        assertEquals(2, bounds.length);
        assertEquals(Number.class, bounds[0]);
        assertEquals(Serializable.class, bounds[1]);
    }
}