简体   繁体   中英

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.

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 - eg with Scala you simply declare the LHS as a val or var , and the compiler knows what type it really needs to be).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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