以执行以下操作:

public Class<List<String>> getObjectType() {
    // what can I return here?
}

我可以从该方法中,将满足泛型和编译返回什么类文字表达? List.class不会编译,并且也不会List.<String>class

如果你想知道“为什么”,我写春天的FactoryBean<List<String>>,这就要求我执行Class<List<String>> getObjectType()的实现。然而,这是的一个Spring问题。

编辑:我的悲哀的哭声已经由权力,是在SpringSource的,所以春季3.0.1将有getObjectType()的返回类型更改为Class<?>,整齐地避免了这个问题听到

有帮助吗?

解决方案

您可以随时转换为你需要什么,这样

return (Class<List<String>>) new ArrayList<String>().getClass();

return (Class<List<String>>) Collections.<String>emptyList().getClass();

但我认为这不是你所追求的。那么它的工作原理,用一个警告,但它是不完全的“美丽”。

我刚发现这

  

为什么没有类字面通配符参数化类型?

     

由于通配符参数化类型没有确切运行时类型的表示。

所以铸造可能是唯一的道路要走。

其他提示

您永远不应该使用结构Class<List<String>>。这是荒谬的,应该产生的Java警告(但没有)。类实例始终代表原始的类型,所以你可以有Class<List>;而已。如果你想要的东西来代表一个具体化的泛型类型一样List<String>,你需要一个“超级型令牌”像吉斯用途:

HTTP://google-guice.googlecode。 COM / GIT中/的Javadoc / COM /谷歌/注射/ TypeLiteral.html

一个Class<List<String>>的存在是固有的危险。这里的原因:

// This statement generates a warning - for a reason...
Class<List<String>> unsafeListClass = (Class<List<String>>) (Class<?>) List.class;

List<Integer> integerList = new ArrayList<Integer>(); // Ok
integerList.add(42); // Ok

System.out.println(unsafeListClass.isInstance(integerList)); // Prints "true".
List<String> stringList =
   unsafeListClass.cast(integerList); // Succeeds, with no warning!
stringList.add("Hello, World!"); // Also succeeds with no warning

for (int x: integerList) {
    // Compiles without warning, but throws ClassCastException at runtime
    System.out.println(100-x);
}

此链接上springframework.org其给出的一些见解。

E.g。

List<String> myList = new ArrayList<String>();
return (Class<List<String>>)myList.getClass();

可以实现该方法是这样的:

public Class<List<String>> getObjectType() {
    return (Class<List<String>>) ((Class)List.class);
}

检查出在SUN的论坛讨论:

http://forums.sun.com/thread.jspa?threadID=5253007

和通过使用“超级型令牌”描述了一个变通引用的博客文章:

http://gafter.blogspot.com/2006/12 /super-type-tokens.html

我不知道这是不可能的,因为任何文字类将被编译为Class.forName(...)和,因为这发生在运行时没有留下任何的一般信息。

我不知道,但下面的问题,我问可能是相关给你...

Java泛型类型参数和操作上的那些类型

了解这个什么:

public class TestMain {
    public static void main(String[] args) throws Exception {
        Type type = TestMain.class.getMethod("dummy").getGenericReturnType();
        System.out.println("type = " + type);
    }

    public List<Integer> dummy() {return null;}
}

此打印:

type = java.util.List<java.lang.Integer>

下面的方法是有问题:

> public Class<List<String>> getModelType() {
>   return (Class<List<String>>) new ArrayList<String>().getClass();
> }

e.g。如果你想测试是否类型的对象说

org.eclipse.emf.common.util.BasicEList<String> 

是类型的

List<String> 

基于前述getModelType()方法的结果,例如:

BasicEList<String> fromObject = ...;
if (getModelType().isAssignableFrom(fromObject.getClass())) {
    transferFromModelToUi(getModelType().cast(fromObject));
}

它会导致错误的,而不是因为这两个对象实现的接口列表(因为getModelType()返回类型列表,而不是ArrayList的一类对象)应该是真实的。

下面是为我工作(有点麻烦,但会导致校正上面的例子中,可以被移动到一个静态初始化的结果)的方法:

public Class<List<String>> getModelType() {
    Class<?> arrayListClass = new ArrayList<String>().getClass();
    Class<?>[] interfaces = arrayListClass.getInterfaces();
    int index = 0;
    for (int i = 0; i < interfaces.length; i++) {
        if (interfaces[i].equals(List.class)) {
            index = i;
            break;
        }
    }
    return (Class<List<String>>) interfaces[index];
}
scroll top