GenericMethodsTests.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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.TestInfo; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTarget; 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 @TestInfo( 54 level = TestLevel.PARTIAL, 55 purpose = "", 56 targets = { 57 @TestTarget( 58 methodName = "getTypeParameters", 59 methodArgs = {} 60 ) 61 }) 62 public void testGenericMethods() { 63 assertLenghtZero(clazz.getTypeParameters()); 64 } 65 66 /** 67 * Tests whether the specified method declares a type parameter T. 68 * @param method the method 69 */ 70 private void checkTypeParameter(Method method) { 71 TypeVariable<Method> typeParameter = getTypeParameter(method); 72 assertEquals("T", typeParameter.getName()); 73 assertEquals(method, typeParameter.getGenericDeclaration()); 74 } 75 76 /** 77 * Tests whether the specified method declares a parameter with the 78 * type of the type parameter. 79 * @param method the method 80 */ 81 private void checkParameterType(Method method) { 82 TypeVariable<Method> typeParameter = getTypeParameter(method); 83 assertLenghtOne(method.getGenericParameterTypes()); 84 Type genericParameterType = method.getGenericParameterTypes()[0]; 85 assertEquals(typeParameter, genericParameterType); 86 assertInstanceOf(TypeVariable.class, genericParameterType); 87 assertEquals(method, ((TypeVariable<?>) genericParameterType).getGenericDeclaration()); 88 } 89 90 /** 91 * Tests whether the type of the return type is the declared type parameter. 92 * @param method the declaring method 93 */ 94 private void checkReturnType(Method method) { 95 TypeVariable<Method> typeParameter = getTypeParameter(method); 96 Type genericReturnType = method.getGenericReturnType(); 97 assertEquals(typeParameter, genericReturnType); 98 assertInstanceOf(TypeVariable.class, genericReturnType); 99 assertEquals(method, ((TypeVariable<?>) genericReturnType).getGenericDeclaration()); 100 } 101 @TestInfo( 102 level = TestLevel.PARTIAL, 103 purpose = "", 104 targets = { 105 @TestTarget( 106 methodName = "getTypeParameters", 107 methodArgs = {} 108 ) 109 }) 110 public void testNoParamNoReturn() throws Exception { 111 Method method = clazz.getMethod("noParamNoReturn"); 112 checkTypeParameter(method); 113 } 114 @TestInfo( 115 level = TestLevel.PARTIAL, 116 purpose = "", 117 targets = { 118 @TestTarget( 119 methodName = "getTypeParameters", 120 methodArgs = {} 121 ), 122 @TestTarget( 123 methodName = "getGenericParameterTypes", 124 methodArgs = {} 125 ), 126 @TestTarget( 127 methodName = "getParameterTypes", 128 methodArgs = {} 129 ) 130 }) 131 public void testParamNoReturn() throws Exception { 132 Method method = clazz.getMethod("paramNoReturn", Object.class); 133 checkTypeParameter(method); 134 checkParameterType(method); 135 } 136 @TestInfo( 137 level = TestLevel.PARTIAL, 138 purpose = "", 139 targets = { 140 @TestTarget( 141 methodName = "getGenericParameterTypes", 142 methodArgs = {} 143 ) 144 }) 145 public void testNoParamReturn() throws Exception { 146 Method method = clazz.getMethod("noParamReturn"); 147 checkTypeParameter(method); 148 assertLenghtZero(method.getGenericParameterTypes()); 149 checkReturnType(method); 150 } 151 @TestInfo( 152 level = TestLevel.PARTIAL, 153 purpose = "", 154 targets = { 155 @TestTarget( 156 methodName = "getTypeParameters", 157 methodArgs = {} 158 ), 159 @TestTarget( 160 methodName = "getParameterTypes", 161 methodArgs = {} 162 ) 163 }) 164 public void testParamReturn() throws Exception { 165 Method method = clazz.getMethod("paramReturn", Object.class); 166 checkTypeParameter(method); 167 checkParameterType(method); 168 checkReturnType(method); 169 } 170 @TestInfo( 171 level = TestLevel.PARTIAL, 172 purpose = "", 173 targets = { 174 @TestTarget( 175 methodName = "getTypeParameters", 176 methodArgs = {} 177 ) 178 }) 179 public void testIndependencyOfMethodTypeParameters() throws Exception { 180 Method method0 = clazz.getMethod("paramNoReturn", Object.class); 181 TypeVariable<Method> typeParameter0 = method0.getTypeParameters()[0]; 182 183 Method method1 = clazz.getMethod("noParamNoReturn"); 184 TypeVariable<Method> typeParameter1 = method1.getTypeParameters()[0]; 185 186 //Generic method type parameters NAMES are equal 187 assertEquals(typeParameter0.getName(), typeParameter1.getName()); 188 //Generic method type PARAMETERS are not equal 189 assertNotEquals(typeParameter0, typeParameter1); 190 } 191} 192