Java反射-在通用接口中获取T的实际类型<;T>;

Java反射-在通用接口中获取T的实际类型<;T>;,java,generics,reflection,interface,Java,Generics,Reflection,Interface,如何在泛型接口中获得T的实际类型 鉴于以下类别: class myClazz { private final IGenericInterface<?> genericInterface; myClazz(final IGenericInterface<otherClazz> genericInterface){ this.genericInterface = genericInterface; }

如何在泛型接口中获得T的实际类型

鉴于以下类别:

class myClazz {
        private final IGenericInterface<?> genericInterface;

        myClazz(final IGenericInterface<otherClazz> genericInterface){
            this.genericInterface = genericInterface;
        }
}

但是我不知道下一步如何获得泛型接口中使用的实际类型的简单名称。

关闭,但您需要使用,以及(因为您的构造函数不是公共的):

Class cls=(Class)((ParameterizedType)myClazz.Class.getDeclaredConstructors()[0]
.getGenericParameterTypes()[0])//第一个构造函数,第一个参数
.getActualTypeArguments()[0];//第一类参数
系统输出打印LN(cls);//印刷品的分类`
应该注意的是,只有当参数的type参数是具体类型时,此代码才有效,否则转换为
将不起作用。例如,在参数是类型变量的情况下,
getActualTypeArguments()[0]
将返回的是的实例,而不是

的可能重复项
String otherClazzString = myClazz.class.getConstructors()[0].getParameterTypes()[0].toString(); // 
Class<?> cls = (Class<?>) ((ParameterizedType) myClazz.class.getDeclaredConstructors()[0]
    .getGenericParameterTypes()[0]) // first constructor, first parameter
        .getActualTypeArguments()[0]; // first type argument

System.out.println(cls); // prints 'class otherClazz`