Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Difficulties in defining null annotations for generics
Difficulties in defining null annotations for generics [message #1564190] Wed, 14 January 2015 17:02 Go to next message
Jari Juslin is currently offline Jari JuslinFriend
Messages: 2
Registered: July 2009
Junior Member
Hello,

I'm facing a hard wall in trying to get null annotations work with generics and inheritance hierarchy.

I have specified @NonNullByDefault on package level.

Then I have a class in that package:

public abstract class Foo<T extends @Nullable Object> {
...

@Nullable
public abstract T process();
}

Now I'm trying to define a session bean, in the same package, that would accept these as a parameter:

@Local
public interface BarLocal {
<T extends @Nullable Object> T executeFoo(Foo<T> foo);
}

Here the method declaration gives an error

"Null constraint mismatch: The type 'T extends @Nullable Object' is not a valid substitute for the type parameter 'T extends @Nullable Object' "

_and_

"Null constraint mismatch: The type 'T extends @Nullable Object' is not a valid substitute for the type parameter 'T extends @NonNull Object' "

What is going on? To me it looks like the @NonNullByDefault would add an implicit @NonNull even to declarations that explicitly state the nullness status. And how can T extends @Nullable Object not be a valid substitute for T extends @Nullable Object? They are exactly the same?

Thanks in advance!
Re: Difficulties in defining null annotations for generics [message #1564542 is a reply to message #1564190] Wed, 14 January 2015 21:30 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Given those snippets, I can reproduce the first error, but not the second.
I'll track this in https://bugs.eclipse.org/457499

For now, I'd suggest you simply change "<T extends @Nullable Object>" to "<T>", as that type bound doesn't really add anything interesting:

  • @NonNullByDefault doesn't normally affect type parameters (unless you explicitly include DefaultLocation.TYPE_PARAMETER, see javadoc of NonNullByDefault#value).
  • everything is a subtype of '@Nullable Object', including '@NonNull Object'.


best,
Stephan

Re: Difficulties in defining null annotations for generics [message #1572624 is a reply to message #1564542] Mon, 19 January 2015 08:34 Go to previous message
Jari Juslin is currently offline Jari JuslinFriend
Messages: 2
Registered: July 2009
Junior Member
Thank you!

Your point of disabling the generic type null annotation checks was a good one; it seems obvious now, but I had not thought about it.

With them I could not get the code to compile. It's rather complicated web of inherited classes; as a whole we have super class taking generic argument and then subclasses of both @Nullable and @NotNull of it; and some routines need to accept both kind.
Previous Topic:Problems installing examples plugin
Next Topic:Tomcat v8.0 not starting
Goto Forum:
  


Current Time: Sat Sep 21 19:03:49 GMT 2024

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

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

Back to the top