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 Type componentType = ((GenericArrayType) genericType).getGenericComponentType(); 40 assertEquals(getTypeParameter(clazz), componentType); 41 assertInstanceOf(TypeVariable.class, componentType); 42 TypeVariable<?> componentTypeVariable = (TypeVariable<?>) componentType; 43 assertEquals("T", componentTypeVariable.getName()); 44 assertEquals(clazz, componentTypeVariable.getGenericDeclaration()); 45 } 46 47 static class B<T> { 48 B<T>[] array; 49 } 50 public void testParameterizedComponentType() throws Exception { 51 @SuppressWarnings("unchecked") 52 Class<? extends B> clazz = GenericArrayTypeTest.B.class; 53 Field field = clazz.getDeclaredField("array"); 54 Type genericType = field.getGenericType(); 55 56 assertInstanceOf(GenericArrayType.class, genericType); 57 GenericArrayType arrayType = (GenericArrayType) genericType; 58 Type componentType = arrayType.getGenericComponentType(); 59 assertInstanceOf(ParameterizedType.class, componentType); 60 ParameterizedType parameteriezdType = (ParameterizedType) componentType; 61 assertEquals(clazz, parameteriezdType.getRawType()); 62 assertEquals(clazz.getTypeParameters()[0], parameteriezdType.getActualTypeArguments()[0]); 63 } 64} 65