diff options
author | Samrat Dhillon | 2013-10-14 05:24:57 +0000 |
---|---|---|
committer | Manju Mathew | 2013-10-14 05:24:57 +0000 |
commit | c34f9c7e211fcf5f1713cdd569cce4fed8a485e2 (patch) | |
tree | 261169d3d48c7826dfd7d545a8365c2432199224 | |
parent | 17cf94f18d9db074d03b412bc5c8d422f8dcfe31 (diff) | |
download | eclipse.jdt.ui-c34f9c7e211fcf5f1713cdd569cce4fed8a485e2.tar.gz eclipse.jdt.ui-c34f9c7e211fcf5f1713cdd569cce4fed8a485e2.tar.xz eclipse.jdt.ui-c34f9c7e211fcf5f1713cdd569cce4fed8a485e2.zip |
Fixed Bug 393098: [extract method] Extracted method should be declared
static if extracted expression is also used in another static method
Signed-off-by: Samrat Dhillon <samrat.dhillon@gmail.com>
8 files changed, 105 insertions, 3 deletions
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_in/A_test988.java b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_in/A_test988.java new file mode 100644 index 0000000000..093b35a0fe --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_in/A_test988.java @@ -0,0 +1,15 @@ +package duplicates_in; + +public class A_test988 { + + public static void sm() { + shared(); + } + + public void nsm() { + /*[*/shared();/*]*/ + } + + public static void shared() { + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_in/A_test989.java b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_in/A_test989.java new file mode 100644 index 0000000000..c721af4e63 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_in/A_test989.java @@ -0,0 +1,16 @@ +package duplicates_in; +import java.util.List; + +public class A_test989 { + + public A_test989(int i) { + } + public A_test989(List<String> l, int k) { + this(l.size()); + } + + public void nsm(List<String> l) { + System.out.println(/*[*/l.size()/*]*/); + } + +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_out/A_test988.java b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_out/A_test988.java new file mode 100644 index 0000000000..2f5e4de094 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_out/A_test988.java @@ -0,0 +1,19 @@ +package duplicates_out; + +public class A_test988 { + + public static void sm() { + extracted(); + } + + public void nsm() { + extracted(); + } + + protected static void extracted() { + /*[*/shared();/*]*/ + } + + public static void shared() { + } +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_out/A_test989.java b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_out/A_test989.java new file mode 100644 index 0000000000..035a6340e3 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/duplicates_out/A_test989.java @@ -0,0 +1,19 @@ +package duplicates_out; +import java.util.List; + +public class A_test989 { + + public A_test989(int i) { + } + public A_test989(List<String> l, int k) { + this(extracted(l)); + } + + public void nsm(List<String> l) { + System.out.println(extracted(l)); + } + protected static int extracted(List<String> l) { + return /*[*/l.size()/*]*/; + } + +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractMethodTests.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractMethodTests.java index 264124d3da..082518263f 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractMethodTests.java +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractMethodTests.java @@ -13,6 +13,7 @@ * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056 * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] should declare method static if extracted from anonymous in static method - https://bugs.eclipse.org/bugs/show_bug.cgi?id=152004 * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] extracting return value results in compile error - https://bugs.eclipse.org/bugs/show_bug.cgi?id=264606 + * Samrat Dhillon <samrat.dhillon@gmail.com> - [extract method] Extracted method should be declared static if extracted expression is also used in another static method https://bugs.eclipse.org/bugs/show_bug.cgi?id=393098 *******************************************************************************/ package org.eclipse.jdt.ui.tests.refactoring; @@ -1914,6 +1915,14 @@ public class ExtractMethodTests extends AbstractSelectionTestCase { public void test987() throws Exception { duplicatesTest(); // for https://bugs.eclipse.org/bugs/show_bug.cgi?id=264606 } + + public void test988() throws Exception{ + duplicatesTest(); + } + + public void test989() throws Exception{ + duplicatesTest(); + } //---- Test code in initializers ----------------------------------------------- diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodRefactoring.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodRefactoring.java index 5426301dbd..1ca75f609e 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodRefactoring.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodRefactoring.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 @@ * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Does not replace similar code in parent class of anonymous class - https://bugs.eclipse.org/bugs/show_bug.cgi?id=160853 * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056 * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] should declare method static if extracted from anonymous in static method - https://bugs.eclipse.org/bugs/show_bug.cgi?id=152004 + * Samrat Dhillon <samrat.dhillon@gmail.com> - [extract method] Extracted method should be declared static if extracted expression is also used in another static method https://bugs.eclipse.org/bugs/show_bug.cgi?id=393098 *******************************************************************************/ package org.eclipse.jdt.internal.corext.refactoring.code; @@ -946,6 +947,19 @@ public class ExtractMethodRefactoring extends Refactoring { } } } + + private boolean forceStatic(){ + if(!fReplaceDuplicates){ + return false; + } + for(int i= 0;i < fDuplicates.length; i++) { + SnippetFinder.Match duplicate= fDuplicates[i]; + if(!duplicate.isInvalidNode() && duplicate.isNodeInStaticContext()) { + return true; + } + } + return false; + } private boolean isDestinationReachable(MethodDeclaration methodDeclaration) { ASTNode start= methodDeclaration; @@ -982,7 +996,7 @@ public class ExtractMethodRefactoring extends Refactoring { int enclosingModifiers= ((BodyDeclaration)enclosingBodyDeclaration).getModifiers(); boolean shouldBeStatic= Modifier.isStatic(enclosingModifiers) || enclosingBodyDeclaration instanceof EnumDeclaration - || fAnalyzer.getForceStatic(); + || fAnalyzer.getForceStatic() || forceStatic(); if (shouldBeStatic) { modifiers|= Modifier.STATIC; } diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/SnippetFinder.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/SnippetFinder.java index eba20796d4..1ad2317431 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/SnippetFinder.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/SnippetFinder.java @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] extracting return value results in compile error - https://bugs.eclipse.org/bugs/show_bug.cgi?id=264606 + * Samrat Dhillon <samrat.dhillon@gmail.com> - [extract method] Extracted method should be declared static if extracted expression is also used in another static method https://bugs.eclipse.org/bugs/show_bug.cgi?id=393098 *******************************************************************************/ package org.eclipse.jdt.internal.corext.refactoring.code; @@ -40,6 +41,8 @@ import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.dom.Bindings; import org.eclipse.jdt.internal.corext.dom.GenericVisitor; +import org.eclipse.jdt.internal.ui.text.correction.ASTResolving; + /* package */ class SnippetFinder extends GenericVisitor { @@ -105,6 +108,11 @@ import org.eclipse.jdt.internal.corext.dom.GenericVisitor; ASTNode first= fNodes.get(0); return (MethodDeclaration)ASTNodes.getParent(first, ASTNode.METHOD_DECLARATION); } + + public boolean isNodeInStaticContext(){ + ASTNode first= fNodes.get(0); + return ASTResolving.isInStaticContext(first); + } } private class Matcher extends ASTMatcher { diff --git a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/code/ExtractMethodInputPage.java b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/code/ExtractMethodInputPage.java index b0617aac97..2999378132 100644 --- a/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/code/ExtractMethodInputPage.java +++ b/org.eclipse.jdt.ui/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/code/ExtractMethodInputPage.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 @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] remember selected access modifier - https://bugs.eclipse.org/bugs/show_bug.cgi?id=101233 + * Samrat Dhillon <samrat.dhillon@gmail.com> - [extract method] Extracted method should be declared static if extracted expression is also used in another static method https://bugs.eclipse.org/bugs/show_bug.cgi?id=393098 *******************************************************************************/ package org.eclipse.jdt.internal.ui.refactoring.code; @@ -221,6 +222,7 @@ public class ExtractMethodInputPage extends UserInputWizardPage { @Override public void widgetSelected(SelectionEvent e) { fRefactoring.setReplaceDuplicates(((Button)e.widget).getSelection()); + updatePreview(getText()); } }); layouter.perform(checkBox); |