问:为什么包含类的名称会出现两次?为什么外部类在泛型类型名称中出现两次?
语境:我生成代码和目标是让现场的声明,如写入源(完全合格是好的,但我需要的类型参数):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
UPDATE: 谢谢大家对您的输入。由于这个问题被标记为重复,我张贴了我目前的工作解决方案over there。
另见https://bugs.openjdk.java.net/browse/JDK-8054213的。 – Radiodef
@Radiodef好,这个问题似乎与我的问题相同。所以这是一个错误? (转换为答案) – Zalumon
它可能应该被认为是一个错误,但是对于类型的'String'表示没有真正的标准化规范。 ['getTypeName'](http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Type.html#getTypeName--)没有指定任何特定的要求,也没有'toString '。这不是API非常发达的部分。 – Radiodef