<分区>
问:为什么包含类的名字出现了两次?
上下文:我正在生成代码,目标是获取字段的声明,如源代码中所写(完全限定没问题,但我需要类型参数):test.Foo.Bar<java.lang.String>
package test;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
public class Foo
{
public static class Bar<TYPE> {}
private Bar<String> bar;
public static void main(String[] args) throws Exception
{
Field field = Foo.class.getDeclaredField("bar");
Type genericType = field.getGenericType();
Type type = field.getType();
System.out.println("genericType: " + genericType.getTypeName());
System.out.println(" type: " + type.getTypeName());
}
}
输出:
genericType: test.Foo.test.Foo$Bar<java.lang.String>
type: test.Foo$Bar
更新: 谢谢大家的意见。由于这个问题被标记为重复,我发布了我当前的工作解决方案 over there .