Bug 478563 - Generate Optional.empty() instead of "null"
Summary: Generate Optional.empty() instead of "null"
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.6   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-09-28 12:18 EDT by Jens Reimann CLA
Modified: 2022-03-30 22:05 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jens Reimann CLA 2015-09-28 12:18:46 EDT
I just made a patch in gerrit [1] which creates:

   return Optional.empty();

instead of

   return null;

for methods which use the new Optional type.

[1] https://git.eclipse.org/r/#/c/56876/
Comment 1 Markus Keller CLA 2015-09-28 14:24:32 EDT
All JDT commit messages have to start with "Bug ###". I've fixed that in the Gerrit.

The second part of

    if ("java.util.Optional".equals(fqn) || "Optional".equals(fqn))

is not correct. The way to go would be via type.resolveBinding() and then getTypeDeclaration().getQualifiedName(). However, not all code paths call this method with an AST that has bindings, so I don't think we can/should add special handling for Optional in that method.

We could add this to ASTNodeFactory.newDefaultExpression(AST, ITypeBinding). With proper Javadoc and formatting: '{' is not on a separate line.

Note that ASTNodeFactory is not API and should not be referenced by client code. What is your concrete use case?
Comment 2 Jens Reimann CLA 2015-09-28 14:42:53 EDT
I feared that already.

I do want to have the jdt generate methods (like the create implementation action) returning a non-null value in the case of an Optional.

Returning null if the type is java.util.Optional seems pretty wrong to me. So Eclipse jdt should generate Optional.empty() instead of null if the return type is "Optional".

I traced the code path to exactly this method and found that the resolve methods all return null.

Meanwhile I did find the second method, including the full type info, but this one is not getting called by the generate implementation action.
Comment 3 Jens Reimann CLA 2015-09-29 03:58:14 EDT
I just uploaded Patch #3.

I did make the same change for the second "newDefaultExpression" method, the one with the ITypeBinding parameter.

This works now for the quick fix to add a missing parameter in a method call.

But again I ran into an issue which needs improvement. At this point I do know the type is "java.util.Optional" and I can make a check in the full qualified name, however inserting a call to "java.util.Optional.empty()" seems a bit unnecessary if the package "java.util" or the type "java.util.Optional" is already imported. Is there a way to check if the simple type name may be used instead?
Comment 4 Markus Keller CLA 2015-11-03 11:45:59 EST
To create a Type node out of an ITypeBinding, you have to use ImportRewrite#addImport(..). Callers need to pass an ImportRewrite and an ImportRewriteContext. See the other methods in ASTNodeFactory for examples.
Comment 5 Jens Reimann CLA 2015-11-03 12:21:46 EST
But when I am in the method "ASTNodeFactory#newDefaultExpression(AST ast, Type type, int extraDimensions)" I already have a Type node, but from that I would like to get a an ITypeBinding.

I can call Type.resolveBinding() in order to resolve this, but this method returns "null".
Comment 6 Markus Keller CLA 2015-11-04 08:25:03 EST
(In reply to Jens Reimann from comment #5)
Yes, that's what I said in comment 1. You have to check callers of that method to see how it's used. If the node is a new ASTNode that was not generated by the parser, then it doesn't have bindings. But if it's a ParameterizedType whose getType() is a reference to Optional, then the structure of that node will tell you if Optional needs qualification or not.

If the calling code is correct, then it already used ImportRewrite#addImport(..) to create that Type node.
Comment 8 Eclipse Genie CLA 2022-03-30 21:02:04 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/192385