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 org.apache.harmony.tests.java.lang.reflect;
     18 
     19 import java.io.Serializable;
     20 import java.lang.reflect.Constructor;
     21 import java.lang.reflect.Method;
     22 import java.lang.reflect.Type;
     23 import java.lang.reflect.TypeVariable;
     24 
     25 /**
     26  * Tests type variables and their properties.
     27  */
     28 public class TypeVariableTest extends GenericReflectionTestsBase {
     29 
     30     static class A<T>{}
     31     public void testSimpleTypeVariableOnClass(){
     32         Class<? extends A> clazz = A.class;
     33         TypeVariable[] typeParameters = clazz.getTypeParameters();
     34         assertLenghtOne(typeParameters);
     35         TypeVariable<Class> typeVariable = typeParameters[0];
     36         assertEquals(clazz, typeVariable.getGenericDeclaration());
     37         assertEquals("T", typeVariable.getName());
     38         Type[] bounds = typeVariable.getBounds();
     39         assertLenghtOne(bounds);
     40         assertEquals(Object.class, bounds[0]);
     41     }
     42 
     43     static class B{
     44         <T> void b(){};
     45     }
     46     public void testSimpleTypeVariableOnMethod() throws Exception{
     47         Class<? extends B> clazz = B.class;
     48         Method method = clazz.getDeclaredMethod("b");
     49         TypeVariable<Method>[] typeParameters = method.getTypeParameters();
     50         assertLenghtOne(typeParameters);
     51         TypeVariable<Method> typeVariable = typeParameters[0];
     52         assertEquals(method, typeVariable.getGenericDeclaration());
     53         assertEquals("T", typeVariable.getName());
     54         Type[] bounds = typeVariable.getBounds();
     55         assertLenghtOne(bounds);
     56         assertEquals(Object.class, bounds[0]);
     57     }
     58 
     59     static class C {
     60         <T>C(){}
     61     }
     62     public void testSimpleTypeVariableOnConstructor() throws Exception{
     63         Class<? extends C> clazz = C.class;
     64         Constructor<?> constructor = clazz.getDeclaredConstructor();
     65         TypeVariable<?>[] typeParameters = constructor.getTypeParameters();
     66         assertLenghtOne(typeParameters);
     67         TypeVariable<?> typeVariable = typeParameters[0];
     68         assertEquals(constructor, typeVariable.getGenericDeclaration());
     69         assertEquals("T", typeVariable.getName());
     70         Type[] bounds = typeVariable.getBounds();
     71         assertLenghtOne(bounds);
     72         assertEquals(Object.class, bounds[0]);
     73     }
     74 
     75     static class D<Q,R,S>{}
     76     public void testMultipleTypeVariablesOnClass() throws Exception {
     77         Class<? extends D> clazz = D.class;
     78         TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
     79         assertEquals(3, typeParameters.length);
     80         assertEquals("Q", typeParameters[0].getName());
     81         assertEquals(clazz, typeParameters[0].getGenericDeclaration());
     82 
     83         assertEquals("R", typeParameters[1].getName());
     84         assertEquals(clazz, typeParameters[1].getGenericDeclaration());
     85 
     86         assertEquals("S", typeParameters[2].getName());
     87         assertEquals(clazz, typeParameters[2].getGenericDeclaration());
     88 
     89     }
     90 
     91     static class E {
     92         <Q,R,S> void e(){}
     93     }
     94     public void testMultipleTypeVariablesOnMethod() throws Exception {
     95         Class<? extends E> clazz = E.class;
     96         Method method = clazz.getDeclaredMethod("e");
     97 
     98         TypeVariable<?>[] typeParameters = method.getTypeParameters();
     99         assertEquals(3, typeParameters.length);
    100         assertEquals("Q", typeParameters[0].getName());
    101         assertEquals(method, typeParameters[0].getGenericDeclaration());
    102 
    103         assertEquals("R", typeParameters[1].getName());
    104         assertEquals(method, typeParameters[1].getGenericDeclaration());
    105 
    106         assertEquals("S", typeParameters[2].getName());
    107         assertEquals(method, typeParameters[2].getGenericDeclaration());
    108     }
    109 
    110     static class F {
    111         <Q,R,S> F(){}
    112     }
    113     public void testMultipleTypeVariablesOnConstructor() throws Exception {
    114         Class<? extends F> clazz = F.class;
    115         Constructor<?> constructor = clazz.getDeclaredConstructor();
    116 
    117         TypeVariable<?>[] typeParameters = constructor.getTypeParameters();
    118         assertEquals(3, typeParameters.length);
    119         assertEquals("Q", typeParameters[0].getName());
    120         assertEquals(constructor, typeParameters[0].getGenericDeclaration());
    121 
    122         assertEquals("R", typeParameters[1].getName());
    123         assertEquals(constructor, typeParameters[1].getGenericDeclaration());
    124 
    125         assertEquals("S", typeParameters[2].getName());
    126         assertEquals(constructor, typeParameters[2].getGenericDeclaration());
    127     }
    128 
    129     static class G <T extends Number>{}
    130 
    131     public void testSingleBound() throws Exception {
    132         Class<? extends G> clazz = G.class;
    133         TypeVariable[] typeParameters = clazz.getTypeParameters();
    134         TypeVariable<Class> typeVariable = typeParameters[0];
    135         Type[] bounds = typeVariable.getBounds();
    136         assertLenghtOne(bounds);
    137         assertEquals(Number.class, bounds[0]);
    138     }
    139 
    140     static class H <T extends Number & Serializable >{}
    141     public void testMultipleBound() throws Exception {
    142         Class<? extends H> clazz = H.class;
    143         TypeVariable[] typeParameters = clazz.getTypeParameters();
    144         TypeVariable<Class> typeVariable = typeParameters[0];
    145         Type[] bounds = typeVariable.getBounds();
    146         assertEquals(2, bounds.length);
    147         assertEquals(Number.class, bounds[0]);
    148         assertEquals(Serializable.class, bounds[1]);
    149     }
    150 }
    151