Bug 440306 - [null]Quickfix extract to checked local variable on Potential null pointer access
Summary: [null]Quickfix extract to checked local variable on Potential null pointer ac...
Status: REOPENED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.4   Edit
Hardware: PC Linux
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: helpwanted
Depends on:
Blocks: 427423
  Show dependency tree
 
Reported: 2014-07-24 05:16 EDT by Holger Klene CLA
Modified: 2022-09-05 00:15 EDT (History)
1 user (show)

See Also:


Attachments
Demo-sourcecode (1.25 KB, text/plain)
2014-07-24 05:17 EDT, Holger Klene CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Holger Klene CLA 2014-07-24 05:16:12 EDT
The quickfix "extract to checked local variable" has multiple issues:

a) It cannot handle generics: <Object> instead of <T> in line 27
b) It cannot detect if there is already an immediately surrounding null-check (it will just stack the ifs)
c) It only applies to 110 annotation error "Potential null pointer access: The field list is specified as @Nullable" but not to 200 annotation error "Potential null pointer access: this expression has a '@Nullable' type".
d) The other quickfixes "extract to local variable" and its companion "extract local variable (replace all occurrences)" handle generics just fine, but won't insert the extra if for the null-check if it's not present.

Can you please try to:
A) Make it handle generics.
B) Check the immediately preceding statement, if it already is a null check (don't offer "extract to checked local variable" but instead fall back to "extract to local variable").
C) Also apply Quickfix to 200 error

Tested with
org.eclipse.jdt.annotation_1.1.0.v20130513-1648.jar and
org.eclipse.jdt.annotation_2.0.0.v20140415-1436.jar
Comment 1 Holger Klene CLA 2014-07-24 05:17:24 EDT
Created attachment 245329 [details]
Demo-sourcecode
Comment 2 Stephan Herrmann CLA 2014-07-24 06:01:25 EDT
(In reply to Holger Klene from comment #0)
> Can you please try to:
> A) Make it handle generics.

The problem is specific to the use of a type variable as a type argument. List<String>, e.g., is handled correctly.
From a quick comparison it looks like org.eclipse.jdt.internal.ui.text.correction.proposals.ExtractToNullCheckedLocalProposal.newType(ITypeBinding, AST, ImportRewrite) is doing a lot of stuff (incompletely it seems) that is better delegated to ImportRewrite.addImport().


> B) Check the immediately preceding statement, if it already is a null check
> (don't offer "extract to checked local variable" but instead fall back to
> "extract to local variable").

Not sure how much effort this would cost.

> C) Also apply Quickfix to 200 error

Should be done as part of bug 427423.
Comment 3 Eclipse Genie CLA 2020-04-16 03:19:45 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 4 Stephan Herrmann CLA 2020-04-16 06:39:32 EDT
At least issue (A) should be addressed.
Comment 5 Stephan Herrmann CLA 2020-06-10 03:35:05 EDT
.
Comment 6 Eclipse Genie CLA 2022-09-05 00:15:09 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.