summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRabea Gransberger2013-03-12 08:06:58 (EDT)
committerDani Megert2013-03-12 08:14:35 (EDT)
commit0e8156a0ee59f04b2e265d8bdbdea1497f47fee3 (patch)
tree81aac8bdef5fd5b784a61529985e29c032f0eac0
parent08653fe28e027c770dd914127101b9aa9c1688f2 (diff)
downloadeclipse.jdt.ui-0e8156a0ee59f04b2e265d8bdbdea1497f47fee3.zip
eclipse.jdt.ui-0e8156a0ee59f04b2e265d8bdbdea1497f47fee3.tar.gz
eclipse.jdt.ui-0e8156a0ee59f04b2e265d8bdbdea1497f47fee3.tar.bz2
Fixed bug 394692: [quick fix] Fix several visibility issuesI20130312-2000I20130312-1000
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ModifierCorrectionsQuickFixTest.java182
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedMethodsQuickFixTest.java49
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedTypesQuickFixTest.java62
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedVariablesQuickFixTest.java48
-rw-r--r--org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java39
-rw-r--r--org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/JdtFlags.java18
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/ModifierCorrectionSubProcessor.java80
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java8
8 files changed, 458 insertions, 28 deletions
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ModifierCorrectionsQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ModifierCorrectionsQuickFixTest.java
index bc8c1f6..3d7c3f6 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ModifierCorrectionsQuickFixTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/ModifierCorrectionsQuickFixTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
* IBM Corporation - initial API and implementation
* 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
* Benjamin Muskalla <b.muskalla@gmx.net> - [quick fix] Quick fix for missing synchronized modifier - https://bugs.eclipse.org/bugs/show_bug.cgi?id=245250
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.ui.tests.quickfix;
@@ -3922,4 +3923,183 @@ public class ModifierCorrectionsQuickFixTest extends QuickFixTest {
assertExpectedExistInProposals(proposals, expected);
}
+
+ /**
+ * Quick Fix proposes wrong visibility for overriding/overridden method.
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=216898
+ *
+ * @throws Exception if anything goes wrong
+ * @since 3.9
+ */
+ public void testOverridingMethodIsPrivate() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+
+ StringBuffer buf= new StringBuffer();
+
+ buf.append("package test1;\n");
+ buf.append("public abstract class A1 {\n");
+ buf.append(" protected abstract void m1();\n");
+ buf.append("}\n");
+ pack1.createCompilationUnit("A1.java", buf.toString(), false, null);
+
+ buf= new StringBuffer();
+
+ buf.append("package test1;\n");
+ buf.append("public class B1 extends A1 {\n");
+ buf.append(" private void m1() {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("B1.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList proposals= collectCorrections(cu, astRoot, 1);
+
+ assertCorrectLabels(proposals);
+ assertNumberOfProposals(proposals, 1);
+
+ String[] expected= new String[1];
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class B1 extends A1 {\n");
+ buf.append(" protected void m1() {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ expected[0]= buf.toString();
+
+ assertExpectedExistInProposals(proposals, expected);
+ }
+
+
+ /**
+ * Quick Fix proposes wrong visibility for overriding/overridden method.
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=216898#c1
+ *
+ * @throws Exception if anything goes wrong
+ * @since 3.9
+ */
+ public void testInvalidVisabilityOverrideMethod() throws Exception {
+ // No simple solution to this problemID IProblem.AbstractMethodCannotBeOverridden
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
+
+ StringBuffer buf= new StringBuffer();
+
+ buf.append("package test1;\n");
+ buf.append("public abstract class Abs {\n");
+ buf.append(" abstract String getName();\n");
+ buf.append("}\n");
+ pack1.createCompilationUnit("Abs.java", buf.toString(), false, null);
+
+ buf= new StringBuffer();
+
+ buf.append("package test2;\n");
+ buf.append("public class AbsImpl extends test1.Abs {\n");
+ buf.append(" String getName() {\n");
+ buf.append(" return \"name\";\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack2.createCompilationUnit("AbsImpl.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList proposals= collectCorrections(cu, astRoot, 2);
+
+ assertCorrectLabels(proposals);
+ assertNumberOfProposals(proposals, 0);
+ }
+
+ /**
+ * Quick Fix proposes wrong visibility for overriding/overridden method.
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=216898#c1
+ *
+ * @throws Exception if anything goes wrong
+ * @since 3.9
+ */
+ public void test216898Comment1Variation() throws Exception {
+ // Changing Abs.getName to protected by hand to allow solution for AbsImpl
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
+
+ StringBuffer buf= new StringBuffer();
+
+ buf.append("package test1;\n");
+ buf.append("public abstract class Abs {\n");
+ buf.append(" protected abstract String getName();\n");
+ buf.append("}\n");
+ pack1.createCompilationUnit("Abs.java", buf.toString(), false, null);
+
+ buf= new StringBuffer();
+
+ buf.append("package test2;\n");
+ buf.append("public class AbsImpl extends test1.Abs {\n");
+ buf.append(" String getName() {\n");
+ buf.append(" return \"name\";\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack2.createCompilationUnit("AbsImpl.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList proposals= collectCorrections(cu, astRoot, 1);
+
+ assertCorrectLabels(proposals);
+ assertNumberOfProposals(proposals, 1);
+
+ String[] expected= new String[1];
+ buf= new StringBuffer();
+ buf.append("package test2;\n");
+ buf.append("public class AbsImpl extends test1.Abs {\n");
+ buf.append(" protected String getName() {\n");
+ buf.append(" return \"name\";\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ expected[0]= buf.toString();
+
+ assertExpectedExistInProposals(proposals, expected);
+ }
+
+ /**
+ * Wrong visibility for overriding method in interface.
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=87239
+ *
+ * @throws Exception if anything goes wrong
+ * @since 3.9
+ */
+ public void testImplementExtendSameMethod() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("interface I {\n");
+ buf.append(" void xxx();\n");
+ buf.append("}\n");
+ buf.append("class A {\n");
+ buf.append(" void xxx() {}\n");
+ buf.append("}\n");
+ buf.append("class B extends A implements I {\n");
+ buf.append(" void xxx() {}//error\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("I.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList proposals= collectCorrections(cu, astRoot, 1);
+
+ assertCorrectLabels(proposals);
+ assertNumberOfProposals(proposals, 1);
+
+ String[] expected= new String[1];
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("interface I {\n");
+ buf.append(" void xxx();\n");
+ buf.append("}\n");
+ buf.append("class A {\n");
+ buf.append(" void xxx() {}\n");
+ buf.append("}\n");
+ buf.append("class B extends A implements I {\n");
+ buf.append(" public void xxx() {}//error\n");
+ buf.append("}\n");
+ expected[0]= buf.toString();
+
+ assertExpectedExistInProposals(proposals, expected);
+ }
+
}
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedMethodsQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedMethodsQuickFixTest.java
index d4804a5..67d6a77 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedMethodsQuickFixTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedMethodsQuickFixTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.ui.tests.quickfix;
@@ -4942,5 +4943,51 @@ public class UnresolvedMethodsQuickFixTest extends QuickFixTest {
}
+ /**
+ * Visibility: fix for public or protected not appropriate.
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=65876#c5
+ *
+ * @throws Exception if anything goes wrong
+ * @since 3.9
+ */
+ public void testIndirectProtectedMethod() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
+
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class A {\n");
+ buf.append(" protected void method() {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ pack1.createCompilationUnit("A.java", buf.toString(), false, null);
+
+ buf= new StringBuffer();
+ buf.append("package test2;\n");
+ buf.append("import test1.A;\n");
+ buf.append("public class B extends A {\n");
+ buf.append(" private void bMethod() {\n");
+ buf.append(" A a = new A();\n");
+ buf.append(" a.method();\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu = pack2.createCompilationUnit("B.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList proposals= collectCorrections(cu, astRoot);
+ assertCorrectLabels(proposals);
+ assertNumberOfProposals(proposals, 1);
+
+ String[] expected= new String[1];
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class A {\n");
+ buf.append(" public void method() {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ expected[0]= buf.toString();
+
+ assertExpectedExistInProposals(proposals, expected);
+ }
}
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedTypesQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedTypesQuickFixTest.java
index eebade9..f3c66c6 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedTypesQuickFixTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedTypesQuickFixTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.ui.tests.quickfix;
@@ -1491,5 +1492,64 @@ public class UnresolvedTypesQuickFixTest extends QuickFixTest {
assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] { expected1, expected2 });
}
+
+ /**
+ * Offers to raise visibility of method instead of class.
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=94755
+ *
+ * @throws Exception if anything goes wrong
+ * @since 3.9
+ */
+ public void testIndirectRefDefaultClass() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
+
+ StringBuffer buf= new StringBuffer();
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("class B {\n");
+ buf.append(" public Object get(Object c) {\n");
+ buf.append(" return null;\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("B.java", buf.toString(), false, null);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class A {\n");
+ buf.append(" B b = new B();\n");
+ buf.append(" public B getB() {\n");
+ buf.append(" return b;\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ cu= pack1.createCompilationUnit("A.java", buf.toString(), false, null);
+
+ buf= new StringBuffer();
+ buf.append("package test2;\n");
+ buf.append("import test1.A;\n");
+ buf.append("public class C {\n");
+ buf.append(" public Object getSide(A a) {\n");
+ buf.append(" return a.getB().get(this);\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ cu= pack2.createCompilationUnit("C.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList proposals= collectCorrections(cu, astRoot);
+ assertNumberOfProposals(proposals, 1);
+ assertCorrectLabels(proposals);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class B {\n");
+ buf.append(" public Object get(Object c) {\n");
+ buf.append(" return null;\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ String expected1= buf.toString();
+
+ assertExpectedExistInProposals(proposals, new String[] { expected1 });
+ }
}
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedVariablesQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedVariablesQuickFixTest.java
index 72bdb7a..62e0498 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedVariablesQuickFixTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/UnresolvedVariablesQuickFixTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.ui.tests.quickfix;
@@ -2713,6 +2714,51 @@ public class UnresolvedVariablesQuickFixTest extends QuickFixTest {
assertExpectedExistInProposals(proposals, expected);
}
+
+
+ /**
+ * Wrong quick fixes when accessing protected field in subclass in different package.
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=280819#c2
+ *
+ * @throws Exception if anything goes wrong
+ * @since 3.9
+ */
+ public void testVarParameterAccess() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ IPackageFragment pack2= fSourceFolder.createPackageFragment("test2", false, null);
+
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class Base {\n");
+ buf.append(" protected int myField;\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("Base.java", buf.toString(), false, null);
+
+ buf= new StringBuffer();
+ buf.append("package test2;\n");
+ buf.append("import test1.Base;\n");
+ buf.append("public class Child extends Base {\n");
+ buf.append(" public void aMethod(Base parent) {\n");
+ buf.append(" System.out.println(parent.myField);\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ cu = pack2.createCompilationUnit("Child.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= getASTRoot(cu);
+ ArrayList proposals= collectCorrections(cu, astRoot);
+ assertCorrectLabels(proposals);
+ assertNumberOfProposals(proposals, 2);
+
+ String [] expected = new String[1];
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class Base {\n");
+ buf.append(" public int myField;\n");
+ buf.append("}\n");
+ expected[0]= buf.toString();
+
+ assertExpectedExistInProposals(proposals, expected);
+ }
}
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java
index bd2bcc0..8eb60e4 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/dom/Bindings.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -10,6 +10,7 @@
* Dmitry Stalnov (dstalnov@fusionone.com) - contributed fix for
* bug "inline method - doesn't handle implicit cast" (see
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=24941).
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.internal.corext.dom;
@@ -493,9 +494,31 @@ public class Bindings {
* @return the method binding representing the method
*/
public static IMethodBinding findOverriddenMethod(IMethodBinding overriding, boolean testVisibility) {
+ List<IMethodBinding> findOverriddenMethods= findOverriddenMethods(overriding, testVisibility, true);
+ if (findOverriddenMethods.isEmpty()) {
+ return null;
+ }
+ return findOverriddenMethods.get(0);
+ }
+
+ /**
+ * Finds all methods that are overridden by the given method. The search is bottom-up, so this
+ * returns the nearest defining/declaring methods in order.
+ *
+ * @param overriding overriding method
+ * @param testVisibility if <code>true</code> the result is tested on visibility
+ * @param firstOnly if <code>true</code> this method will return when the first overridden
+ * method is found
+ * @return the method bindings representing the overridden method or an empty list if no result
+ * is found
+ * @since 3.9
+ */
+ public static List<IMethodBinding> findOverriddenMethods(IMethodBinding overriding, boolean testVisibility, boolean firstOnly) {
+ List<IMethodBinding> methodList= new ArrayList<IMethodBinding>();
+
int modifiers= overriding.getModifiers();
if (Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers) || overriding.isConstructor()) {
- return null;
+ return methodList;
}
ITypeBinding type= overriding.getDeclaringClass();
@@ -503,7 +526,10 @@ public class Bindings {
IMethodBinding res= findOverriddenMethodInHierarchy(type.getSuperclass(), overriding);
if (res != null && !Modifier.isPrivate(res.getModifiers())) {
if (!testVisibility || isVisibleInHierarchy(res, overriding.getDeclaringClass().getPackage())) {
- return res;
+ methodList.add(res);
+ if (firstOnly) {
+ return methodList;
+ }
}
}
}
@@ -511,10 +537,13 @@ public class Bindings {
for (int i= 0; i < interfaces.length; i++) {
IMethodBinding res= findOverriddenMethodInHierarchy(interfaces[i], overriding);
if (res != null) {
- return res; // methods from interfaces are always public and therefore visible
+ methodList.add(res); // methods from interfaces are always public and therefore visible
+ if (firstOnly) {
+ return methodList;
+ }
}
}
- return null;
+ return methodList;
}
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/JdtFlags.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/JdtFlags.java
index 6bc7ee5..9a75357 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/JdtFlags.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/util/JdtFlags.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.internal.corext.util;
@@ -342,6 +343,21 @@ public class JdtFlags {
return visibility1;
}
+ /**
+ * Gets the higher visibility of the given parameters.
+ *
+ * @param visibility1 First visibility value to compare. Valid inputs are the {@link Modifier} visibility constants.
+ * @param visibility2 Second visibility value to compare. Valid inputs are the {@link Modifier} visibility constants.
+ * @return The parameter value with the higher visibility.
+ * @since 3.9
+ */
+ public static int getHigherVisibility(int visibility1, int visibility2) {
+ if (isHigherVisibility(visibility1, visibility2))
+ return visibility1;
+ else
+ return visibility2;
+ }
+
public static int clearAccessModifiers(int flags) {
return clearFlag(Modifier.PROTECTED | Modifier.PUBLIC | Modifier.PRIVATE, flags);
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/ModifierCorrectionSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/ModifierCorrectionSubProcessor.java
index ca25949..a2a7eb1 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/ModifierCorrectionSubProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/ModifierCorrectionSubProcessor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
* IBM Corporation - initial API and implementation
* 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
* Benjamin Muskalla <b.muskalla@gmx.net> - [quick fix] Quick fix for missing synchronized modifier - https://bugs.eclipse.org/bugs/show_bug.cgi?id=245250
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.correction;
@@ -154,6 +155,9 @@ public class ModifierCorrectionSubProcessor {
if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding= ((IVariableBinding) binding).getType();
}
+ if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
+ binding= ((IMethodBinding) binding).getReturnType();
+ }
if (binding instanceof IMethodBinding) {
IMethodBinding methodDecl= (IMethodBinding) binding;
if (methodDecl.isDefaultConstructor()) {
@@ -183,7 +187,7 @@ public class ModifierCorrectionSubProcessor {
switch (kind) {
case TO_VISIBLE:
excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
- includedModifiers= getNeededVisibility(selectedNode, typeBinding);
+ includedModifiers= getNeededVisibility(selectedNode, typeBinding, binding);
label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
break;
case TO_STATIC:
@@ -241,10 +245,15 @@ public class ModifierCorrectionSubProcessor {
if (kind == TO_VISIBLE && problem.getProblemId() != IProblem.OverridingNonVisibleMethod) {
- IMethodBinding defining= Bindings.findOverriddenMethod(method, false);
- if (defining != null) {
+ // e.g. IProblem.InheritedMethodReducesVisibility, IProblem.MethodReducesVisibility
+ List<IMethodBinding> methods= Bindings.findOverriddenMethods(method, false, false);
+ if (!methods.isEmpty()) {
+ int includedModifiers= 0;
+ for (IMethodBinding binding : methods) {
+ int temp= JdtFlags.getVisibilityCode(binding);
+ includedModifiers= JdtFlags.getHigherVisibility(temp, includedModifiers);
+ }
int excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
- int includedModifiers= JdtFlags.getVisibilityCode(defining);
String label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodvisibility_description, new String[] { getVisibilityString(includedModifiers) });
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposals.add(new ModifierChangeCorrectionProposal(label, cu, method, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_1, image));
@@ -257,26 +266,58 @@ public class ModifierCorrectionSubProcessor {
overriddenInClass= Bindings.findOverriddenMethodInType(curr, method);
}
if (overriddenInClass != null) {
- IMethodBinding overriddenDecl= overriddenInClass.getMethodDeclaration();
- ICompilationUnit targetCU= ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), overriddenDecl.getDeclaringClass());
- if (targetCU != null) {
- String methodLabel= BasicElementLabels.getJavaElementName(curr.getName() + '.' + overriddenInClass.getName());
+ final IMethodBinding overriddenDecl= overriddenInClass.getMethodDeclaration();
+ final ICompilationUnit overriddenMethodCU= ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), overriddenDecl.getDeclaringClass());
+
+ if (overriddenMethodCU != null) {
+ //target method and compilation unit for the quick fix
+ IMethodBinding targetMethod= overriddenDecl;
+ ICompilationUnit targetCU= overriddenMethodCU;
+
String label;
int excludedModifiers;
int includedModifiers;
switch (kind) {
case TO_VISIBLE:
- excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
- includedModifiers= JdtFlags.getVisibilityCode(method);
- label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changeoverriddenvisibility_description, new String[] { methodLabel, getVisibilityString(includedModifiers) });
+ if (JdtFlags.isPrivate(method)) {
+ // Propose to increase the visibility of this method, because decreasing to private is not possible.
+ targetMethod= method;
+ targetCU= cu;
+
+ excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
+ includedModifiers= JdtFlags.getVisibilityCode(overriddenDecl);
+ }
+ else if (JdtFlags.isPackageVisible(method) && !overriddenDecl.getDeclaringClass().getPackage().isEqualTo(method.getDeclaringClass().getPackage())) {
+ // method is package visible but not in the same package as overridden method
+ // propose to make the method protected
+
+ excludedModifiers= Modifier.PRIVATE;
+ includedModifiers= Modifier.PROTECTED;
+
+ // if it is already protected, ignore it
+ if (JdtFlags.isProtected(overriddenDecl)) {
+ return;
+ }
+ }
+ else {
+ excludedModifiers= Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
+ includedModifiers= JdtFlags.getVisibilityCode(method);
+
+ if (JdtFlags.getVisibilityCode(overriddenDecl) == JdtFlags.getVisibilityCode(method)) {
+ // don't propose the same visibility it already has
+ return;
+ }
+ }
+
+ label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changeoverriddenvisibility_description, new String[] { getMethodLabel(targetMethod), getVisibilityString(includedModifiers) });
break;
case TO_NON_FINAL:
- label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononfinal_description, methodLabel);
+ label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononfinal_description, getMethodLabel(targetMethod));
excludedModifiers= Modifier.FINAL;
includedModifiers= 0;
break;
case TO_NON_STATIC:
- label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodLabel);
+ label= Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, getMethodLabel(targetMethod));
excludedModifiers= Modifier.STATIC;
includedModifiers= 0;
break;
@@ -285,7 +326,7 @@ public class ModifierCorrectionSubProcessor {
return;
}
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
- proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, overriddenDecl, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_2, image));
+ proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, targetMethod, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_2, image));
}
}
}
@@ -430,6 +471,10 @@ public class ModifierCorrectionSubProcessor {
}
}
}
+
+ private static String getMethodLabel(IMethodBinding targetMethod) {
+ return BasicElementLabels.getJavaElementName(targetMethod.getDeclaringClass().getName() + '.' + targetMethod.getName());
+ }
private static String getVisibilityString(int code) {
if (Modifier.isPublic(code)) {
@@ -443,13 +488,16 @@ public class ModifierCorrectionSubProcessor {
}
- private static int getNeededVisibility(ASTNode currNode, ITypeBinding targetType) {
+ private static int getNeededVisibility(ASTNode currNode, ITypeBinding targetType, IBinding binding) {
ITypeBinding currNodeBinding= Bindings.getBindingOfParentType(currNode);
if (currNodeBinding == null) { // import
return Modifier.PUBLIC;
}
if (Bindings.isSuperType(targetType, currNodeBinding)) {
+ if (binding != null && (JdtFlags.isProtected(binding) || binding.getKind() == IBinding.TYPE)) {
+ return Modifier.PUBLIC;
+ }
return Modifier.PROTECTED;
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java
index 44d31f7..a877736 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/UnresolvedElementsSubProcessor.java
@@ -8,6 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Renaud Waldura &lt;renaud+eclipse@waldura.com&gt; - New class/interface with wizard
+ * Rabea Gransberger <rgransberger@gmx.de> - [quick fix] Fix several visibility issues - https://bugs.eclipse.org/394692
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.correction;
@@ -295,7 +296,7 @@ public class UnresolvedElementsSubProcessor {
boolean isWriteAccess= ASTResolving.isWriteAccess(node);
// similar variables
- addSimilarVariableProposals(cu, astRoot, binding, simpleName, isWriteAccess, proposals);
+ addSimilarVariableProposals(cu, astRoot, binding, resolvedField, simpleName, isWriteAccess, proposals);
if (binding == null) {
addStaticImportFavoriteProposals(context, simpleName, false, proposals);
@@ -415,7 +416,7 @@ public class UnresolvedElementsSubProcessor {
}
}
- private static void addSimilarVariableProposals(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding, SimpleName node, boolean isWriteAccess, Collection<ICommandAccess> proposals) {
+ private static void addSimilarVariableProposals(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding, IVariableBinding resolvedField, SimpleName node, boolean isWriteAccess, Collection<ICommandAccess> proposals) {
int kind= ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY;
if (!isWriteAccess) {
kind |= ScopeAnalyzer.METHODS; // also try to find similar methods
@@ -474,6 +475,9 @@ public class UnresolvedElementsSubProcessor {
if (currName.equals(otherNameInAssign)) {
continue loop;
}
+ if (resolvedField != null && Bindings.equals(resolvedField, curr)) {
+ continue loop;
+ }
boolean isFinal= Modifier.isFinal(curr.getModifiers());
if (isFinal && curr.isField() && isWriteAccess) {
continue loop;