GenericMethodsTests.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package tests.api.java.lang.reflect;
18
19
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetNew;
23import dalvik.annotation.TestTargetClass;
24
25import java.lang.reflect.Method;
26import java.lang.reflect.Type;
27import java.lang.reflect.TypeVariable;
28
29
30/**
31 * Tests unbounded type parameters declared on methods.
32 */
33@TestTargetClass(Method.class)
34public class GenericMethodsTests extends GenericReflectionTestsBase{
35
36    static class GenericMethods {
37
38        public <T> void noParamNoReturn() {}
39
40        public <T> void paramNoReturn(T param) {}
41
42        @SuppressWarnings("unchecked")
43        public <T> T noParamReturn() { return (T) new Object(); }
44
45        public <T> T paramReturn(T param) {return param;}
46    }
47
48    private static Class<? extends GenericMethods> clazz = GenericMethodsTests.GenericMethods.class;
49
50    /**
51     * Tests that there are no Type Parameters on the Class itself.
52     */
53    @TestTargetNew(
54        level = TestLevel.PARTIAL_COMPLETE,
55        notes = "",
56        method = "getTypeParameters",
57        args = {}
58    )
59    public void testGenericMethods() {
60        assertLenghtZero(clazz.getTypeParameters());
61    }
62
63    /**
64     * Tests whether the specified method declares a type parameter T.
65     * @param method the method
66     */
67    private void checkTypeParameter(Method method) {
68        TypeVariable<Method> typeParameter = getTypeParameter(method);
69        assertEquals("T", typeParameter.getName());
70        assertEquals(method, typeParameter.getGenericDeclaration());
71    }
72
73    /**
74     * Tests whether the specified method declares a parameter with the
75     * type of the type parameter.
76     * @param method the method
77     */
78    private void checkParameterType(Method method) {
79        TypeVariable<Method> typeParameter = getTypeParameter(method);
80        assertLenghtOne(method.getGenericParameterTypes());
81        Type genericParameterType = method.getGenericParameterTypes()[0];
82        assertEquals(typeParameter, genericParameterType);
83        assertInstanceOf(TypeVariable.class, genericParameterType);
84        assertEquals(method, ((TypeVariable<?>) genericParameterType).getGenericDeclaration());
85    }
86
87    /**
88     * Tests whether the type of the return type is the declared type parameter.
89     * @param method the declaring method
90     */
91    private void checkReturnType(Method method) {
92        TypeVariable<Method> typeParameter = getTypeParameter(method);
93        Type genericReturnType = method.getGenericReturnType();
94        assertEquals(typeParameter, genericReturnType);
95        assertInstanceOf(TypeVariable.class, genericReturnType);
96        assertEquals(method, ((TypeVariable<?>) genericReturnType).getGenericDeclaration());
97    }
98    @TestTargetNew(
99        level = TestLevel.PARTIAL_COMPLETE,
100        notes = "",
101        method = "getTypeParameters",
102        args = {}
103    )
104    public void testNoParamNoReturn() throws Exception {
105        Method method = clazz.getMethod("noParamNoReturn");
106        checkTypeParameter(method);
107    }
108
109    @TestTargets({
110        @TestTargetNew(
111            level = TestLevel.PARTIAL_COMPLETE,
112            notes = "",
113            method = "getTypeParameters",
114            args = {}
115        ),
116        @TestTargetNew(
117            level = TestLevel.PARTIAL_COMPLETE,
118            notes = "",
119            method = "getGenericParameterTypes",
120            args = {}
121        ),
122        @TestTargetNew(
123            level = TestLevel.PARTIAL_COMPLETE,
124            notes = "",
125            method = "getParameterTypes",
126            args = {}
127        )
128    })
129    public void testParamNoReturn() throws Exception {
130        Method method = clazz.getMethod("paramNoReturn", Object.class);
131        checkTypeParameter(method);
132        checkParameterType(method);
133    }
134
135    @TestTargets({
136        @TestTargetNew(
137            level = TestLevel.PARTIAL_COMPLETE,
138            notes = "",
139            method = "getGenericParameterTypes",
140            args = {}
141        ),
142        @TestTargetNew(
143            level = TestLevel.PARTIAL_COMPLETE,
144            notes = "",
145            method = "getGenericReturnType",
146            args = {}
147        )
148    })
149    public void testNoParamReturn() throws Exception {
150        Method method = clazz.getMethod("noParamReturn");
151        checkTypeParameter(method);
152        assertLenghtZero(method.getGenericParameterTypes());
153        checkReturnType(method);
154    }
155    @TestTargets({
156        @TestTargetNew(
157            level = TestLevel.PARTIAL_COMPLETE,
158            notes = "",
159            method = "getTypeParameters",
160            args = {}
161        ),
162        @TestTargetNew(
163            level = TestLevel.PARTIAL_COMPLETE,
164            notes = "",
165            method = "getParameterTypes",
166            args = {}
167        ),
168        @TestTargetNew(
169            level = TestLevel.PARTIAL_COMPLETE,
170            notes = "",
171            method = "getGenericReturnType",
172            args = {}
173        )
174    })
175    public void testParamReturn() throws Exception {
176        Method method = clazz.getMethod("paramReturn", Object.class);
177        checkTypeParameter(method);
178        checkParameterType(method);
179        checkReturnType(method);
180    }
181    @TestTargetNew(
182        level = TestLevel.PARTIAL,
183        notes = "",
184        method = "getTypeParameters",
185        args = {}
186    )
187    public void testIndependencyOfMethodTypeParameters() throws Exception {
188        Method method0 = clazz.getMethod("paramNoReturn", Object.class);
189        TypeVariable<Method> typeParameter0 = method0.getTypeParameters()[0];
190
191        Method method1 = clazz.getMethod("noParamNoReturn");
192        TypeVariable<Method> typeParameter1 = method1.getTypeParameters()[0];
193
194        //Generic method type parameters NAMES are equal
195        assertEquals(typeParameter0.getName(), typeParameter1.getName());
196        //Generic method type PARAMETERS are not equal
197        assertNotEquals(typeParameter0, typeParameter1);
198    }
199}
200