Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Typesafety comiler bug(Oxygen.2 produces no error when using optional)
Typesafety comiler bug [message #1783205] Fri, 09 March 2018 08:22 Go to next message
Veselin Markov is currently offline Veselin MarkovFriend
Messages: 15
Registered: February 2012
Junior Member
Hello

why does the following code compile?


Optional<List<?>> nullableList = Optional.ofNullable(null);
Optional<String> nullableSize = Optional.ofNullable(nullableList.map(e -> e.size()));
Re: Typesafety comiler bug [message #1783279 is a reply to message #1783205] Sat, 10 March 2018 16:28 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
This compiles, because the compiler looks at signatures, not javadoc :)
From the signatures of Optional and its method, there is no indication that this code is more prone to NPE than any other piece of code in Java.

If you want to help the Eclipse compiler to recognize the risk of NPE in this kind of usage, enable annotation-based null analysis, and provide external annotations that capture the semantics of class Optional. You may want to try this Optional.eea whether it already covers your case.

If you are new to null annotations, and external annotations in particular, you'll find some entry points in https://wiki.eclipse.org/JDT_Core/Null_Analysis and http://www.lastnpe.org/

HTH
Stephan
Re: Typesafety comiler bug [message #1783447 is a reply to message #1783279] Tue, 13 March 2018 09:39 Go to previous messageGo to next message
Veselin Markov is currently offline Veselin MarkovFriend
Messages: 15
Registered: February 2012
Junior Member
Hi,

my concern wasn't the null check. I think that var nullableSize must be of type Optional<Optional<Integer>> instead of Optional<String> and that the compiler have to show an error in this line.
Re: Typesafety comiler bug [message #1783473 is a reply to message #1783447] Tue, 13 March 2018 13:43 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Well, if that's what you are asking, what about this answer from ecj:
----------
1. ERROR in /tmp/X.java (at line 6)
        Optional<String> nullableSize = Optional.ofNullable(nullableList.map(e -> e.size()));
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from Optional<Optional<Object>> to Optional<String>
----------
1 problem (1 error)

Re: Typesafety comiler bug [message #1783580 is a reply to message #1783205] Wed, 14 March 2018 11:00 Go to previous messageGo to next message
Veselin Markov is currently offline Veselin MarkovFriend
Messages: 15
Registered: February 2012
Junior Member
I'm not sure where did you get the output, but I expected that I get this in eclipse in the java editor. Instead it says everything's fine: see attachment.
  • Attachment: no_error.PNG
    (Size: 27.27KB, Downloaded 91 times)
Re: Typesafety comiler bug [message #1783673 is a reply to message #1783580] Thu, 15 March 2018 12:47 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
I used the batch compiler version of ecj, but surely in the IDE the compiler will give the same answer.
Some things to check on your side:
- is the compiler invoked? Make sure "Build automatically" is checked, and/or trigger a build via "Project > Clean"
- do editor and Problems View show the same issues (I see two warnings in the editor - is it the same in the Problems View)?
- what exact version of plug-in org.eclipse.jdt.core have you installed (see About > Installation Details > Plug-ins)
- if you insert a main method, can the program be launched? Can method error() be invoked?
- ...
Re: Typesafety comiler bug [message #1785461 is a reply to message #1783205] Sat, 14 April 2018 07:04 Go to previous messageGo to next message
Veselin Markov is currently offline Veselin MarkovFriend
Messages: 15
Registered: February 2012
Junior Member
I reproduced the problemat work and at home and checked that everythin is compiled. For me it still seems like a bug. If calling the compiler directly produces compile error and the java editor doesn't show it, then it seems to be an editor problem. Did you try to reproduce the problem in Eclipse?
  • Attachment: Unbenannt.png
    (Size: 150.52KB, Downloaded 88 times)
Re: Typesafety comiler bug [message #1785603 is a reply to message #1785461] Tue, 17 April 2018 12:21 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Quote:
Did you try to reproduce the problem in Eclipse?


Yes, same result as in my first comment, i.e. ecj correctly reports a type error -- in the IDE just like during batch compilation.

S.t. must be broken either in your installation or your workspace. See my previous comment for some question that might bring us closer to understanding the problem.
Previous Topic:Can't run java program in Eclipse - javaw error
Next Topic:Replacement of package org.eclipse.osgi.framework.internal.core.BundleFragment /FrameworkProperties
Goto Forum:
  


Current Time: Thu Mar 28 17:34:23 GMT 2024

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

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

Back to the top