Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 90140 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java (+157 lines)
Lines 10-15 Link Here
10
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] Shouldn't offer "Add throws declaration" quickfix for overriding signature if result would conflict with overridden signature
10
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] Shouldn't offer "Add throws declaration" quickfix for overriding signature if result would conflict with overridden signature
11
 *     Lukas Hanke <hanke@yatta.de> - Bug 241696 [quick fix] quickfix to iterate over a collection - https://bugs.eclipse.org/bugs/show_bug.cgi?id=241696
11
 *     Lukas Hanke <hanke@yatta.de> - Bug 241696 [quick fix] quickfix to iterate over a collection - https://bugs.eclipse.org/bugs/show_bug.cgi?id=241696
12
 *     Lukas Hanke <hanke@yatta.de> - Bug 430818 [1.8][quick fix] Quick fix for "for loop" is not shown for bare local variable/argument/field - https://bugs.eclipse.org/bugs/show_bug.cgi?id=430818
12
 *     Lukas Hanke <hanke@yatta.de> - Bug 430818 [1.8][quick fix] Quick fix for "for loop" is not shown for bare local variable/argument/field - https://bugs.eclipse.org/bugs/show_bug.cgi?id=430818
13
 *     Sandra Lions <sandra.lions-piron@oracle.com> - Bug 90140 - [quick fix] for qualified enum constants in switch-case labels - https://bugs.eclipse.org/bugs/show_bug.cgi?id=90140
13
 *******************************************************************************/
14
 *******************************************************************************/
14
package org.eclipse.jdt.ui.tests.quickfix;
15
package org.eclipse.jdt.ui.tests.quickfix;
15
16
Lines 9173-9178 public class LocalCorrectionsQuickFixTest extends QuickFixTest { Link Here
9173
		assertExpectedExistInProposals(proposals, expected);
9174
		assertExpectedExistInProposals(proposals, expected);
9174
	}
9175
	}
9175
9176
9177
	public void testReplaceWithoutSurroundedParenthesis() throws Exception {
9178
		IPackageFragment pack1= fSourceFolder.createPackageFragment("pack", false, null);
9179
		StringBuffer buf= new StringBuffer();
9180
		buf.append("package pack;\n");
9181
		buf.append("public class E {\n");
9182
		buf.append("    public enum color {black, white}\n");
9183
		buf.append("    public void foo(color c) {\n");
9184
		buf.append("		switch (c) {\n");
9185
		buf.append("            case (black):\n");
9186
		buf.append("                System.out.println(\"Black\");\n");
9187
		buf.append("                break;\n");
9188
		buf.append("            case white:\n");
9189
		buf.append("                System.out.println(\"White\");\n");
9190
		buf.append("                break;\n");
9191
		buf.append("            default:\n");
9192
		buf.append("                break;\n");
9193
		buf.append("        }\n");
9194
		buf.append("    }\n");
9195
		buf.append("}\n");
9196
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
9197
9198
		CompilationUnit astRoot= getASTRoot(cu);
9199
		ArrayList proposals= collectCorrections(cu, astRoot, 1);
9200
9201
		assertNumberOfProposals(proposals, 1);
9202
		assertCorrectLabels(proposals);
9203
9204
		CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0);
9205
		String preview= getPreviewContent(proposal);
9206
9207
		buf= new StringBuffer();
9208
		buf.append("package pack;\n");
9209
		buf.append("public class E {\n");
9210
		buf.append("    public enum color {black, white}\n");
9211
		buf.append("    public void foo(color c) {\n");
9212
		buf.append("		switch (c) {\n");
9213
		buf.append("            case black:\n");
9214
		buf.append("                System.out.println(\"Black\");\n");
9215
		buf.append("                break;\n");
9216
		buf.append("            case white:\n");
9217
		buf.append("                System.out.println(\"White\");\n");
9218
		buf.append("                break;\n");
9219
		buf.append("            default:\n");
9220
		buf.append("                break;\n");
9221
		buf.append("        }\n");
9222
		buf.append("    }\n");
9223
		buf.append("}\n");
9224
		assertEqualString(preview, buf.toString());
9225
		String expected= buf.toString();
9226
		assertExpectedExistInProposals(proposals, new String[] { expected });
9227
	}
9228
	
9229
	public void testReplaceWithUnqualifiedEnumConstant() throws Exception {
9230
		IPackageFragment pack1= fSourceFolder.createPackageFragment("pack", false, null);
9231
		StringBuffer buf= new StringBuffer();
9232
		buf.append("package pack;\n");
9233
		buf.append("public class E {\n");
9234
		buf.append("    public enum color {black, white}\n");
9235
		buf.append("    public void foo(color c) {\n");
9236
		buf.append("		switch (c) {\n");
9237
		buf.append("            case color.black:\n");
9238
		buf.append("                System.out.println(\"Black\");\n");
9239
		buf.append("                break;\n");
9240
		buf.append("            case white:\n");
9241
		buf.append("                System.out.println(\"White\");\n");
9242
		buf.append("                break;\n");
9243
		buf.append("            default:\n");
9244
		buf.append("                break;\n");
9245
		buf.append("        }\n");
9246
		buf.append("    }\n");
9247
		buf.append("}\n");
9248
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
9249
9250
		CompilationUnit astRoot= getASTRoot(cu);
9251
		ArrayList proposals= collectCorrections(cu, astRoot, 1);
9252
9253
		assertNumberOfProposals(proposals, 1);
9254
		assertCorrectLabels(proposals);
9255
9256
		CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0);
9257
		String preview= getPreviewContent(proposal);
9258
9259
		buf= new StringBuffer();
9260
		buf.append("package pack;\n");
9261
		buf.append("public class E {\n");
9262
		buf.append("    public enum color {black, white}\n");
9263
		buf.append("    public void foo(color c) {\n");
9264
		buf.append("		switch (c) {\n");
9265
		buf.append("            case black:\n");
9266
		buf.append("                System.out.println(\"Black\");\n");
9267
		buf.append("                break;\n");
9268
		buf.append("            case white:\n");
9269
		buf.append("                System.out.println(\"White\");\n");
9270
		buf.append("                break;\n");
9271
		buf.append("            default:\n");
9272
		buf.append("                break;\n");
9273
		buf.append("        }\n");
9274
		buf.append("    }\n");
9275
		buf.append("}\n");
9276
		assertEqualString(preview, buf.toString());
9277
		String expected= buf.toString();
9278
		assertExpectedExistInProposals(proposals, new String[] { expected });
9279
	}
9280
	
9281
	public void testReplaceWithUnqualifiedEnumConstantWithoutParenthesis() throws Exception {
9282
		IPackageFragment pack1= fSourceFolder.createPackageFragment("pack", false, null);
9283
		StringBuffer buf= new StringBuffer();
9284
		buf.append("package pack;\n");
9285
		buf.append("public class E {\n");
9286
		buf.append("    public enum color {black, white}\n");
9287
		buf.append("    public void foo(color c) {\n");
9288
		buf.append("		switch (c) {\n");
9289
		buf.append("            case (color.black):\n");
9290
		buf.append("                System.out.println(\"Black\");\n");
9291
		buf.append("                break;\n");
9292
		buf.append("            case white:\n");
9293
		buf.append("                System.out.println(\"White\");\n");
9294
		buf.append("                break;\n");
9295
		buf.append("            default:\n");
9296
		buf.append("                break;\n");
9297
		buf.append("        }\n");
9298
		buf.append("    }\n");
9299
		buf.append("}\n");
9300
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
9301
9302
		CompilationUnit astRoot= getASTRoot(cu);
9303
		ArrayList proposals= collectCorrections(cu, astRoot, 2, 0); //quick fix on 1st problem
9304
9305
		assertNumberOfProposals(proposals, 1);
9306
		assertCorrectLabels(proposals);
9307
9308
		CUCorrectionProposal proposal= (CUCorrectionProposal)proposals.get(0);
9309
		String preview= getPreviewContent(proposal);
9310
9311
		buf= new StringBuffer();
9312
		buf.append("package pack;\n");
9313
		buf.append("public class E {\n");
9314
		buf.append("    public enum color {black, white}\n");
9315
		buf.append("    public void foo(color c) {\n");
9316
		buf.append("		switch (c) {\n");
9317
		buf.append("            case black:\n");
9318
		buf.append("                System.out.println(\"Black\");\n");
9319
		buf.append("                break;\n");
9320
		buf.append("            case white:\n");
9321
		buf.append("                System.out.println(\"White\");\n");
9322
		buf.append("                break;\n");
9323
		buf.append("            default:\n");
9324
		buf.append("                break;\n");
9325
		buf.append("        }\n");
9326
		buf.append("    }\n");
9327
		buf.append("}\n");
9328
		assertEqualString(preview, buf.toString());
9329
		String expected= buf.toString();
9330
		assertExpectedExistInProposals(proposals, new String[] { expected });
9331
	}
9332
	
9176
	public void testCollectionsFieldMethodReplacement() throws Exception {
9333
	public void testCollectionsFieldMethodReplacement() throws Exception {
9177
		Hashtable options= JavaCore.getOptions();
9334
		Hashtable options= JavaCore.getOptions();
9178
		options.put(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION, JavaCore.WARNING);
9335
		options.put(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION, JavaCore.WARNING);
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java (+2 lines)
Lines 392-397 public final class CorrectionMessages extends NLS { Link Here
392
	public static String LocalCorrectionsSubProcessor_insert_cases_omitted;
392
	public static String LocalCorrectionsSubProcessor_insert_cases_omitted;
393
	public static String LocalCorrectionsSubProcessor_insert_fall_through;
393
	public static String LocalCorrectionsSubProcessor_insert_fall_through;
394
	public static String LocalCorrectionsSubProcessor_override_hashCode_description;
394
	public static String LocalCorrectionsSubProcessor_override_hashCode_description;
395
	public static String LocalCorrectionsSubProcessor_replace_with_unqualified_enum_constant;
396
	public static String LocalCorrectionsSubProcessor_replace_without_surrounded_parenthesis;
395
	public static String LocalCorrectionsSubProcessor_throw_allocated_description;
397
	public static String LocalCorrectionsSubProcessor_throw_allocated_description;
396
	public static String SuppressWarningsSubProcessor_fix_suppress_token_label;
398
	public static String SuppressWarningsSubProcessor_fix_suppress_token_label;
397
	public static String SuppressWarningsSubProcessor_remove_annotation_label;
399
	public static String SuppressWarningsSubProcessor_remove_annotation_label;
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties (+2 lines)
Lines 117-122 LocalCorrectionsSubProcessor_qualify_left_hand_side_description=Qualify left han Link Here
117
LocalCorrectionsSubProcessor_replacefieldaccesswithmethod_description=Replace with ''{0}''
117
LocalCorrectionsSubProcessor_replacefieldaccesswithmethod_description=Replace with ''{0}''
118
LocalCorrectionsSubProcessor_return_allocated_description=Return the allocated object
118
LocalCorrectionsSubProcessor_return_allocated_description=Return the allocated object
119
LocalCorrectionsSubProcessor_throw_allocated_description=Throw the allocated object
119
LocalCorrectionsSubProcessor_throw_allocated_description=Throw the allocated object
120
LocalCorrectionsSubProcessor_replace_with_unqualified_enum_constant=Replace with unqualified enum constant ''{0}''
121
LocalCorrectionsSubProcessor_replace_without_surrounded_parenthesis=Replace without surrounded parenthesis
120
TypeMismatchSubProcessor_addcast_description=Add cast to ''{0}''
122
TypeMismatchSubProcessor_addcast_description=Add cast to ''{0}''
121
TypeMismatchSubProcessor_changecast_description=Change cast to ''{0}''
123
TypeMismatchSubProcessor_changecast_description=Change cast to ''{0}''
122
TypeMismatchSubProcessor_changereturntype_description=Change method return type to ''{0}''
124
TypeMismatchSubProcessor_changereturntype_description=Change method return type to ''{0}''
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java (+2 lines)
Lines 180-185 public interface IProposalRelevance { Link Here
180
	public static final int CORRECT_PACKAGE_DECLARATION= 5;
180
	public static final int CORRECT_PACKAGE_DECLARATION= 5;
181
	public static final int TYPE_ARGUMENTS_FROM_CONTEXT= 5;
181
	public static final int TYPE_ARGUMENTS_FROM_CONTEXT= 5;
182
	public static final int REMOVE_REDUNDANT_NULLNESS_ANNOTATION= 5;
182
	public static final int REMOVE_REDUNDANT_NULLNESS_ANNOTATION= 5;
183
	public static final int REPLACE_WITH_UNQUALIFIED_ENUM_CONSTANT= 5;
184
	public static final int REPLACE_WITHOUT_SURROUNDED_PARENTHESIS= 5;
183
185
184
	public static final int ADD_MISSING_TAG= 4;
186
	public static final int ADD_MISSING_TAG= 4;
185
	public static final int INSERT_FALL_THROUGH= 4;
187
	public static final int INSERT_FALL_THROUGH= 4;
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java (+59 lines)
Lines 10-15 Link Here
10
 *     Renaud Waldura &lt;renaud+eclipse@waldura.com&gt; - Access to static proposal
10
 *     Renaud Waldura &lt;renaud+eclipse@waldura.com&gt; - Access to static proposal
11
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] Shouldn't offer "Add throws declaration" quickfix for overriding signature if result would conflict with overridden signature
11
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] Shouldn't offer "Add throws declaration" quickfix for overriding signature if result would conflict with overridden signature
12
 *     Lukas Hanke <hanke@yatta.de> - Bug 241696 [quick fix] quickfix to iterate over a collection - https://bugs.eclipse.org/bugs/show_bug.cgi?id=241696
12
 *     Lukas Hanke <hanke@yatta.de> - Bug 241696 [quick fix] quickfix to iterate over a collection - https://bugs.eclipse.org/bugs/show_bug.cgi?id=241696
13
 *     Sandra Lions <sandra.lions-piron@oracle.com> - Bug 90140 - [quick fix] for qualified enum constants in switch-case labels - https://bugs.eclipse.org/bugs/show_bug.cgi?id=90140
13
 *******************************************************************************/
14
 *******************************************************************************/
14
package org.eclipse.jdt.internal.ui.text.correction;
15
package org.eclipse.jdt.internal.ui.text.correction;
15
16
Lines 765-770 public class LocalCorrectionsSubProcessor { Link Here
765
		}
766
		}
766
767
767
	}
768
	}
769
	
770
	public static void addEnumConstantsCannotBeSurroundedByParenthesisProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
771
		ASTNode coveringNode= problem.getCoveringNode(context.getASTRoot());
772
		if (!(coveringNode instanceof ParenthesizedExpression)) {
773
			return;
774
		}
775
		ParenthesizedExpression parenthesizedExpression= ((ParenthesizedExpression) coveringNode);
776
		Expression expression= parenthesizedExpression.getExpression();
777
		
778
		while (expression instanceof ParenthesizedExpression) {
779
			expression= ((ParenthesizedExpression) expression).getExpression();
780
		}
781
782
		if (expression instanceof SimpleName) {
783
			SimpleName simpleName= (SimpleName) expression;
784
785
			AST ast= coveringNode.getAST();
786
			ASTRewrite rewrite= ASTRewrite.create(ast);
787
			rewrite.replace(coveringNode, simpleName, null);
788
789
			String label= CorrectionMessages.LocalCorrectionsSubProcessor_replace_without_surrounded_parenthesis;
790
			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
791
			ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_WITHOUT_SURROUNDED_PARENTHESIS, image);
792
			proposals.add(proposal);
793
		} else if (expression instanceof QualifiedName) {
794
			QualifiedName qualifiedName= (QualifiedName) expression;
795
			SimpleName simpleName= qualifiedName.getName();
796
797
			AST ast= coveringNode.getAST();
798
			ASTRewrite rewrite= ASTRewrite.create(ast);
799
			rewrite.replace(coveringNode, simpleName, null);
800
801
			// Do we need to define another message here (for both corrections) ? something like :
802
			// Replace with unqualified enum constant ''{0}'' AND without surrounded parenthesis
803
			String label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_replace_with_unqualified_enum_constant, BasicElementLabels.getJavaElementName(simpleName.getIdentifier()));
804
			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
805
			ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_WITHOUT_SURROUNDED_PARENTHESIS, image);
806
			proposals.add(proposal);
807
		}
808
	}
809
810
	public static void addIllegalQualifiedEnumConstantLabelProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
811
		ASTNode coveringNode= problem.getCoveringNode(context.getASTRoot());
812
		
813
		if (coveringNode instanceof QualifiedName){
814
			QualifiedName qualifiedName= (QualifiedName) coveringNode;
815
			SimpleName simpleName= qualifiedName.getName();
816
817
			AST ast= coveringNode.getAST();
818
			ASTRewrite rewrite= ASTRewrite.create(ast);
819
			rewrite.replace(coveringNode, simpleName, null);
820
821
			String label= Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_replace_with_unqualified_enum_constant, BasicElementLabels.getJavaElementName(simpleName.getIdentifier()));
822
			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
823
			ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REPLACE_WITH_UNQUALIFIED_ENUM_CONSTANT, image);
824
			proposals.add(proposal);
825
		}
826
	}
768
827
769
	public static void addUnnecessaryThrownExceptionProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
828
	public static void addUnnecessaryThrownExceptionProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
770
		ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
829
		ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickFixProcessor.java (+8 lines)
Lines 95-100 public class QuickFixProcessor implements IQuickFixProcessor { Link Here
95
			case IProblem.AbstractMethodsInConcreteClass:
95
			case IProblem.AbstractMethodsInConcreteClass:
96
			case IProblem.AbstractMethodInEnum:
96
			case IProblem.AbstractMethodInEnum:
97
			case IProblem.EnumConstantMustImplementAbstractMethod:
97
			case IProblem.EnumConstantMustImplementAbstractMethod:
98
			case IProblem.EnumConstantsCannotBeSurroundedByParenthesis:
98
			case IProblem.ShouldImplementHashcode:
99
			case IProblem.ShouldImplementHashcode:
99
			case IProblem.BodyForNativeMethod:
100
			case IProblem.BodyForNativeMethod:
100
			case IProblem.OuterLocalMustBeFinal:
101
			case IProblem.OuterLocalMustBeFinal:
Lines 161-166 public class QuickFixProcessor implements IQuickFixProcessor { Link Here
161
			case IProblem.DuplicateMethod:
162
			case IProblem.DuplicateMethod:
162
			case IProblem.DuplicateTypeVariable:
163
			case IProblem.DuplicateTypeVariable:
163
			case IProblem.DuplicateNestedType:
164
			case IProblem.DuplicateNestedType:
165
			case IProblem.IllegalQualifiedEnumConstantLabel:
164
			case IProblem.IllegalModifierForInterfaceMethod:
166
			case IProblem.IllegalModifierForInterfaceMethod:
165
			case IProblem.IllegalModifierForInterfaceMethod18:
167
			case IProblem.IllegalModifierForInterfaceMethod18:
166
			case IProblem.IllegalModifierForInterface:
168
			case IProblem.IllegalModifierForInterface:
Lines 760-765 public class QuickFixProcessor implements IQuickFixProcessor { Link Here
760
				NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.LOCAL, proposals);
762
				NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.LOCAL, proposals);
761
				NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.INVERSE, proposals);
763
				NullAnnotationsCorrectionProcessor.addReturnAndArgumentTypeProposal(context, problem, ChangeKind.INVERSE, proposals);
762
				break;
764
				break;
765
			case IProblem.EnumConstantsCannotBeSurroundedByParenthesis:
766
				LocalCorrectionsSubProcessor.addEnumConstantsCannotBeSurroundedByParenthesisProposal(context, problem, proposals);
767
				break;
768
			case IProblem.IllegalQualifiedEnumConstantLabel:
769
				LocalCorrectionsSubProcessor.addIllegalQualifiedEnumConstantLabelProposal(context, problem, proposals);
770
				break;
763
			default:
771
			default:
764
		}
772
		}
765
		if (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
773
		if (JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {

Return to bug 90140