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

(-)a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractTemp/canExtract/A_test110_in.java (+11 lines)
Added Link Here
1
package p; //6, 9, 6, 25
2
3
class A {
4
	void m() {
5
		System.out.println(calculateCount());
6
		calculateCount();
7
	}
8
	private int calculateCount() {
9
		return 1;
10
	}
11
}
(-)a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractTemp/canExtract/A_test110_out.java (+12 lines)
Added Link Here
1
package p; //6, 9, 6, 25
2
3
class A {
4
	void m() {
5
		int temp= calculateCount();
6
		System.out.println(temp);
7
		calculateCount();
8
	}
9
	private int calculateCount() {
10
		return 1;
11
	}
12
}
(-)a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractTemp/canExtract/A_test111_in.java (+16 lines)
Added Link Here
1
package p; //11, 9, 11, 25
2
3
class A {
4
	void m() {
5
		System.out.println(calculateCount());
6
		calculateCount();
7
		System.out.println(calculateCount());
8
		
9
		int x= calculateCount();
10
		
11
		calculateCount();
12
	}
13
	private int calculateCount() {
14
		return 1;
15
	}
16
}
(-)a/org.eclipse.jdt.ui.tests.refactoring/resources/ExtractTemp/canExtract/A_test111_out.java (+17 lines)
Added Link Here
1
package p; //11, 9, 11, 25
2
3
class A {
4
	void m() {
5
		int temp= calculateCount();
6
		System.out.println(temp);
7
		calculateCount();
8
		System.out.println(temp);
9
		
10
		int x= temp;
11
		
12
		calculateCount();
13
	}
14
	private int calculateCount() {
15
		return 1;
16
	}
17
}
(-)a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/ExtractTempTests.java (-1 / +12 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
2
 * Copyright (c) 2000, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Nikolay Metchev <nikolaymetchev@gmail.com> - [extract local] Extract to local variable not replacing multiple occurrences in same statement - https://bugs.eclipse.org/406347
10
 *     Nikolay Metchev <nikolaymetchev@gmail.com> - [extract local] Extract to local variable not replacing multiple occurrences in same statement - https://bugs.eclipse.org/406347
11
 *     Nicolaj Hoess <nicohoess@gmail.com> - [extract local] puts declaration at wrong position - https://bugs.eclipse.org/65875
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.jdt.ui.tests.refactoring;
13
package org.eclipse.jdt.ui.tests.refactoring;
13
14
Lines 681-686 Link Here
681
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
682
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=335173
682
		helper1(5, 20, 5, 29, true, false, "temp", "i");
683
		helper1(5, 20, 5, 29, true, false, "temp", "i");
683
	}
684
	}
685
	
686
	public void test110() throws Exception {
687
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=65875
688
		helper1(6, 9, 6, 25, true, false, "temp", "calculateCount");
689
	}
690
	
691
	public void test111() throws Exception {
692
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=65875
693
		helper1(11, 9, 11, 25, true, false, "temp", "calculateCount");
694
	}
684
695
685
	public void test113() throws Exception {
696
	public void test113() throws Exception {
686
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=406347
697
		//test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=406347
(-)a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractTempRefactoring.java (-2 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
2
 * Copyright (c) 2000, 2014 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 8-13 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Nikolay Metchev <nikolaymetchev@gmail.com> - [extract local] Extract to local variable not replacing multiple occurrences in same statement - https://bugs.eclipse.org/406347
10
 *     Nikolay Metchev <nikolaymetchev@gmail.com> - [extract local] Extract to local variable not replacing multiple occurrences in same statement - https://bugs.eclipse.org/406347
11
 *     Nicolaj Hoess <nicohoess@gmail.com> - [extract local] puts declaration at wrong position - https://bugs.eclipse.org/65875
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.jdt.internal.corext.refactoring.code;
13
package org.eclipse.jdt.internal.corext.refactoring.code;
13
14
Lines 666-672 Link Here
666
		Expression initializer= getSelectedExpression().createCopyTarget(fCURewrite.getASTRewrite(), true);
667
		Expression initializer= getSelectedExpression().createCopyTarget(fCURewrite.getASTRewrite(), true);
667
		VariableDeclarationStatement vds= createTempDeclaration(initializer);
668
		VariableDeclarationStatement vds= createTempDeclaration(initializer);
668
669
669
		if ((!fReplaceAllOccurrences) || (retainOnlyReplacableMatches(getMatchingFragments()).length <= 1)) {
670
		IASTFragment[] replacableMatches= retainOnlyReplacableMatches(getMatchingFragments());
671
		if ((!fReplaceAllOccurrences) || (replacableMatches.length == 0) || (replacableMatches.length == 1 && replacableMatches[0].equals(getSelectedExpression().getAssociatedExpression()))) {
670
			insertAt(getSelectedExpression().getAssociatedNode(), vds);
672
			insertAt(getSelectedExpression().getAssociatedNode(), vds);
671
			return;
673
			return;
672
		}
674
		}
Lines 999-1004 Link Here
999
1001
1000
	private boolean shouldReplaceSelectedExpressionWithTempDeclaration() throws JavaModelException {
1002
	private boolean shouldReplaceSelectedExpressionWithTempDeclaration() throws JavaModelException {
1001
		IExpressionFragment selectedFragment= getSelectedExpression();
1003
		IExpressionFragment selectedFragment= getSelectedExpression();
1004
		IExpressionFragment firstExpression= getFirstReplacedExpression();
1005
		if (firstExpression.getStartPosition() < selectedFragment.getStartPosition()) return false;
1002
		return selectedFragment.getAssociatedNode().getParent() instanceof ExpressionStatement
1006
		return selectedFragment.getAssociatedNode().getParent() instanceof ExpressionStatement
1003
			&& selectedFragment.matches(ASTFragmentFactory.createFragmentForFullSubtree(selectedFragment.getAssociatedNode()));
1007
			&& selectedFragment.matches(ASTFragmentFactory.createFragmentForFullSubtree(selectedFragment.getAssociatedNode()));
1004
	}
1008
	}

Return to bug 65875