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

(-)a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractMethodWorkSpace/ExtractMethodTests/nested_in/A_test655.java (+12 lines)
Added Link Here
1
package nested_in;
2
3
public class A_test655 {
4
		class Inner {
5
			void foo() {
6
			}
7
			public void test() {
8
				/*[*/foo();/*]*/
9
			}
10
		}
11
	}
12
}
(-)a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractMethodTests.java (+10 lines)
Lines 11-20 Link Here
11
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Missing return value, while extracting code out of a loop - https://bugs.eclipse.org/bugs/show_bug.cgi?id=213519
11
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Missing return value, while extracting code out of a loop - https://bugs.eclipse.org/bugs/show_bug.cgi?id=213519
12
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] missing return type when code can throw exception - https://bugs.eclipse.org/bugs/show_bug.cgi?id=97413
12
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] missing return type when code can throw exception - https://bugs.eclipse.org/bugs/show_bug.cgi?id=97413
13
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
13
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
14
 *     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
14
 *     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
15
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] extracting return value results in compile error - https://bugs.eclipse.org/bugs/show_bug.cgi?id=264606
15
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] extracting return value results in compile error - https://bugs.eclipse.org/bugs/show_bug.cgi?id=264606
16
 *     Nikolay Metchev   <nikolaymetchev@gmail.com>    - [extract method] Compilation error on extracting to outer type
16
 *******************************************************************************/
17
 *******************************************************************************/
17
package org.eclipse.jdt.ui.tests.refactoring;
18
package org.eclipse.jdt.ui.tests.refactoring;
18
19
19
import java.lang.reflect.Modifier;
20
import java.lang.reflect.Modifier;
20
import java.util.ArrayList;
21
import java.util.ArrayList;
Lines 161-170 Link Here
161
		performTest(fgTestSetup.getExpressionPackage(), "A", COMPARE_WITH_OUTPUT, "expression_out");
162
		performTest(fgTestSetup.getExpressionPackage(), "A", COMPARE_WITH_OUTPUT, "expression_out");
162
	}
163
	}
163
164
164
	protected void nestedTest() throws Exception {
165
	protected void nestedTest() throws Exception {
165
		performTest(fgTestSetup.getNestedPackage(), "A", COMPARE_WITH_OUTPUT, "nested_out");
166
		performTest(fgTestSetup.getNestedPackage(), "A", COMPARE_WITH_OUTPUT, "nested_out");
167
	}
168
169
	protected void nestedTest(int mode, int destination) throws Exception {
170
		performTest(fgTestSetup.getNestedPackage(), "A", mode, "nested_out", null, null, destination);
166
	}
171
	}
167
172
168
	protected void returnTest() throws Exception {
173
	protected void returnTest() throws Exception {
169
		performTest(fgTestSetup.getReturnPackage(), "A", COMPARE_WITH_OUTPUT, "return_out");
174
		performTest(fgTestSetup.getReturnPackage(), "A", COMPARE_WITH_OUTPUT, "return_out");
170
	}
175
	}
Lines 1497-1506 Link Here
1497
1502
1498
	public void test654() throws Exception {
1503
	public void test654() throws Exception {
1499
		nestedTest();
1504
		nestedTest();
1500
	}
1505
	}
1501
1506
1507
	// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=413967
1508
	public void test655() throws Exception {
1509
		nestedTest(INVALID_SELECTION, 1);
1510
	}
1511
	
1502
	//---- Extracting method containing a return statement.
1512
	//---- Extracting method containing a return statement.
1503
1513
1504
	public void test700() throws Exception {
1514
	public void test700() throws Exception {
1505
		returnTest();
1515
		returnTest();
1506
	}
1516
	}
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java (+3 lines)
Lines 6-15 Link Here
6
 * http://www.eclipse.org/legal/epl-v10.html
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
10
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
11
 *     Nikolay Metchev   <nikolaymetchev@gmail.com>    - [extract method] Compilation error on extracting to outer type
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.jdt.internal.corext.refactoring;
13
package org.eclipse.jdt.internal.corext.refactoring;
13
14
14
import org.eclipse.osgi.util.NLS;
15
import org.eclipse.osgi.util.NLS;
15
16
Lines 539-548 Link Here
539
540
540
	public static String ExtractMethodAnalyzer_cannot_extract_from_annotation;
541
	public static String ExtractMethodAnalyzer_cannot_extract_from_annotation;
541
542
542
	public static String ExtractMethodAnalyzer_cannot_extract_method_name_reference;
543
	public static String ExtractMethodAnalyzer_cannot_extract_method_name_reference;
543
544
545
	public static String ExtractMethodAnalyzer_cannot_extract_method_accesibility;
546
544
	public static String ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name;
547
	public static String ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name;
545
548
546
	public static String ExtractMethodAnalyzer_cannot_extract_name_in_declaration;
549
	public static String ExtractMethodAnalyzer_cannot_extract_name_in_declaration;
547
550
548
	public static String ExtractMethodAnalyzer_cannot_extract_null_type;
551
	public static String ExtractMethodAnalyzer_cannot_extract_null_type;
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodAnalyzer.java (+76 lines)
Lines 7-16 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
10
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
11
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607
11
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607
12
 *     Nikolay Metchev   <nikolaymetchev@gmail.com>    - [extract method] Compilation error on extracting to outer type
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.jdt.internal.corext.refactoring.code;
14
package org.eclipse.jdt.internal.corext.refactoring.code;
14
15
15
import java.util.ArrayList;
16
import java.util.ArrayList;
16
import java.util.HashSet;
17
import java.util.HashSet;
Lines 50-59 Link Here
50
import org.eclipse.jdt.core.dom.IVariableBinding;
51
import org.eclipse.jdt.core.dom.IVariableBinding;
51
import org.eclipse.jdt.core.dom.Initializer;
52
import org.eclipse.jdt.core.dom.Initializer;
52
import org.eclipse.jdt.core.dom.LabeledStatement;
53
import org.eclipse.jdt.core.dom.LabeledStatement;
53
import org.eclipse.jdt.core.dom.Message;
54
import org.eclipse.jdt.core.dom.Message;
54
import org.eclipse.jdt.core.dom.MethodDeclaration;
55
import org.eclipse.jdt.core.dom.MethodDeclaration;
56
import org.eclipse.jdt.core.dom.MethodInvocation;
57
import org.eclipse.jdt.core.dom.Modifier;
55
import org.eclipse.jdt.core.dom.Name;
58
import org.eclipse.jdt.core.dom.Name;
56
import org.eclipse.jdt.core.dom.PrimitiveType;
59
import org.eclipse.jdt.core.dom.PrimitiveType;
57
import org.eclipse.jdt.core.dom.QualifiedName;
60
import org.eclipse.jdt.core.dom.QualifiedName;
58
import org.eclipse.jdt.core.dom.SimpleName;
61
import org.eclipse.jdt.core.dom.SimpleName;
59
import org.eclipse.jdt.core.dom.Statement;
62
import org.eclipse.jdt.core.dom.Statement;
Lines 62-71 Link Here
62
import org.eclipse.jdt.core.dom.SwitchCase;
65
import org.eclipse.jdt.core.dom.SwitchCase;
63
import org.eclipse.jdt.core.dom.SwitchStatement;
66
import org.eclipse.jdt.core.dom.SwitchStatement;
64
import org.eclipse.jdt.core.dom.ThisExpression;
67
import org.eclipse.jdt.core.dom.ThisExpression;
65
import org.eclipse.jdt.core.dom.TryStatement;
68
import org.eclipse.jdt.core.dom.TryStatement;
66
import org.eclipse.jdt.core.dom.Type;
69
import org.eclipse.jdt.core.dom.Type;
70
import org.eclipse.jdt.core.dom.TypeDeclaration;
67
import org.eclipse.jdt.core.dom.VariableDeclaration;
71
import org.eclipse.jdt.core.dom.VariableDeclaration;
68
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
72
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
69
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
73
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
70
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
74
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
71
import org.eclipse.jdt.core.dom.WhileStatement;
75
import org.eclipse.jdt.core.dom.WhileStatement;
Lines 92-101 Link Here
92
96
93
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
97
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
94
import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
98
import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
95
99
96
/* package */ class ExtractMethodAnalyzer extends CodeAnalyzer {
100
/* package */ class ExtractMethodAnalyzer extends CodeAnalyzer {
101
102
	private static final class AccesibilityVisitor extends ASTVisitor {
103
		private static final class ClassFinder extends ASTVisitor {
104
			private final ITypeBinding fDeclaringClass;
105
106
			private boolean fFound= false;
107
108
			private ClassFinder(ITypeBinding declaringClass) {
109
				fDeclaringClass= declaringClass;
110
			}
111
112
			@Override
113
			public boolean visit(ClassInstanceCreation node) {
114
				if (node.resolveTypeBinding().isSubTypeCompatible(fDeclaringClass)) {
115
					fFound = true;
116
					return false;
117
				}
118
				return true;
119
			}
120
		}
121
122
		private ASTNode fDestination;
123
		private RefactoringStatus fStatus= new RefactoringStatus();
124
125
		private ASTNode[] fSelectedNodes;
126
127
		public AccesibilityVisitor(ASTNode destination, ASTNode[] selectedNodes) {
128
			fDestination= destination;
129
			fSelectedNodes= selectedNodes;
130
		}
131
132
		@Override
133
		public boolean visit(MethodInvocation node) {
134
			IMethodBinding resolvedMethodBinding= node.resolveMethodBinding();
135
			if (!Modifier.isStatic(resolvedMethodBinding.getModifiers())) {
136
				Expression expression= node.getExpression();
137
				if (expression != null) {
138
					return true;
139
				}
140
				final ITypeBinding declaringClass= resolvedMethodBinding.getDeclaringClass();
141
				ClassFinder classFinder= new ClassFinder(declaringClass);
142
				for (ASTNode selectedNode : fSelectedNodes) {
143
					selectedNode.accept(classFinder);
144
					if (classFinder.fFound) {
145
						return true;
146
					}
147
				}
148
				if (fDestination.getNodeType() == ASTNode.TYPE_DECLARATION) {
149
					TypeDeclaration destination= (TypeDeclaration) fDestination;
150
					ITypeBinding binding= destination.resolveBinding();
151
					if (binding != null && !declaringClass.isSubTypeCompatible(binding)) {
152
						fStatus.addFatalError(
153
								Messages.format(
154
										RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_accesibility, new Object[] { resolvedMethodBinding.getName(), binding.getName() }));
155
					}
156
				}
157
			}
158
			return true;
159
		}
160
161
		public RefactoringStatus getStatus() {
162
			return fStatus;
163
		}
164
	}
97
165
98
	public static final int ERROR=					-2;
166
	public static final int ERROR=					-2;
99
	public static final int UNDEFINED=				-1;
167
	public static final int UNDEFINED=				-1;
100
	public static final int NO=						0;
168
	public static final int NO=						0;
101
	public static final int EXPRESSION=				1;
169
	public static final int EXPRESSION=				1;
Lines 319-328 Link Here
319
		ITypeBinding type= ASTNodes.getEnclosingType(destination);
387
		ITypeBinding type= ASTNodes.getEnclosingType(destination);
320
		status.merge(Checks.checkMethodInType(type, methodName, arguments));
388
		status.merge(Checks.checkMethodInType(type, methodName, arguments));
321
		status.merge(Checks.checkMethodInHierarchy(type.getSuperclass(), methodName, null, arguments));
389
		status.merge(Checks.checkMethodInHierarchy(type.getSuperclass(), methodName, null, arguments));
322
	}
390
	}
323
391
392
	public void checkAccesibility(RefactoringStatus result, ASTNode[] selectedNodes, ASTNode destination) {
393
		AccesibilityVisitor visitor= new AccesibilityVisitor(destination, selectedNodes);
394
		for (ASTNode node : selectedNodes) {
395
			node.accept(visitor);
396
		}
397
		result.merge(visitor.getStatus());
398
	}
399
324
	private ITypeBinding[] getArgumentTypes() {
400
	private ITypeBinding[] getArgumentTypes() {
325
		ITypeBinding[] result= new ITypeBinding[fArguments.length];
401
		ITypeBinding[] result= new ITypeBinding[fArguments.length];
326
		for (int i= 0; i < fArguments.length; i++) {
402
		for (int i= 0; i < fArguments.length; i++) {
327
			result[i]= fArguments[i].getType();
403
			result[i]= fArguments[i].getType();
328
		}
404
		}
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractMethodRefactoring.java (+3 lines)
Lines 8-17 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     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
10
 *     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
11
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
11
 *     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
12
 *     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
12
 *     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
13
 *     Nikolay Metchev   <nikolaymetchev@gmail.com>    - [extract method] Compilation error on extracting to outer type
13
 *******************************************************************************/
14
 *******************************************************************************/
14
package org.eclipse.jdt.internal.corext.refactoring.code;
15
package org.eclipse.jdt.internal.corext.refactoring.code;
15
16
16
import java.util.ArrayList;
17
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.Arrays;
Lines 471-480 Link Here
471
			throw new OperationCanceledException();
472
			throw new OperationCanceledException();
472
473
473
		BodyDeclaration node= fAnalyzer.getEnclosingBodyDeclaration();
474
		BodyDeclaration node= fAnalyzer.getEnclosingBodyDeclaration();
474
		if (node != null) {
475
		if (node != null) {
475
			fAnalyzer.checkInput(result, fMethodName, fDestination);
476
			fAnalyzer.checkInput(result, fMethodName, fDestination);
477
			ASTNode[] selectedNodes= fAnalyzer.getSelectedNodes();
478
			fAnalyzer.checkAccesibility(result, selectedNodes, fDestination);
476
			pm.worked(1);
479
			pm.worked(1);
477
		}
480
		}
478
		pm.done();
481
		pm.done();
479
		return result;
482
		return result;
480
	}
483
	}
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties (+2 lines)
Lines 7-16 Link Here
7
#
7
#
8
# Contributors:
8
# Contributors:
9
#     IBM Corporation - initial API and implementation
9
#     IBM Corporation - initial API and implementation
10
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
10
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056
11
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607
11
#     Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Name ambiguous return value in error message - https://bugs.eclipse.org/bugs/show_bug.cgi?id=50607
12
#     Nikolay Metchev   <nikolaymetchev@gmail.com>    - [extract method] Compilation error on extracting to outer type
12
###############################################################################
13
###############################################################################
13
# NLS properties for the Refactoring Core
14
# NLS properties for the Refactoring Core
14
15
15
#######################################
16
#######################################
16
# org.eclipse.jdt.internal.core.refactoring
17
# org.eclipse.jdt.internal.core.refactoring
Lines 118-127 Link Here
118
ExtractMethodAnalyzer_cannot_extract_from_annotation=Cannot extract out of an annotation.
119
ExtractMethodAnalyzer_cannot_extract_from_annotation=Cannot extract out of an annotation.
119
ExtractMethodAnalyzer_cannot_extract_variable_declaration=Cannot extract parts of a variable declaration. Select whole declaration.
120
ExtractMethodAnalyzer_cannot_extract_variable_declaration=Cannot extract parts of a variable declaration. Select whole declaration.
120
ExtractMethodAnalyzer_cannot_extract_type_reference=Cannot extract a single type reference.
121
ExtractMethodAnalyzer_cannot_extract_type_reference=Cannot extract a single type reference.
121
ExtractMethodAnalyzer_cannot_extract_switch_case=Cannot extract the expression of a case statement.
122
ExtractMethodAnalyzer_cannot_extract_switch_case=Cannot extract the expression of a case statement.
122
ExtractMethodAnalyzer_cannot_extract_method_name_reference=Cannot extract a single method name.
123
ExtractMethodAnalyzer_cannot_extract_method_name_reference=Cannot extract a single method name.
124
ExtractMethodAnalyzer_cannot_extract_method_accesibility=Cannot extract because the method ''{0}'' is not visible from ''{1}''.
123
ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name=Can not extract part of a qualified name
125
ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name=Can not extract part of a qualified name
124
ExtractMethodAnalyzer_cannot_extract_name_in_declaration=Cannot extract the name part of a declaration.
126
ExtractMethodAnalyzer_cannot_extract_name_in_declaration=Cannot extract the name part of a declaration.
125
ExtractMethodAnalyzer_compile_errors_no_parent_binding=The selection cannot cannot be analyzed because of compilation errors in the enclosing type. To perform the operation you will need to fix the errors.
127
ExtractMethodAnalyzer_compile_errors_no_parent_binding=The selection cannot cannot be analyzed because of compilation errors in the enclosing type. To perform the operation you will need to fix the errors.
126
ExtractMethodAnalyzer_ambiguous_return_value=Ambiguous return value: Expression, access to local or return statement extracted.
128
ExtractMethodAnalyzer_ambiguous_return_value=Ambiguous return value: Expression, access to local or return statement extracted.
127
ExtractMethodAnalyzer_compile_errors=The method''s body cannot be analyzed because of compilation errors in method ''{0}''. To perform the operation you will need to fix the errors.
129
ExtractMethodAnalyzer_compile_errors=The method''s body cannot be analyzed because of compilation errors in method ''{0}''. To perform the operation you will need to fix the errors.

Return to bug 413967