Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use generics with an array of Classes?

Tags:

java

generics

I want to create an array of Classes, each representing a type that is available in the system I'm building. 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!

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. 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:

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

But what's the right way to do this with arrays?

like image 707
skiphoppy Avatar asked Apr 14 '09 21:04

skiphoppy


People also ask

Can you use generics with an array?

To understand the reason, you first need to know two arrays are covariant and generics are invariant. Because of this fundamental reason, arrays and generics do not fit well with each other.

How do you create an array of generic classes in Java?

You can do this: E[] arr = (E[])new Object[INITIAL_ARRAY_LENGTH]; This is one of the suggested ways of implementing a generic collection in Effective Java; Item 26. No type errors, no need to cast the array repeatedly.

How do you create a generic array of objects in Java?

getComponentType(), size); Notice how it makes use of Array#newInstance to build a new array, like in our previous stack example. We can also see that parameter a is used to provide a type to Array#newInstance. Finally, the result from Array#newInstance is cast to T[] to create a generic array.


2 Answers

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 (e.g. ArrayList<Class<? extends SuperClass>>), which works just as well as an array and also allows expansion.
  • Put a @SuppressWarnings("unchecked") annotation on the code creating the array along with a comment justifying its use.
like image 75
Simon Nickerson Avatar answered Oct 10 '22 22:10

Simon Nickerson


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.

like image 25
erickson Avatar answered Oct 10 '22 22:10

erickson