Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Relaxing annotation based null checks on JDK8
Relaxing annotation based null checks on JDK8 [message #1695721] Mon, 18 May 2015 11:39 Go to next message
Eclipse UserFriend
Hi,

I'm in the process of upgrading my code base to use JDK8 and I'm stumbling over the stricter annotation based null checking that requires me to annotate generic types with @Nullable/@NonNull.

The JDK7 approach allowed me to selectively add @Nullable/@NonNull on methods. The JDK8 annotations require me to annotate generic types, causing tons of errors in my code base. Example:
class Foo<T> exends Object
{
    public T bar()
    {
        return null;
    }
}


I understand why the new analysis marks the return null statement an error but if I change the declaration of the class to Foo<@Nullable T> all the client code that uses Foo will contain errors.

I do understand the why the new strictness is required. It's clearly my codebase's fault to not properly check the type bounds but alas, I have a lot of code that I cannot and do not want to change right now.

Is there any possibility to get the old, relaxed @Nullable/@NonNull checks on JDK8? (Hint: just using the JDK7 compatible annotations does not work ....).

-dirk
Re: Relaxing annotation based null checks on JDK8 [message #1695736 is a reply to message #1695721] Mon, 18 May 2015 13:36 Go to previous messageGo to next message
Eclipse UserFriend
Don't return a null, return an Optional<T>. Downstream, callers should invoke the empty() method rather than == null.
Re: Relaxing annotation based null checks on JDK8 [message #1695749 is a reply to message #1695736] Mon, 18 May 2015 18:47 Go to previous messageGo to next message
Eclipse UserFriend
Erick Hagstrom wrote on Mon, 18 May 2015 13:36
Don't return a null, return an Optional<T>. Downstream, callers should invoke the empty() method rather than == null.


But that would still require changes in the downstream code, no? My point was that the new style checking was too strict for my codebase. I really cannot go through the entire code base and rewrite every occurrence using Optional<T>.

I'm looking for ways to get by with the JDK7 behaviour of casual @NonNull checking with JDK8 that does not require to change all of my caller's code just now.
Re: Relaxing annotation based null checks on JDK8 [message #1695814 is a reply to message #1695749] Tue, 19 May 2015 10:00 Go to previous messageGo to next message
Eclipse UserFriend
You're correct, using Optional<T> requires downstream changes.

It sounds as though you don't really want to use the new @Nullable/@NonNull annotations. Perhaps you could just not do that? I mean, why use null checking if you don't want null checking??? Not trying to be a smart @$$, just feeling a bit confused about what you're trying to accomplish.
Re: Relaxing annotation based null checks on JDK8 [message #1696202 is a reply to message #1695814] Fri, 22 May 2015 19:49 Go to previous message
Eclipse UserFriend
If you are asking for a way to explicitly mark the return type as neither @NonNull nor @Nullable (what I call a legacy type), then: no JDT does not provide this option in conjunction with null type annotations.
Note that with this new level of null annotations we really want to provide a tool for catching all null problems, not for hiding existing null problems.

Seeing your attempt at Foo<@Nullable T> let's me ask: did you try this:
class Foo<T> 
{
    public @Nullable T bar()
    {
        return null;
    }
}


This might slightly improve the situation if other methods continue to use the unannotated type T (with pessimistic checking), and only this one method bar needs to be excluded from this regime.

If your intention during migration is: let clients believe the return is @NonNull while infact it is not, then s.t. as crude as the following might be a clear sign that your code is transitory:
class Foo<T> {
  /** EVERY USE OF THIS CONSTANT IS A BUG */
  @SuppressWarnings({ "null", "unchecked" })
  @Deprecated
  final @NonNull T stealthNull = (T) getStealthNull();
  
  Object getStealthNull() {
    return null;
  }

  T bar () {
     return stealthNull;
  }
}


But don't tell anybody that I recommended this Cool

HTH,
Stephan

PS: Support for using old-style null annotations in Java 8 has been improved in Mars.
Previous Topic:Launch Error in Java
Next Topic:Eclipse on Surface
Goto Forum:
  


Current Time: Sun Apr 20 09:17:07 EDT 2025

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

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

Back to the top