简体   繁体   English

如何将泛型与类数组一起使用?

[英]How do I use generics with an array of Classes?

I want to create an array of Classes, each representing a type that is available in the system I'm building. 我想创建一个Classes数组,每个类表示我正在构建的系统中可用的类型。 All the Classes involved are subclasses of a common superclass. 涉及的所有类都是公共超类的子类。 So I'd like to do: 所以我想做:

Class<? extends SuperClass>[] availableTypes = { SubClass1.class, SubClass2.class };

This gives me the error: 这给了我错误:

Cannot create a generic array of Class<? extends SuperClass>.

I get the same message if I try to qualify the creation of the array on the right hand side of the initialization: 如果我尝试在初始化的右侧限定数组的创建,我会收到相同的消息:

Class<? extends SuperClass>[] availableTypes = Class<? extends SuperClass>[] { SubClass1.class, SubClass2.class };

I can get the code to compile if I eliminate the generics qualifications: 如果我取消了泛型资格,我可以编译代码:

Class[] availableTypes = { SubClass1.class, SubClass2.class };

But then I get the generics warning: 但后来我得到了泛型警告:

Class is a raw type. 类是原始类型。 References to generic type Class should be parameterized. 应参数化对泛型类的引用。

I'm trying; 我尝试着; I'm trying! 我尝试着! :) Also, at this point, even if this didn't provoke a warning, I lose a piece of the interface I was trying to define. :)此外,在这一点上,即使这没有引起警告,我也失去了我试图定义的界面。 I don't want to just return an array of arbitrary classes; 我不想只返回一个任意类的数组; I want to return an array of classes that are all subclasses of a particular SuperClass! 我想返回一个类的数组,这些类都是特定SuperClass的子类!

Eclipse has some pretty powerful tools for figuring out what parameters to use to fix generics declarations, but in this case it falls down, as it tends to do when you deal with Class. Eclipse有一些非常强大的工具可用于确定用于修复泛型声明的参数,但在这种情况下,它会降低,因为它在处理Class时往往会失败。 The "Infer Generic Type Arguments" procedure it offers doesn't change the code at all, leaving the warning. 它提供的“推断通用类型参数”过程根本不会更改代码,而是留下警告。

I was able to work around this by using a Collection instead: 我可以通过使用Collection来解决这个问题:

List<Class<? extends SuperClass>> availableTypes = new List<Class<? extends SuperClass>>();

But what's the right way to do this with arrays? 但是使用数组做到这一点的正确方法是什么?

It seems a bit defeatist, but problems like this are precisely the reason why most people avoid mixing arrays and generics. 它似乎有点失败,但这样的问题正是大多数人避免混合数组和泛型的原因。 Because of the way generics are implemented ( type erasure ), arrays and generics will never work well together. 由于泛型的实现方式( 类型擦除 ),数组和泛型将永远无法很好地协同工作。

Two workarounds: 两个解决方法:

  • Stick to using a collection (eg ArrayList<Class<? extends SuperClass>> ), which works just as well as an array and also allows expansion. 坚持使用集合(例如ArrayList<Class<? extends SuperClass>> ),它与数组一样工作,也允许扩展。
  • Put a @SuppressWarnings("unchecked") annotation on the code creating the array along with a comment justifying its use. 在创建数组的代码上放置@SuppressWarnings("unchecked")注释以及证明其使用的注释。

Use this syntax: 使用以下语法:

Class<? extends SuperClass>[] avail = new Class[] { SubClass1.class, ... };

It will give you an "unchecked" warning, and rightly so, since you might be including a Class object for a type that doesn't extend SuperClass in the array. 它会给你一个“未经检查”的警告,这是正确的,因为你可能包含一个Class对象, Class对象不会在数组中扩展SuperClass

The right way to do this with arrays is to do it with a Collection. 使用数组执行此操作的正确方法是使用Collection执行此操作。 Sorry! 抱歉! For a complicated set of reasons, arrays don't play nice with generics. 由于一系列复杂的原因,数组与泛型不匹配。 Arrays have a different covariance model than generic objects do, which ultimately causes the problems you are running into. 数组具有与通用对象不同的协方差模型,这最终会导致您遇到的问题。 For example, with arrays, but not (normally) with generic objects, you can legally do this: 例如,对于数组,但不是(通常)使用通用对象,您可以合法地执行此操作:

Object[] myArray = new String[5];

while you cannot do this: 虽然你不能这样做:

LinkedList<Object> myCollection = new LinkedList<String>();

If you want more detail, you can see the Arrays In Java Generics page from the excellent Generics FAQ 如果您想了解更多详细信息,可以从优秀的Generics常见问题解答中看到Arrays In Java Generics页面

As simonn said, you can also just use your arrays as they are, and use @SuppressWarnings("unchecked") to silence the warnings. 正如simonn所说,你也可以按原样使用你的阵列,并使用@SuppressWarnings("unchecked")来消除警告。 This will function, but without the type safety that generics can provide you. 这将起作用,但没有泛型可以为您提供的类型安全。 If it's performance that you are worried about, just use an ArrayList so you are just using a thin wrapper around an array, but with all of the type safety guarantees provided by generics. 如果您担心它的性能,只需使用ArrayList这样您就可以在数组周围使用一个薄的包装器,但具有泛型提供的所有类型安全保证。

But what's the right way to do this with arrays? 但是使用数组做到这一点的正确方法是什么?

There's no type-safe way to do this; 没有类型安全的方法来做到这一点; using the collection is the right approach. 使用该集合是正确的方法。 To see why, imagine if this were allowed. 要了解原因,想象一下是否允许这样做。 You could have a situation like this: 你可能会遇到这样的情况:

// Illegal!
Object[] baskets = new FruitBasket<? extends Citrus>[10];

// This is okay.
baskets[0] = new FruitBasket<Lemon>();

// Danger! This should fail, but the type system will let it through.
baskets[0] = new FruitBasket<Potato>();

The type system needs to detect whether a basket that gets added to the array is of the type FruitBasket<? extends Citrus> 类型系统需要检测添加到数组的篮子是否属于FruitBasket<? extends Citrus> FruitBasket<? extends Citrus> or a subtype. FruitBasket<? extends Citrus>或子类型。 A FruitBasket does not match and should be rejected with an ArrayStoreException . 一个水果篮不符,应与被拒绝ArrayStoreException But nothing happens! 但没有任何反应!

Because of type erasure, the JVM can only see the runtime type of the array. 由于类型擦除,JVM只能看到数组的运行时类型。 At runtime, we need to compare the array type to the element type to make sure they match. 在运行时,我们需要将数组类型与元素类型进行比较,以确保它们匹配。 The array's runtime component type is FruitBasket[] after type erasure; 类型擦除后,数组的运行时组件类型为FruitBasket[] ; likewise, the element's runtime type is FruitBasket . 同样,元素的运行时类型是FruitBasket No problems will be detected -- and that's why this is dangerous. 没有问题会被发现 - 这就是为什么这是危险的。

The problem is that it is illegal to create an array of a generic type. 问题是创建泛型类型的数组是非法的。 The only way to get around it is by casting to the generic type when you create the array, but that's not a very good solution. 解决它的唯一方法是在创建数组时转换为泛型类型,但这不是一个非常好的解决方案。 (Note that it is possible to use a generic array, just not create one: see this question .) (注意,可以使用通用数组,只是不创建一个:看到这个问题 。)

You should almost always be using lists instead of arrays anyway, so I think you came up with the best solution already. 你应该几乎总是使用列表而不是数组,所以我认为你已经提出了最好的解决方案。

Not type safe and with unchecked cast warning: 不安全且未经检查的投射警告:

Class<? extends SuperClass>[] availableTypes =
    (Class<? extends SuperClass>[])
    (new Class[]{ SubClass1.class, SubClass2.class });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM