Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 90140

Summary: [quick fix] for qualified enum constants in switch-case labels
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: UIAssignee: sandra lions <sandra.lions-piron>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: daniel_megert, hauser, jerome.cambon, manju656, markus.kell.r, noopur_gupta, sandra.lions-piron
Version: 3.1Flags: noopur_gupta: review+
Target Milestone: 4.5 M1   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Unit Test + Patch
none
Unit Test + Patch version 2
none
Updated Patch none

Description Markus Keller CLA 2005-04-04 06:29:03 EDT
 
Comment 1 Markus Keller CLA 2005-04-04 06:32:58 EDT
Example from the news.

The code below does not compile: "The enum constant EnumTester.color.black
reference cannot be qualified in a case label".

We could offer a quick fix which just removes the qualifier.

class EnumTester {

    public enum color {black, white}
    
    public void test(color c) {
        switch (c) {
            case color.black:
                System.out.println("Black");
                break;
            case color.white:
                System.out.println("White");
                break;
        }
    }
}
Comment 2 Dirk Baeumer CLA 2005-04-04 13:26:13 EDT
Martin, please resolve as later if out of scope for 3.1
Comment 3 Markus Keller CLA 2009-11-30 13:33:43 EST
*** Bug 296413 has been marked as a duplicate of this bug. ***
Comment 4 Markus Keller CLA 2009-11-30 13:36:52 EST
(In reply to bug 296413 comment #0)
> dropping "MyEnumClass." would be so easy

Implementation would be similar to LocalCorrectionsSubProcessor#addUnnecessaryInstanceofProposal(..)

Tests for this are in org.eclipse.jdt.ui.tests, in LocalCorrectionsQuickFixTest.
Comment 5 Jerome Cambon CLA 2014-05-19 06:21:47 EDT
You can assign this one to me.
Not sure to be clear about which part of this code should be removed ?
Comment 6 sandra lions CLA 2014-05-19 06:26:53 EDT
Actually I already started to work on this one.
I updated our internal wiki but forget to add a comment in bugzilla. 
Sorry for that :-(

You can assign the issue to me.
Comment 7 sandra lions CLA 2014-05-20 09:22:34 EDT
Created attachment 243292 [details]
Unit Test + Patch

This contribution complies with http://www.eclipse.org/legal/CoO.php
Comment 8 Noopur Gupta CLA 2014-05-27 09:39:54 EDT
(In reply to sandra lions from comment #7)
> Created attachment 243292 [details] [diff]
> Unit Test + Patch
> 
> This contribution complies with http://www.eclipse.org/legal/CoO.php

Patch looks good. 

Some review comments:

- Use the given template for updating the contributors list in the file header (https://wiki.eclipse.org/JDT_UI/How_to_Contribute#Coding_Conventions):
Your Name <email@example.com> - Bug Title - https://bugs.eclipse.org/BUG_NUMBER

- Local variable 'ast' can be inlined as it is used only once.

- Since enum constants cannot be surrounded by parenthesis, a case like "case ((color.white)): ..." would not be legal. Still, the quick fix can handle the parenthesis removal also in such a case. => Extract the qualifiedName from ParenthesizedExpression (see LocalCorrectionsSubProcessor#addUnnecessaryInstanceofProposal(..)) and replace the original coveringNode with simpleName.
Comment 9 sandra lions CLA 2014-05-28 12:11:32 EDT
Created attachment 243645 [details]
Unit Test + Patch version 2

Take into account Noopur Gupta comments (comment 8):
- fixed contributors list
- handle quick fix for enum surrounded with parenthesis.
  Note that I have a question in the java code (see LocalCorrectionsSubProcessor.addEnumConstantsCannotBeSurroundedByParenthesisProposal)
- not sure to understand what you mean by : Local variable 'ast' can be inlined as it is used only once.
  Do you mean add some linked proposals in order to rename the enum ? Just like the Assign parameter to existing field quick fix ?
Comment 10 Noopur Gupta CLA 2014-05-29 06:08:09 EDT
Created attachment 243673 [details]
Updated Patch

(In reply to sandra lions from comment #9)
I have updated your fix and tests with changes required in comment #8.
Please have a look.
- Contributors list was still not updated in all the changed files. Also, notice the exact template format.
- "Local variable 'ast' can be inlined as it is used only once." => Referred to the local variable 'ast' that was present in LocalCorrectionsSubProcessor#addIllegalQualifiedEnumConstantLabelProposal.
- "the quick fix can handle the parenthesis case" => Referred to updating the same quick fix so that it is also available in examples like:
    "case (color.white): ..."
Comment 11 sandra lions CLA 2014-06-02 08:50:49 EDT
Hi Noopur,
I tried your updated patch and have 2 questions :

- when we get a qualified enum surrounded with parenthesis:
	case ((color.black)):
we do not get the light icon in the left margin.
I thought this icon was needed to indicate a quick fix was available, is it ? 
That’s the reason I added the case IProblem.EnumConstantsCannotBeSurroundedByParenthesis in the QuickFixPorcessor.java.

- when we get an unqualified enum surrounded with parenthesis:
	case ((black)):
we do not get the quick fix anymore (in order to simply remove the parenthesis). 
I first wanted to propose a quick fix for both cases (qualified enum AND/OR enum surrounded with parenthesis), 
but I agree, the parenthesis quick fix is larger than the scope of the current issue.
Do you prefer I log another issue to track the enum parenthesis quick fix ?

Thanks for your feedback,
sandra
Comment 12 Noopur Gupta CLA 2014-06-03 08:33:13 EDT
(In reply to sandra lions from comment #11)

> - when we get a qualified enum surrounded with parenthesis:
> 	case ((color.black)):
> we do not get the light icon in the left margin.
In such a case, we have multiple markers (errors) at that line - You can see that when you hover on the error marker in the vertical ruler. 
Hence, though the light bulb exists, it is hidden behind the other error marker, and is not directly visible. 
You can view the light bulb if you check the preference option: Java > Editor > Hovers > Expand vertical ruler icons upon hovering. Then, re-open the editor and hover on the vertical ruler.

> - when we get an unqualified enum surrounded with parenthesis:
> 	case ((black)):
> we do not get the quick fix anymore (in order to simply remove the
> parenthesis). 
> but I agree, the parenthesis quick fix is larger than the scope of the
> current issue.
> Do you prefer I log another issue to track the enum parenthesis quick fix ?
The other quick fix (that only removes the parenthesis) is not required as part of this bug. Also, it is a rare scenario and not of much relevance. If required, we can log another bug for that later.