Bug 383540 - [quick fix] Add quick fixes for issues arising from null-annotated fields
Summary: [quick fix] Add quick fixes for issues arising from null-annotated fields
Status: RESOLVED DUPLICATE of bug 398995
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.8   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: null beta
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-26 08:11 EDT by Stephan Herrmann CLA
Modified: 2013-01-24 14:47 EST (History)
3 users (show)

See Also:


Attachments
new quickfix: extract to null-checked local (35.16 KB, patch)
2012-06-26 08:11 EDT, Stephan Herrmann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Herrmann CLA 2012-06-26 08:11:48 EDT
Created attachment 217877 [details]
new quickfix: extract to null-checked local

This is spawned from bug 337977 comment 18 to separate out changes that are meaningful only in the context of bug 331649, which in turn lives in a topic branch, not in master.

From bug 337977 comment 18:

Here's one more quick fix: when dereferencing a @Nullable field:
- extract the field reference to a new local variable
- add a null check surrounding the dereference (both now using the local)

This is actually a matured version of an example from the JDT tutorial
at this year's ECNA :)  Thanks Ayush for the start!


I'm intending to include this in the patch feature on behalf of bug 331649.

At last this patch also contains some tests (for the new quickfix only).

The quickfix considers the following:
- statement inside a block / with no directly enclosing block
- don't propose the quickfix inside a field initializer
- don't propose names that are already in use in the current method/initializer
- always add an else block with a TODO comment
- if the statement to protect is a return, also add a return to the else block
- handle various kinds of types
  - reference / primitive
  - arrays
  - parameterized types
- various shapes of field references (single / qualified / this-qualified)
- field reference inside structures like ternary, assignment-as-expression
- dereference of array, indexed or ".length"


I'm not sure if we can/should go into suggesting an if statement that covers
several dereferences of the same field? Perhaps for subsequent occurrences
we could propose to extend coverage of an existing if statement..
Comment 1 Stephan Herrmann CLA 2013-01-24 14:47:48 EST
Oops, I completely forgot that I created this bug time ago. Anyway, let's continue in the new bug.

*** This bug has been marked as a duplicate of bug 398995 ***