summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikolay Metchev2013-08-20 02:58:09 (EDT)
committer Noopur Gupta2013-08-20 02:58:09 (EDT)
commita7c8179b26c590dc8e6c57f367219e70f1518cbe (patch)
treeac500c114da06a555a85b3597a32bbd4811662d0
parentdd6840893f31388648711918dc88408ad36f7191 (diff)
downloadeclipse.jdt.ui-a7c8179b26c590dc8e6c57f367219e70f1518cbe.zip
eclipse.jdt.ui-a7c8179b26c590dc8e6c57f367219e70f1518cbe.tar.gz
eclipse.jdt.ui-a7c8179b26c590dc8e6c57f367219e70f1518cbe.tar.bz2
Fixed bug 409594: [organize imports] Import static (Ctrl+Shift+M)
creates imports for private methods Signed-off-by: Nikolay Metchev <nikolaymetchev@gmail.com>
-rw-r--r--org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/AddImportTest.java115
-rw-r--r--org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddImportsOperation.java13
2 files changed, 122 insertions, 6 deletions
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/AddImportTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/AddImportTest.java
index f754efc..edfae88 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/AddImportTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/AddImportTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2011 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
+ * Nikolay Metchev <nikolaymetchev@gmail.com> - Import static (Ctrl+Shift+M) creates imports for private methods - https://bugs.eclipse.org/409594
*******************************************************************************/
package org.eclipse.jdt.ui.tests.core;
@@ -981,6 +982,118 @@ public class AddImportTest extends CoreTests {
buf.append("}\n");
assertEqualString(cu.getSource(), buf.toString());
}
+
+ public void testAddImportActionBug_409594_test1() throws Exception {
+ IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
+
+ IPackageFragment pack1= sourceFolder.createPackageFragment("p", false, null);
+ String input =
+ "package p;\n" +
+ "\n" +
+ "class A {\n" +
+ " static void foo() {\n" +
+ " A.bar();\n" +
+ " }\n" +
+ "\n" +
+ " private static void bar() {\n" +
+ " }\n" +
+ "}";
+ ICompilationUnit cu= pack1.createCompilationUnit("A.java", input, false, null);
+
+ int selOffset= input.indexOf("bar");
+
+ AddImportsOperation op= new AddImportsOperation(cu, selOffset, 3, null, true);
+ op.run(null);
+
+ String expected =
+ "package p;\n" +
+ "\n" +
+ "class A {\n" +
+ " static void foo() {\n" +
+ " bar();\n" +
+ " }\n" +
+ "\n" +
+ " private static void bar() {\n" +
+ " }\n" +
+ "}";
+ assertEqualString(cu.getSource(), expected);
+ }
+
+ public void testAddImportActionBug_409594_test2() throws Exception {
+ IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
+
+ IPackageFragment pack1= sourceFolder.createPackageFragment("p", false, null);
+ String input =
+ "package p;\n" +
+ "\n" +
+ "class A {\n" +
+ " static void foo() {\n" +
+ " A.bar();\n" +
+ " }\n" +
+ "\n" +
+ " public static void bar() {\n" +
+ " }\n" +
+ "}";
+ ICompilationUnit cu= pack1.createCompilationUnit("A.java", input, false, null);
+
+ int selOffset= input.indexOf("bar");
+
+ AddImportsOperation op= new AddImportsOperation(cu, selOffset, 3, null, true);
+ op.run(null);
+
+ String expected =
+ "package p;\n" +
+ "\n" +
+ "import static p.A.bar;\n" +
+ "\n" +
+ "class A {\n" +
+ " static void foo() {\n" +
+ " bar();\n" +
+ " }\n" +
+ "\n" +
+ " public static void bar() {\n" +
+ " }\n" +
+ "}";
+ assertEqualString(cu.getSource(), expected);
+ }
+
+ public void testAddImportActionBug_409594_test3() throws Exception {
+ IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
+
+ IPackageFragment pack1= sourceFolder.createPackageFragment("p", false, null);
+ String input =
+ "package p;\n" +
+ "class SnippetX {\n" +
+ " private static class Test {\n" +
+ " class X {\n" +
+ " void foo() {\n" +
+ " Test.bar();\n" +
+ " }\n" +
+ " }\n" +
+ " public static void bar() {}\n" +
+ " }\n" +
+ "}";
+ ICompilationUnit cu= pack1.createCompilationUnit("A.java", input, false, null);
+
+ int selOffset= input.indexOf("bar");
+
+ AddImportsOperation op= new AddImportsOperation(cu, selOffset, 3, null, true);
+ op.run(null);
+
+ String expected =
+ "package p;\n" +
+ "class SnippetX {\n" +
+ " private static class Test {\n" +
+ " class X {\n" +
+ " void foo() {\n" +
+ " bar();\n" +
+ " }\n" +
+ " }\n" +
+ " public static void bar() {}\n" +
+ " }\n" +
+ "}";
+ assertEqualString(cu.getSource(), expected);
+ }
public void testAddImports_bug107206() throws Exception {
IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddImportsOperation.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddImportsOperation.java
index 53e2b09..6af8549 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddImportsOperation.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/AddImportsOperation.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
+ * Nikolay Metchev <nikolaymetchev@gmail.com> - Import static (Ctrl+Shift+M) creates imports for private methods - https://bugs.eclipse.org/409594
*******************************************************************************/
package org.eclipse.jdt.internal.corext.codemanipulation;
@@ -279,10 +280,12 @@ public class AddImportsOperation implements IWorkspaceRunnable {
if (containerName.length() > 0) {
if (containerName.equals(declaringClass.getName()) || containerName.equals(declaringClass.getQualifiedName()) ) {
- String res= importRewrite.addStaticImport(declaringClass.getQualifiedName(), binding.getName(), isField);
- if (!res.equals(simpleName)) {
- // adding import failed
- return null;
+ if (!Modifier.isPrivate(binding.getModifiers()) && !Modifier.isPrivate(declaringClass.getModifiers())) {
+ String res= importRewrite.addStaticImport(declaringClass.getQualifiedName(), binding.getName(), isField);
+ if (!res.equals(simpleName)) {
+ // adding import failed
+ return null;
+ }
}
return new ReplaceEdit(qualifierStart, simpleNameStart - qualifierStart, ""); //$NON-NLS-1$
}