Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Java8 Nullness question: annotation is not applicable at this location(After I upgraded my project to Java8, I started getting new errors)
icon5.gif  Java8 Nullness question: annotation is not applicable at this location [message #1385672] Tue, 10 June 2014 12:16 Go to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Hi everyone,

First of all, if this is not the correct forum, please feel free to move the topic and/or let me know where I should post these questions.

I have recently upgraded my projects to Java8 and starting using the new and better type qualifiers. I am using Eclipse Standard / SDK Luna RC3 Release (4.4.0RC3) with the new annotations package (org.eclipse.jdt.annotation 2.0.0).

Below are some new errors/warning I get with this upgrade in my projects. I have tried my best to reduce the examples into minimal working code examples. If you need more details or the actual projects (to test is easier), please let me know.

1.
package "nullness" has the following package-info.java:
@org.eclipse.jdt.annotation.NonNullByDefault
package nullness;


"Generics.java":
package nullness;

public abstract class Generics<Ex extends Throwable>
{
    protected abstract void compute() throws Ex;
}


Here, on line
protected abstract void compute() throws Ex;
, I get the following error for "Ex":

Nullness annotations are not applicable at this location


I think they are related to generics I use (e.g., Ex) since if I change the code to:
package nullness;

public abstract class Generics<Ex extends Throwable>
{
    protected abstract void compute() throws Exception;
}


the error disappears.

I could not understand what the error means (to be honest it is rather vague). Does anyone have an idea what is causing the error and how I can fix it?

Thanks and best regards,
Re: Java8 Nullness question: annotation is not applicable at this location [message #1385675 is a reply to message #1385672] Tue, 10 June 2014 12:52 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
This looks like a bug, which is probably caused by a bad interaction between two factors:


  • @NonNullByDefault now affects more locations (tunable by its 'value' attribute)
  • types in a throws clause cannot be marked as nullable or nonnull (is nonnull by construction)


Please file a bug against JDT/Core with your examples and I will take a look.

As for a workaround: you may want to try adding this to method compute():
@NonNullByDefault({})


Regarding details of null type annotations, please see this new section in the help (temporary location): http://help.eclipse.org/staging/luna/topic/org.eclipse.jdt.doc.user/tasks/task-using_null_type_annotations.htm?cp=1_3_9_1

(or search for "null type annotations" in the online help since 4.4 RC4).

Thanks,
Stephan
Re: Java8 Nullness question: annotation is not applicable at this location [message #1385677 is a reply to message #1385675] Tue, 10 June 2014 13:05 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
I have written a longer reply, but for some reason the form decided to switch to "tree view" rather than posting my reply.

Thanks for the workaround, it works with one caveat:

The following code gives the same exception:
package nullness;

import org.eclipse.jdt.annotation.NonNullByDefault;

public abstract class Generics<Ex extends Throwable>
{
    @NonNullByDefault({})
    protected abstract void compute() throws Ex;
}


The following code works:
package nullness;

import org.eclipse.jdt.annotation.NonNullByDefault;

@NonNullByDefault({})
public abstract class Generics<Ex extends Throwable>
{
    protected abstract void compute() throws Ex;
}


I generally don't like raising the level of the annotations is that is not necessary, not sure whether it is a problem in this case.

I will create a bug report with my working example soon. Again, thanks for the help!
Re: Java8 Nullness question: annotation is not applicable at this location [message #1385681 is a reply to message #1385677] Tue, 10 June 2014 13:15 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
What you write let's me infer what the compiler is doing internally:

The nullness default expands "Ex extends Throwable" to "Ex extends @NonNull Throwable", good.

When analysing "throws Ex" the compiler sees that "Ex" is a nonnull type, OK.

It concludes that "Ex" has an explicit, illegal @NonNull annotation, wrong.

You may fine tune the workaround by excluding only one element from the nullness default: TYPE_BOUND, to make the declaration at class level:
@NonNullByDefault({ PARAMETER, RETURN_TYPE, FIELD, TYPE_ARGUMENT })


(hope it works, haven't tried ...)

Still only a workaround, but less drastic than your variant.

best,
Stephan
Re: Java8 Nullness question: annotation is not applicable at this location [message #1385682 is a reply to message #1385677] Tue, 10 June 2014 13:18 Go to previous message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
Stephan, I have just created the bug report:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=437050

Thanks,
Previous Topic:Connecting to SQL Server
Next Topic:System property Problem
Goto Forum:
  


Current Time: Tue May 21 10:27:58 GMT 2024

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

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

Back to the top