Home | History | Annotate | Download | only in reflect
      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 libcore.java.lang.reflect;
     18 
     19 import java.lang.reflect.Constructor;
     20 import java.lang.reflect.Method;
     21 import java.lang.reflect.ParameterizedType;
     22 import java.lang.reflect.Type;
     23 import java.lang.reflect.TypeVariable;
     24 import org.apache.harmony.tests.java.lang.reflect.GenericReflectionTestsBase;
     25 
     26 
     27 /**
     28  * Tests type parameters declared on classes.
     29  */
     30 public class OldGenericTypesTest extends GenericReflectionTestsBase {
     31 
     32     static class GenericType<T>{
     33         T methodGenericType(T t){ return t;}
     34         @SuppressWarnings("hiding")
     35         <T> T hidingMethodGenericType(T t){ return t;}
     36         static <T> T staticMethodGenericType(T t){ return t;}
     37     }
     38 
     39     static class MultipleBoundedGenericTypes<T,S extends T>{
     40         void multipleBoundedGenericTypesTS(T t, S s){}
     41     }
     42 
     43     static class SimpleInheritance <T> extends GenericType<T>{}
     44 
     45     static class ConstructorGenericType<T>{
     46         ConstructorGenericType(T t){}
     47     }
     48 
     49     static class InnerClassTest<T>{
     50         class InnerClass {
     51             InnerClass(T t) {}
     52             void innerMethod(T t){}
     53         }
     54     }
     55 
     56     static class ExceptionTest<T extends Exception>{
     57         void exceptionTest() throws T{}
     58         class InnerClass{
     59             void innerExceptionTest() throws T{}
     60         }
     61     }
     62 
     63     static interface InterfaceTest<T>{}
     64     @SuppressWarnings("unchecked")
     65     public void testConstructorGenericType() throws Exception {
     66         Class<? extends ConstructorGenericType> clazz = ConstructorGenericType.class;
     67         TypeVariable<Class> typeVariable = getTypeParameter(clazz);
     68         Constructor<?> constructor = clazz.getDeclaredConstructor(Object.class);
     69         Type[] genericParameterTypes = constructor.getGenericParameterTypes();
     70         assertLenghtOne(genericParameterTypes);
     71         Type parameterType = genericParameterTypes[0];
     72 
     73         assertEquals(typeVariable, parameterType);
     74     }
     75     @SuppressWarnings("unchecked")
     76     public void testStaticMethodGenericType() throws Exception {
     77         Class<? extends GenericType> clazz = GenericType.class;
     78         TypeVariable<Class> typeVariable = getTypeParameter(clazz);
     79 
     80         Method method = clazz.getDeclaredMethod("staticMethodGenericType", Object.class);
     81         Type[] genericParameterTypes = method.getGenericParameterTypes();
     82         assertLenghtOne(genericParameterTypes);
     83         Type parameterType = genericParameterTypes[0];
     84         assertNotEquals(typeVariable, parameterType);
     85         assertInstanceOf(TypeVariable.class, parameterType);
     86         assertEquals(method, ((TypeVariable)parameterType).getGenericDeclaration());
     87     }
     88     @SuppressWarnings("unchecked")
     89     public void testHidingMethodGenericType() throws Exception {
     90         Class<? extends GenericType> clazz = GenericType.class;
     91         TypeVariable<Class> typeVariable = getTypeParameter(clazz);
     92 
     93         Method method = clazz.getDeclaredMethod("hidingMethodGenericType",  Object.class);
     94         Type[] genericParameterTypes = method.getGenericParameterTypes();
     95         assertLenghtOne(genericParameterTypes);
     96         Type parameterType = genericParameterTypes[0];
     97         assertNotEquals(typeVariable, parameterType);
     98         assertInstanceOf(TypeVariable.class, parameterType);
     99         assertEquals(method, ((TypeVariable)parameterType).getGenericDeclaration());
    100     }
    101 
    102     static class MultipleGenericTypes<T,S>{
    103         void multipleGenericTypesT(T t){}
    104         void multipleGenericTypesS(S s){}
    105         void multipleGenericTypesTS(T t, S s){}
    106     }
    107     @SuppressWarnings("unchecked")
    108     public void testMultipleGenericTypes() throws Exception {
    109         //Type parameters
    110         Class<? extends MultipleGenericTypes> clazz = MultipleGenericTypes.class;
    111         TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
    112         assertEquals(2, typeParameters.length);
    113         TypeVariable<?> typeVariableT = typeParameters[0];
    114         assertEquals(clazz, typeVariableT.getGenericDeclaration());
    115         assertEquals("T", typeVariableT.getName());
    116         TypeVariable<?> typeVariableS = typeParameters[1];
    117         assertEquals("S", typeVariableS.getName());
    118         assertEquals(clazz, typeVariableS.getGenericDeclaration());
    119 
    120         // multipleGenericTypesT
    121         Method multipleGenericTypesT = clazz.getDeclaredMethod("multipleGenericTypesT", new Class[]{Object.class});
    122         Type[] multipleGenericTypesTTypes = multipleGenericTypesT.getGenericParameterTypes();
    123         assertLenghtOne(multipleGenericTypesTTypes);
    124         Type multipleGenericTypesTType = multipleGenericTypesTTypes[0];
    125         assertEquals(typeVariableT, multipleGenericTypesTType);
    126 
    127         // multipleGenericTypesS
    128         Method multipleGenericTypesS = clazz.getDeclaredMethod("multipleGenericTypesS", new Class[]{Object.class});
    129         Type[] multipleGenericTypesSTypes = multipleGenericTypesS.getGenericParameterTypes();
    130         assertLenghtOne(multipleGenericTypesSTypes);
    131         Type multipleGenericTypesSType = multipleGenericTypesSTypes[0];
    132         assertEquals(typeVariableS, multipleGenericTypesSType);
    133 
    134         // multipleGenericTypesS
    135         Method multipleGenericTypesTS = clazz.getDeclaredMethod("multipleGenericTypesTS", new Class[]{Object.class, Object.class});
    136         Type[] multipleGenericTypesTSTypes = multipleGenericTypesTS.getGenericParameterTypes();
    137         assertEquals(2, multipleGenericTypesTSTypes.length);
    138         Type multipleGenericTypesTSTypeT = multipleGenericTypesTSTypes[0];
    139         assertEquals(typeVariableT, multipleGenericTypesTSTypeT);
    140         Type multipleGenericTypesTSTypeS = multipleGenericTypesTSTypes[1];
    141         assertEquals(typeVariableS, multipleGenericTypesTSTypeS);
    142     }
    143 
    144     @SuppressWarnings("unchecked")
    145     public void testMultipleBoundedGenericTypes() throws Exception {
    146         //Type parameters
    147         Class<? extends MultipleBoundedGenericTypes> clazz = MultipleBoundedGenericTypes.class;
    148         TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
    149         assertEquals(2, typeParameters.length);
    150         TypeVariable<?> typeVariableT = typeParameters[0];
    151         assertEquals(clazz, typeVariableT.getGenericDeclaration());
    152         assertEquals("T", typeVariableT.getName());
    153         TypeVariable<?> typeVariableS = typeParameters[1];
    154         assertEquals("S", typeVariableS.getName());
    155         assertEquals(clazz, typeVariableS.getGenericDeclaration());
    156         Type[] boundsS = typeVariableS.getBounds();
    157         assertLenghtOne(boundsS);
    158         Type boundS = boundsS[0];
    159         assertEquals(typeVariableT, boundS);
    160     }
    161     @SuppressWarnings("unchecked")
    162     public void testSimpleInheritance() throws Exception {
    163         Class<? extends SimpleInheritance> clazz = SimpleInheritance.class;
    164         TypeVariable<Class> subTypeVariable = getTypeParameter(clazz);
    165 
    166         assertInstanceOf(ParameterizedType.class, clazz.getGenericSuperclass());
    167         ParameterizedType parameterizedSuperType = (ParameterizedType) clazz.getGenericSuperclass();
    168         assertInstanceOf(Class.class, parameterizedSuperType.getRawType());
    169 
    170         TypeVariable<Class> superTypeParameter = getTypeParameter((Class<?>)parameterizedSuperType.getRawType());
    171         TypeVariable<Class> typeParameter = getTypeParameter(GenericType.class);
    172         assertEquals(superTypeParameter, typeParameter);
    173 
    174         assertNotEquals(subTypeVariable, superTypeParameter);
    175 
    176         Type[] actualTypeArguments = parameterizedSuperType.getActualTypeArguments();
    177         assertLenghtOne(actualTypeArguments);
    178         assertInstanceOf(TypeVariable.class, actualTypeArguments[0]);
    179         TypeVariable actualSuperTypeVariable = (TypeVariable) actualTypeArguments[0];
    180         assertEquals(subTypeVariable, actualSuperTypeVariable);
    181     }
    182     @SuppressWarnings("unchecked")
    183     public void testInnerClassTest() throws Exception {
    184         Class<? extends InnerClassTest> clazz =InnerClassTest.class;
    185         TypeVariable<Class> typeVariable = getTypeParameter(clazz);
    186 
    187         Class<?>[] declaredClasses = clazz.getDeclaredClasses();
    188         assertLenghtOne(declaredClasses);
    189         Class<?> innerClazz = declaredClasses[0];
    190         assertEquals(InnerClassTest.InnerClass.class, innerClazz);
    191 
    192         //constructor
    193         Constructor<?>[] declaredConstructors = innerClazz.getDeclaredConstructors();
    194         assertLenghtOne(declaredConstructors);
    195         Constructor<?> declaredConstructor = declaredConstructors[0];
    196         Type[] genericParameterTypes = declaredConstructor.getGenericParameterTypes();
    197         assertLenghtOne(genericParameterTypes);
    198         assertEquals(typeVariable, genericParameterTypes[0]);
    199         assertInstanceOf(TypeVariable.class, genericParameterTypes[0]);
    200         TypeVariable<?> constructorTypeVariable = (TypeVariable<?>) genericParameterTypes[0];
    201         assertEquals(clazz ,constructorTypeVariable.getGenericDeclaration());
    202 
    203         //method
    204         Method declaredMethods = innerClazz.getDeclaredMethod("innerMethod", Object.class);
    205         Type[] methodParameterTypes = declaredMethods.getGenericParameterTypes();
    206         assertLenghtOne(methodParameterTypes);
    207         assertEquals(typeVariable, methodParameterTypes[0]);
    208         assertInstanceOf(TypeVariable.class, methodParameterTypes[0]);
    209         TypeVariable<?> methodTypeVariable = (TypeVariable<?>) methodParameterTypes[0];
    210         assertEquals(clazz, methodTypeVariable.getGenericDeclaration());
    211     }
    212     @SuppressWarnings("unchecked")
    213     public void testException() throws Exception {
    214         Class<? extends ExceptionTest> clazz = ExceptionTest.class;
    215         TypeVariable<Class> typeVariable = getTypeParameter(clazz);
    216         Method method = clazz.getDeclaredMethod("exceptionTest");
    217         Type[] genericExceptionTypes = method.getGenericExceptionTypes();
    218         assertLenghtOne(genericExceptionTypes);
    219         assertEquals(typeVariable, genericExceptionTypes[0]);
    220 
    221         Class<?>[] declaredClasses = clazz.getDeclaredClasses();
    222         assertLenghtOne(declaredClasses);
    223         Class<?> innerClazz = declaredClasses[0];
    224         assertEquals(ExceptionTest.InnerClass.class, innerClazz);
    225 
    226         //method
    227         Method declaredMethods = innerClazz.getDeclaredMethod("innerExceptionTest");
    228         Type[] exceptionTypes = declaredMethods.getGenericExceptionTypes();
    229         assertLenghtOne(exceptionTypes);
    230         assertEquals(typeVariable, exceptionTypes[0]);
    231         assertInstanceOf(TypeVariable.class, exceptionTypes[0]);
    232         TypeVariable<?> methodTypeVariable = (TypeVariable<?>) exceptionTypes[0];
    233         assertEquals(clazz, methodTypeVariable.getGenericDeclaration());
    234     }
    235 }
    236