BoundedGenericMethodsTests.java revision 229e34b182b98e1dba15d3dc6341954986ae2b7a
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
19import java.lang.reflect.Method;
20import java.lang.reflect.Type;
21import java.lang.reflect.TypeVariable;
22
23/**
24 * Tests bounded type parameters declared on methods.
25 */
26public class BoundedGenericMethodsTests extends GenericReflectionTestsBase {
27    @SuppressWarnings("unchecked")
28    static class BoundedGenericMethods<S> {
29
30        public <T extends BoundedGenericMethods> void noParamNoReturn() {}
31        public <T extends BoundedGenericMethods> void paramNoReturn(T param) {}
32
33        public <T extends BoundedGenericMethods> T noParamReturn() {
34            return (T) new Object();
35        }
36        public <T extends BoundedGenericMethods> T paramReturn(T t) {
37            return t;
38        }
39    }
40    @SuppressWarnings("unchecked")
41    private static Class<? extends BoundedGenericMethods> clazz = BoundedGenericMethodsTests.BoundedGenericMethods.class;
42
43    /**
44     * Tests whether the type parameter is upper bounded by BoundedGenericMethods.
45     * <T extends BoundedGenericMethods>.
46     *
47     * @param method
48     *            the declaring method
49     */
50    private void checkBoundedTypeParameter(Method method) {
51        TypeVariable<Method> typeParameter = getTypeParameter(method);
52        assertEquals("T", typeParameter.getName());
53        assertEquals(method, typeParameter.getGenericDeclaration());
54
55        Type[] bounds = typeParameter.getBounds();
56        assertLenghtOne(bounds);
57        Type bound = bounds[0];
58        assertEquals(BoundedGenericMethods.class, bound);
59    }
60
61    /**
62     * Tests whether the specified method declares a parameter with the type of
63     * the type parameter.
64     *
65     * @param method
66     *            the declaring method
67     */
68    private void parameterType(Method method) {
69        TypeVariable<Method> typeParameter = getTypeParameter(method);
70        assertLenghtOne(method.getGenericParameterTypes());
71        Type genericParameterType = method.getGenericParameterTypes()[0];
72        assertEquals(typeParameter, genericParameterType);
73        assertTrue(genericParameterType instanceof TypeVariable);
74        TypeVariable<?> typeVariable = (TypeVariable<?>) genericParameterType;
75        assertEquals(method, typeVariable.getGenericDeclaration());
76
77        Type[] paramBounds = typeVariable.getBounds();
78        assertLenghtOne(paramBounds);
79        Type paramBound = paramBounds[0];
80        assertEquals(BoundedGenericMethods.class, paramBound);
81    }
82
83    @SuppressWarnings("unchecked")
84    private void checkReturnType(Method method) {
85        Type genericReturnType = method.getGenericReturnType();
86        assertEquals(getTypeParameter(method), genericReturnType);
87        assertTrue(genericReturnType instanceof TypeVariable);
88
89        TypeVariable<Method> returnTypeVariable = (TypeVariable<Method>) genericReturnType;
90        assertEquals(method, returnTypeVariable.getGenericDeclaration());
91
92        Type[] bounds = returnTypeVariable.getBounds();
93        assertLenghtOne(bounds);
94        Type bound = bounds[0];
95
96        assertEquals(BoundedGenericMethods.class, bound);
97    }
98
99
100
101    /**
102     * Tests that there are is one Type Parameter on the Class itself.
103     */
104    public void testBoundedGenericMethods() {
105        assertLenghtOne(clazz.getTypeParameters());
106    }
107    public void testNoParamNoReturn() throws SecurityException, NoSuchMethodException {
108        Method method = clazz.getMethod("noParamNoReturn");
109        checkBoundedTypeParameter(method);
110    }
111    public void testUnboundedParamNoReturn() throws SecurityException, NoSuchMethodException {
112        Method method = clazz.getMethod("paramNoReturn", BoundedGenericMethods.class);
113        checkBoundedTypeParameter(method);
114        parameterType(method);
115    }
116    public void testNoParamReturn() throws SecurityException, NoSuchMethodException {
117        Method method = clazz.getMethod("noParamReturn");
118        checkBoundedTypeParameter(method);
119        assertLenghtZero(method.getGenericParameterTypes());
120        checkReturnType(method);
121    }
122    public void testUnboundedParamReturn() throws SecurityException, NoSuchMethodException {
123        Method method = clazz.getMethod("paramReturn", BoundedGenericMethods.class);
124        checkBoundedTypeParameter(method);
125        parameterType(method);
126        checkReturnType(method);
127    }
128}
129