Bug 531040 - [10][quick assist] Convert type to 'var'
Summary: [10][quick assist] Convert type to 'var'
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.8   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: BETA J18.3   Edit
Assignee: Kalyan Prasad Tatavarthi CLA
QA Contact:
URL:
Whiteboard:
Keywords: noteworthy
Depends on: 532473
Blocks: 525750 532541
  Show dependency tree
 
Reported: 2018-02-12 08:41 EST by Noopur Gupta CLA
Modified: 2018-03-20 17:51 EDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Noopur Gupta CLA 2018-02-12 08:41:16 EST
Add a quick assist to convert a type to 'var'.

Examples:

String coolIde = "Eclipse";
into:
var coolIde = "Eclipse";

List<String> names = new LinkedList<>()
into:
var names = new LinkedList<String>()
Comment 1 Kalyan Prasad Tatavarthi CLA 2018-03-15 04:16:03 EDT
How do we handle Type annotations while converting to 'var'

For Example, please consider the below snippet: 

@Target(ElementType.TYPE_USE)
public @interface N {

}

@N String a = new String();


Now, how do we handle converting to 'var'

Conversion to 

var a= new String()

we lose the Type annotation.

One suggestion is Convert to :

var a= (@N String) new String();


Please weigh in on your suggestions.
Comment 2 Eclipse Genie CLA 2018-03-16 06:30:36 EDT
New Gerrit change created: https://git.eclipse.org/r/119548
Comment 3 Eclipse Genie CLA 2018-03-16 06:38:14 EDT
Gerrit change https://git.eclipse.org/r/119548 was merged to [BETA_JAVA_18_3].
Commit: http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=6a8f68da1bbbed60ccca77d6348268f1b6a85acb
Comment 4 Kalyan Prasad Tatavarthi CLA 2018-03-16 07:12:43 EDT
As Type Annotations are not applicable on 'var' type, the current behavior is that the type annotations will be removed.

If there is a need to handle type annotations during conversion of type to var, we will handle it in a separate bug.
Comment 5 Eclipse Genie CLA 2018-03-16 09:58:55 EDT
New Gerrit change created: https://git.eclipse.org/r/119578
Comment 6 Eclipse Genie CLA 2018-03-16 10:05:22 EDT
Gerrit change https://git.eclipse.org/r/119578 was merged to [BETA_JAVA_18_3].
Commit: http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=f6b7636f68643141505c3d4f84e45b519ad94130
Comment 7 Noopur Gupta CLA 2018-03-16 10:09:56 EDT
(In reply to Kalyan Prasad Tatavarthi from comment #4)
> As Type Annotations are not applicable on 'var' type, the current behavior
> is that the type annotations will be removed.
> 
> If there is a need to handle type annotations during conversion of type to
> var, we will handle it in a separate bug.
Sounds good.

(In reply to Eclipse Genie from comment #6)
> Gerrit change https://git.eclipse.org/r/119578 was merged to
> [BETA_JAVA_18_3].
> Commit:
> http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=f6b7636f68643141505c3d4f84e45b519ad94130
Added some more checks, removed unused field and its assignments, converted if-statements to blocks and some code clean-up.

Yet to review the changes in TypeChangeCorrectionProposal.java.
Comment 8 Noopur Gupta CLA 2018-03-20 06:18:49 EDT
(In reply to Noopur Gupta from comment #7)
> Yet to review the changes in TypeChangeCorrectionProposal.java.

Done. We also updated the code to remove type annotations with VariableDeclarationExpression along with some cleanup:

http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?h=BETA_JAVA_18_3&id=20f8f6638c32997a86f52fc265c8314b198e6322
Comment 9 Eclipse Genie CLA 2018-03-20 12:38:31 EDT
New Gerrit change created: https://git.eclipse.org/r/119803
Comment 10 Stephan Herrmann CLA 2018-03-20 12:39:52 EDT
(In reply to Kalyan Prasad Tatavarthi from comment #4)
> As Type Annotations are not applicable on 'var' type, the current behavior
> is that the type annotations will be removed.
> 
> If there is a need to handle type annotations during conversion of type to
> var, we will handle it in a separate bug.

Sorry for late response. I have no objections.

If a user wishes to specify a local variable using type annotations, using an explicit type is a necessary prerequisite. By saying 'var' the user gives full control to the compiler.

In particular with regard to null type annotations, the compiler will continue to use all its wit incl. flow analysis to figure out the nullness of the variable. 

(In reply to Eclipse Genie from comment #9)
> New Gerrit change created: https://git.eclipse.org/r/119803

This change adds a test to document / codify the current (good) behavior.
Comment 11 Eclipse Genie CLA 2018-03-20 17:51:14 EDT
Gerrit change https://git.eclipse.org/r/119803 was merged to [BETA_JAVA_18_3].
Commit: http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=604e0d84e4cabe094be7914b63bbb589ac97df73