Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse warning: unchecked conversion vs redundant specification of type arguments

I just recently switched from JDK1.6 to JDK 1.7.

I have this code:

SomeClass<SomeType> someVariable = new SomeClass<SomeType>(createSomeObject());

Now I'm getting a warning:

Redundant specification of type arguments <SomeType>

If I use the quick fix Eclipse gives me this:

SomeClass<SomeType> someVariable = new SomeClass<>(createSomeObject());

Which results in

Got an exception - expecting EOF, found 'xyz'

xyz is the next item in my code text.

When I remove the angled brackets, I get this warning:

SomeClass is a raw type. References to generic type SomeClass<M> should be parameterized

If I add the type parameter I end up with the first warning (redundant spec...)

WTF is going on?

I want to keep both warnings and I'm still using Eclipse 3.7.1. I'm not willing to update my Eclipse, if there's another way to solve this problem, since it'll take me some time to configure it the way I want it again.

like image 418
Torsten Avatar asked Feb 12 '13 09:02

Torsten


People also ask

What is unchecked conversion?

The warning message “unchecked conversion” implies that we should check the conversion before the assignment. To check the type conversion, we can go through the raw type collection and cast every element to our parameterized type.

What is unchecked warning in Java?

An unchecked warning tells a programmer that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.


1 Answers

Redundant specification of type arguments <SomeType>

comes from Java 7's type inference mechanism. Specifying the generic types twice is indeed redundant, since the compiler can intuit what you require simply from

SomeClass<SomeType> someVariable = new SomeClass<>(createSomeObject());

and consequently you don't need the generic type in both the declaration and definition (type inference could go further - e.g. with Scala you simply declare the LHS as a val or var, and the compiler knows what type it really needs to be).

like image 164
Brian Agnew Avatar answered Oct 16 '22 22:10

Brian Agnew