OT : Generified class returning T[] ?

A little off topic, and I dont think this is an IDEA generics bug:

I have a generified class :
class Foo { } is it possible to have a method in that class return a T[] ? The compiler keeps telling me that Im trying to return a generic array - which indeed I am - is there a way to return a typed collection like this? T[] getXYZ() or List]]> getXYZ()

Nick

7 comments
Comment actions Permalink

You can return a List]]>, but you just can't declare generic arrays; this is a limitation of the generics type system, and it's described in the specification, section 7.3:

The component type of an array object may not be a type variable or a parameterized type, unless it is an (unbounded) wildcard type.You can declare array types whose element type is a type variable or a parameterized type, but not array objects.

For more info: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

0
Comment actions Permalink

Nick

>A little off topic, and I dont think this is an IDEA generics bug:
>

>


IDEA generates code that doesn't compile, with the error you mentioned
above.


For example,

public class StubApplication implements
com.intellij.openapi.application.Application
{
...
public T[] getComponents (Class baseInterfaceClass) {..} ... } If you delete 'getComponents(..)', and let IDEA recreate it, you'll get public T[] getComponents (Class]]> baseInterfaceClass) {
return new T[0];
}


, that doesn't compile, for the same reason.


Alain

0
Comment actions Permalink


"Nick Pratt" <no_mail@jetbrains.com> wrote in message
news:6420211.1115654573263.JavaMail.itn@is.intellij.net...

A little off topic, and I dont think this is an IDEA generics bug:

>

I have a generified class :
class Foo<T>
{
}

>

is it possible to have a method in that class return a T[] ?
The compiler keeps telling me that Im trying to return a generic array -

which indeed I am - is there a way to return a typed collection like this?
What version of the compiler are you using? In released
version of jdk 5.0 this limitation has been removed.

Eugene.


0
Comment actions Permalink

What version of the compiler are you using? In
released
version of jdk 5.0 this limitation has been removed.


Hmmm, not that I'm aware of. JDK 5.0 contains the final release implementation for JSR14, and if you check the specification it specifically disallows generic arrays except for very particular cases (unbounded wildcards).

0
Comment actions Permalink


"Marcus Brito" <mbrito@gmail.com> wrote in message
news:31474462.1115666344456.JavaMail.itn@is.intellij.net...

What version of the compiler are you using? In
released
version of jdk 5.0 this limitation has been removed.

>

Hmmm, not that I'm aware of. JDK 5.0 contains the final release

implementation for JSR14, and if you check the specification it specifically
disallows generic arrays except for very particular cases (unbounded
wildcards).

No jsr 14 has been greatly outdated. Please consult jls3
if you don't believe me and javac:)
Eugene.


0
Comment actions Permalink

Probably I have not read your post carefully enough.
No, you are not allowed to return new T[], but you can
have variables of type T[] , and return them.
Probably you should consult SUN Generics forum archives for further
details.

"Nick Pratt" <no_mail@jetbrains.com> wrote in message
news:6420211.1115654573263.JavaMail.itn@is.intellij.net...

A little off topic, and I dont think this is an IDEA generics bug:

>

I have a generified class :
class Foo<T>
{
}

>

is it possible to have a method in that class return a T[] ?
The compiler keeps telling me that Im trying to return a generic array -

which indeed I am - is there a way to return a typed collection like this?
>

T[] getXYZ()
or
List<T> getXYZ()

>

Nick



0
Comment actions Permalink

You can return a generic array ( look at Collection.toArray() ), but creating that array can be tricky. In order to programaticly create an array, the type must be known at compile time. To create an array at run-time, you must obtain a Class object.

I get arround these limitations with a very simple static function:

public static ]]> T[] toArray(T...array)
{
return array;
}

0

Please sign in to leave a comment.