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 216898
Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ModifierCorrectionsQuickFixTest.java (+40 lines)
Lines 9-14 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] 'Remove invalid modifiers' does not appear for enums and annotations - https://bugs.eclipse.org/bugs/show_bug.cgi?id=110589
10
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] 'Remove invalid modifiers' does not appear for enums and annotations - https://bugs.eclipse.org/bugs/show_bug.cgi?id=110589
11
 *     Benjamin Muskalla <b.muskalla@gmx.net> - [quick fix] Quick fix for missing synchronized modifier - https://bugs.eclipse.org/bugs/show_bug.cgi?id=245250
11
 *     Benjamin Muskalla <b.muskalla@gmx.net> - [quick fix] Quick fix for missing synchronized modifier - https://bugs.eclipse.org/bugs/show_bug.cgi?id=245250
12
 *     Rabea Gransberger <rgransberger@gmx.de> - [quick fix] QuickFix proposes wrong visibility for overriding/overridden method - https://bugs.eclipse.org/bugs/show_bug.cgi?id=216898
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.jdt.ui.tests.quickfix;
14
package org.eclipse.jdt.ui.tests.quickfix;
14
15
Lines 1655-1660 Link Here
1655
		buf.append("}\n");
1656
		buf.append("}\n");
1656
		assertEqualString(preview, buf.toString());
1657
		assertEqualString(preview, buf.toString());
1657
	}
1658
	}
1659
	
1660
	public void testOverridingMethodIsPrivate() throws Exception {
1661
		IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
1662
1663
		StringBuffer buf= new StringBuffer();
1664
		buf.append("package test2;\n");
1665
		buf.append("public class C {\n");
1666
		buf.append("    protected void foo() {\n");
1667
		buf.append("    }\n");
1668
		buf.append("}\n");
1669
		pack2.createCompilationUnit("C.java", buf.toString(), false, null);
1670
1671
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
1672
		buf= new StringBuffer();
1673
		buf.append("package test1;\n");
1674
		buf.append("import test2.C;\n");
1675
		buf.append("public class E extends C {\n");
1676
		buf.append("    private void foo() {\n");
1677
		buf.append("    }\n");
1678
		buf.append("}\n");
1679
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
1680
1681
		CompilationUnit astRoot= getASTRoot(cu);
1682
		ArrayList proposals= collectCorrections(cu, astRoot);
1683
		assertNumberOfProposals(proposals, 1);
1684
		assertCorrectLabels(proposals);
1685
1686
		CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0);
1687
		String preview= getPreviewContent(proposal);
1688
1689
		buf= new StringBuffer();
1690
		buf.append("package test1;\n");
1691
		buf.append("import test2.C;\n");
1692
		buf.append("public class E extends C {\n");
1693
		buf.append("    protected void foo() {\n");
1694
		buf.append("    }\n");
1695
		buf.append("}\n");
1696
		assertEqualString(preview, buf.toString());
1697
	}
1658
1698
1659
	public void testOverridesStaticMethod() throws Exception {
1699
	public void testOverridesStaticMethod() throws Exception {
1660
		IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
1700
		IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/ModifierCorrectionSubProcessor.java (-26 / +46 lines)
Lines 9-14 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] 'Remove invalid modifiers' does not appear for enums and annotations - https://bugs.eclipse.org/bugs/show_bug.cgi?id=110589
10
 *     Benjamin Muskalla <bmuskalla@innoopract.com> - [quick fix] 'Remove invalid modifiers' does not appear for enums and annotations - https://bugs.eclipse.org/bugs/show_bug.cgi?id=110589
11
 *     Benjamin Muskalla <b.muskalla@gmx.net> - [quick fix] Quick fix for missing synchronized modifier - https://bugs.eclipse.org/bugs/show_bug.cgi?id=245250
11
 *     Benjamin Muskalla <b.muskalla@gmx.net> - [quick fix] Quick fix for missing synchronized modifier - https://bugs.eclipse.org/bugs/show_bug.cgi?id=245250
12
 *     Rabea Gransberger <rgransberger@gmx.de> - [quick fix] QuickFix proposes wrong visibility for overriding/overridden method - https://bugs.eclipse.org/bugs/show_bug.cgi?id=216898
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.jdt.internal.ui.text.correction;
14
package org.eclipse.jdt.internal.ui.text.correction;
14
15
Lines 257-291 Link Here
257
			overriddenInClass= Bindings.findOverriddenMethodInType(curr, method);
258
			overriddenInClass= Bindings.findOverriddenMethodInType(curr, method);
258
		}
259
		}
259
		if (overriddenInClass != null) {
260
		if (overriddenInClass != null) {
260
			IMethodBinding overriddenDecl= overriddenInClass.getMethodDeclaration();
261
			final IMethodBinding overriddenDecl= overriddenInClass.getMethodDeclaration();
261
			ICompilationUnit targetCU= ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), overriddenDecl.getDeclaringClass());
262
			final ICompilationUnit overriddenMethodCU= ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), overriddenDecl.getDeclaringClass());
262
			if (targetCU != null) {
263
263
				String methodLabel= BasicElementLabels.getJavaElementName(curr.getName() + '.' + overriddenInClass.getName());
264
			IMethodBinding targetMethod= overriddenDecl;
264
				String label;
265
			ICompilationUnit targetCU= overriddenMethodCU;
265
				int excludedModifiers;
266
266
				int includedModifiers;
267
			String methodLabel= BasicElementLabels.getJavaElementName(overriddenDecl.getDeclaringClass().getName() + '.' + overriddenDecl.getName());
267
				switch (kind) {
268
			String label;
268
					case TO_VISIBLE:
269
			int excludedModifiers;
270
			int includedModifiers;
271
			switch (kind) {
272
				case TO_VISIBLE:
273
					if (JdtFlags.isPrivate(method)) {
274
						// Propose to increase the visibility of this method, because decreasing to private is not possible.
275
						targetMethod= method;
276
						targetCU= cu;
277
278
						methodLabel= BasicElementLabels.getJavaElementName(method.getDeclaringClass().getName() + '.' + method.getName());
279
280
						excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
281
						includedModifiers= JdtFlags.getVisibilityCode(overriddenDecl);
282
					}
283
					else {
269
						excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
284
						excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
270
						includedModifiers= JdtFlags.getVisibilityCode(method);
285
						includedModifiers= JdtFlags.getVisibilityCode(method);
271
						label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changeoverriddenvisibility_description, new String[] { methodLabel, getVisibilityString(includedModifiers) });
286
					}
272
						break;
287
					label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changeoverriddenvisibility_description,
273
					case TO_NON_FINAL:
288
							new String[] { methodLabel, getVisibilityString(includedModifiers) });
274
						label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononfinal_description, methodLabel);
289
					break;
275
						excludedModifiers= Modifier.FINAL;
290
				case TO_NON_FINAL:
276
						includedModifiers= 0;
291
					label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononfinal_description, methodLabel);
277
						break;
292
					excludedModifiers= Modifier.FINAL;
278
					case TO_NON_STATIC:
293
					includedModifiers= 0;
279
						label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodLabel);
294
					break;
280
						excludedModifiers= Modifier.STATIC;
295
				case TO_NON_STATIC:
281
						includedModifiers= 0;
296
					label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodLabel);
282
						break;
297
					excludedModifiers= Modifier.STATIC;
283
					default:
298
					includedModifiers= 0;
284
						Assert.isTrue(false, "not supported"); //$NON-NLS-1$
299
					break;
285
						return;
300
				default:
286
				}
301
					Assert.isTrue(false, "not supported"); //$NON-NLS-1$
302
					return;
303
			}
304
305
			if (targetCU != null) {
287
				Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
306
				Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
288
				proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, overriddenDecl, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_2, image));
307
				proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, targetMethod, selectedNode, includedModifiers, excludedModifiers,
308
						IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_2, image));
289
			}
309
			}
290
		}
310
		}
291
	}
311
	}

Return to bug 216898