get Class Type Variable - Java Reflection

Java examples for Reflection:Class

Description

get Class Type Variable

Demo Code


//package com.java2s;

import java.lang.reflect.TypeVariable;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class classInstance = String.class;
        getClassTypeVariable(classInstance);
    }/*from  w w w . java 2 s . com*/

    public static void getClassTypeVariable(Class<?> classInstance) {
        TypeVariable<?>[] tv;
        String x;
        tv = classInstance.getTypeParameters(); //warning: unchecked conversion
        for (int i = 0; i < tv.length; i++) {
            x = tv[i].getName(); // E,K,V...
            if (i == 0) //?
                System.out.print("<" + x);
            else
                //
                System.out.print("," + x);
            if (i == tv.length - 1) //
                System.out.println(">");
        }
    }
}

Related Tutorials