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 17 package tests.api.java.lang.reflect; 18 19 20 import java.lang.reflect.Method; 21 import java.lang.reflect.Type; 22 import java.lang.reflect.TypeVariable; 23 24 25 /** 26 * Tests unbounded type parameters declared on methods. 27 */ 28 public class GenericMethodsTests extends GenericReflectionTestsBase{ 29 30 static class GenericMethods { 31 32 public <T> void noParamNoReturn() {} 33 34 public <T> void paramNoReturn(T param) {} 35 36 @SuppressWarnings("unchecked") 37 public <T> T noParamReturn() { return (T) new Object(); } 38 39 public <T> T paramReturn(T param) {return param;} 40 } 41 42 private static Class<? extends GenericMethods> clazz = GenericMethodsTests.GenericMethods.class; 43 44 /** 45 * Tests that there are no Type Parameters on the Class itself. 46 */ 47 public void testGenericMethods() { 48 assertLenghtZero(clazz.getTypeParameters()); 49 } 50 51 /** 52 * Tests whether the specified method declares a type parameter T. 53 * @param method the method 54 */ 55 private void checkTypeParameter(Method method) { 56 TypeVariable<Method> typeParameter = getTypeParameter(method); 57 assertEquals("T", typeParameter.getName()); 58 assertEquals(method, typeParameter.getGenericDeclaration()); 59 } 60 61 /** 62 * Tests whether the specified method declares a parameter with the 63 * type of the type parameter. 64 * @param method the method 65 */ 66 private void checkParameterType(Method method) { 67 TypeVariable<Method> typeParameter = getTypeParameter(method); 68 assertLenghtOne(method.getGenericParameterTypes()); 69 Type genericParameterType = method.getGenericParameterTypes()[0]; 70 assertEquals(typeParameter, genericParameterType); 71 assertInstanceOf(TypeVariable.class, genericParameterType); 72 assertEquals(method, ((TypeVariable<?>) genericParameterType).getGenericDeclaration()); 73 } 74 75 /** 76 * Tests whether the type of the return type is the declared type parameter. 77 * @param method the declaring method 78 */ 79 private void checkReturnType(Method method) { 80 TypeVariable<Method> typeParameter = getTypeParameter(method); 81 Type genericReturnType = method.getGenericReturnType(); 82 assertEquals(typeParameter, genericReturnType); 83 assertInstanceOf(TypeVariable.class, genericReturnType); 84 assertEquals(method, ((TypeVariable<?>) genericReturnType).getGenericDeclaration()); 85 } 86 public void testNoParamNoReturn() throws Exception { 87 Method method = clazz.getMethod("noParamNoReturn"); 88 checkTypeParameter(method); 89 } 90 91 public void testParamNoReturn() throws Exception { 92 Method method = clazz.getMethod("paramNoReturn", Object.class); 93 checkTypeParameter(method); 94 checkParameterType(method); 95 } 96 97 public void testNoParamReturn() throws Exception { 98 Method method = clazz.getMethod("noParamReturn"); 99 checkTypeParameter(method); 100 assertLenghtZero(method.getGenericParameterTypes()); 101 checkReturnType(method); 102 } 103 public void testParamReturn() throws Exception { 104 Method method = clazz.getMethod("paramReturn", Object.class); 105 checkTypeParameter(method); 106 checkParameterType(method); 107 checkReturnType(method); 108 } 109 public void testIndependencyOfMethodTypeParameters() throws Exception { 110 Method method0 = clazz.getMethod("paramNoReturn", Object.class); 111 TypeVariable<Method> typeParameter0 = method0.getTypeParameters()[0]; 112 113 Method method1 = clazz.getMethod("noParamNoReturn"); 114 TypeVariable<Method> typeParameter1 = method1.getTypeParameters()[0]; 115 116 //Generic method type parameters NAMES are equal 117 assertEquals(typeParameter0.getName(), typeParameter1.getName()); 118 //Generic method type PARAMETERS are not equal 119 assertNotEquals(typeParameter0, typeParameter1); 120 } 121 } 122