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 org.apache.harmony.tests.java.lang.reflect; 18 19import java.lang.reflect.Field; 20import java.lang.reflect.GenericArrayType; 21import java.lang.reflect.ParameterizedType; 22import java.lang.reflect.Type; 23import java.lang.reflect.TypeVariable; 24 25/** 26 * Tests generic reflection on arrays with generic or parameterized component types. 27 */ 28public class GenericArrayTypeTest extends GenericReflectionTestsBase { 29 30 static class A<T> { 31 T[] array; 32 } 33 public void testGetGenericComponentType() throws Exception { 34 @SuppressWarnings("unchecked") 35 Class<? extends A> clazz = GenericArrayTypeTest.A.class; 36 Field field = clazz.getDeclaredField("array"); 37 Type genericType = field.getGenericType(); 38 assertInstanceOf(GenericArrayType.class, genericType); 39 assertEquals("T[]", genericType.toString()); 40 assertEquals("T[]", genericType.getTypeName()); 41 42 Type componentType = ((GenericArrayType) genericType).getGenericComponentType(); 43 assertEquals(getTypeParameter(clazz), componentType); 44 assertInstanceOf(TypeVariable.class, componentType); 45 TypeVariable<?> componentTypeVariable = (TypeVariable<?>) componentType; 46 assertEquals("T", componentTypeVariable.getName()); 47 assertEquals(clazz, componentTypeVariable.getGenericDeclaration()); 48 } 49 50 static class B<T> { 51 B<T>[] array; 52 } 53 public void testParameterizedComponentType() throws Exception { 54 @SuppressWarnings("unchecked") 55 Class<? extends B> clazz = GenericArrayTypeTest.B.class; 56 Field field = clazz.getDeclaredField("array"); 57 Type genericType = field.getGenericType(); 58 assertInstanceOf(GenericArrayType.class, genericType); 59 60 String bName = B.class.getName(); 61 assertEquals(bName + "<T>[]", genericType.toString()); 62 assertEquals(bName + "<T>[]", genericType.getTypeName()); 63 64 GenericArrayType arrayType = (GenericArrayType) genericType; 65 Type componentType = arrayType.getGenericComponentType(); 66 assertInstanceOf(ParameterizedType.class, componentType); 67 ParameterizedType parameterizedType = (ParameterizedType) componentType; 68 assertEquals(clazz, parameterizedType.getRawType()); 69 assertEquals(clazz.getTypeParameters()[0], parameterizedType.getActualTypeArguments()[0]); 70 } 71} 72