Community
Participate
Working Groups
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/
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?
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.
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?
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.
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".
(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.
Gerrit change https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/56876 was merged to [master]. Commit: http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=a0f84843bb1b6f5636679aac948e3fd8fda5b64f
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/192385
Gerrit change https://git.eclipse.org/r/c/jdt/eclipse.jdt.ui/+/192385 was merged to [master]. Commit: http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=6cadbcd988f5291040f939fe5cf3ba8070526722