Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Generics and unparameterized types
Generics and unparameterized types [message #664886] Tue, 12 April 2011 12:05 Go to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

I've something I'm really having trouble to understand why generic
informations are completely removed from a class when used in an
unparameterized way (a.getNames() is return List only!).

It's even more strange because the base class inheriting from the
Generic information is still available!

Because it also fails for javac I don't think it is a compiler bug but
I'd like to understand why this happens.

> import java.util.List;
>
> public class TestCompiler {
> public interface Base {
> public List<String> getBaseList();
> }
>
> public interface A<O> extends Base {
> public List<O> getTest();
>
> public List<String> getNames();
> }
>
> public interface B {
> public List<?> getTest();
>
> public List<String> getNames();
> }
>
> public static void main(String[] args) {
> A a = getA();
> A<?> a2 = getA();
>
> B b = getB();
>
> for (String s : a.getNames()) {
> System.err.println(s);
> }
>
> for( String s : a.getBaseList() ) {
> System.err.println(s);
> }
>
> for (String s : a2.getNames()) {
> System.err.println(s);
> }
>
> for (String s : b.getNames()) {
> System.err.println(s);
> }
> }
>
> private static A getA() {
> return null;
> }
>
> private static B getB() {
> return null;
> }
> }

Tom
Re: Generics and unparameterized types [message #664924 is a reply to message #664886] Tue, 12 April 2011 13:55 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NOSPAM.ibm.com

Hi,

You said "A a = getA();" and this makes it a Raw type (i.e. a type with
NO generic information). Making something a Raw type, by definition,
strips ALL generic information from it. That is the spec. So by
definition the getNames() returns a List, not a List<String>. And
List.iterator() returns Objects.

Look at this way:

public void y() {
A<Number> a = getA();
x(a);
}

public void x(A a) {
a.getNames();
}

What do you expect a.getNames() to return? It only knows that A is a RAW
type so it can only treat it as List. It doesn't matter that it was
called with A<Number> in the x(a) call. That information is lost because
it was a Raw type.


Rich
Re: Generics and unparameterized types [message #664926 is a reply to message #664924] Tue, 12 April 2011 14:03 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi Rich,

It's totally clear to me that

List<O> getTest()

gets

List getTest()

when A is a raw type but why does the same happen to a method like

List<String> getNames()

and then why is the information not also stripped from

List<String> getBaseList();

I can accept that all generic information is stripped but at the moment
it only happens on the concrete class informations defined in A but NOT
in Base!

Tom

Am 12.04.11 15:55, schrieb Rich Kulp:
> Hi,
>
> You said "A a = getA();" and this makes it a Raw type (i.e. a type with
> NO generic information). Making something a Raw type, by definition,
> strips ALL generic information from it. That is the spec. So by
> definition the getNames() returns a List, not a List<String>. And
> List.iterator() returns Objects.
>
> Look at this way:
>
> public void y() {
> A<Number> a = getA();
> x(a);
> }
>
> public void x(A a) {
> a.getNames();
> }
>
> What do you expect a.getNames() to return? It only knows that A is a RAW
> type so it can only treat it as List. It doesn't matter that it was
> called with A<Number> in the x(a) call. That information is lost because
> it was a Raw type.
>
>
> Rich
Re: Generics and unparameterized types [message #664933 is a reply to message #664926] Tue, 12 April 2011 14:19 Go to previous message
Eclipse UserFriend
Originally posted by: richkulp.us.NOSPAM.ibm.com

On 4/12/2011 10:03 AM, Tom Schindl wrote:
> and then why is the information not also stripped from
>
> List<String> getBaseList();
>

Base isn't a Raw type. It actually is generic enabled. Since it doesn't
have any generic information in it's declaration (interface Base) then
Base by itself is not a Raw type.

A is one that is declared with a generic in the declaration itself. So
when you use it without the generics it becomes a raw type. Maybe it is
overkill that List<String> became just List, but I don't know the
reasoning well enough to know why. There may be valid reasons in the
spec for it.

--
Thanks,
Rich Kulp
Previous Topic:Can't Create Executable JAR
Next Topic:Eclipse EE doesnot start at all
Goto Forum:
  


Current Time: Tue Apr 16 22:49:16 GMT 2024

Powered by FUDForum. Page generated in 1.08178 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top