diff options
author | Manju Mathew | 2013-09-05 07:15:20 +0000 |
---|---|---|
committer | Manju Mathew | 2013-09-05 07:15:20 +0000 |
commit | 9a2003ce0627698e5dada41f627aa4df3de31051 (patch) | |
tree | 5d2daec6095d8d1b2354237bd7ce83693c938223 | |
parent | 2707c1b948defd52b6690b00006a52f93785d707 (diff) | |
download | eclipse.jdt.ui-9a2003ce0627698e5dada41f627aa4df3de31051.tar.gz eclipse.jdt.ui-9a2003ce0627698e5dada41f627aa4df3de31051.tar.xz eclipse.jdt.ui-9a2003ce0627698e5dada41f627aa4df3de31051.zip |
Fix for bug 408937: [1.8][rename] Unable to rename variables in lambda
expression field
13 files changed, 263 insertions, 10 deletions
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda0/in/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda0/in/A.java new file mode 100644 index 0000000000..5abbe088b1 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda0/in/A.java @@ -0,0 +1,12 @@ +package p; + @FunctionalInterface + interface I { + int foo (int x); + } + + public class A { + I i= (int /*[*/f/*]*/) -> { + int p= 10; + return ii.foo(f) + p; + }; + } diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda0/out/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda0/out/A.java new file mode 100644 index 0000000000..e9b62351ad --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda0/out/A.java @@ -0,0 +1,12 @@ +package p; + @FunctionalInterface + interface I { + int foo (int x); + } + + public class A { + I i= (int /*[*/renamedF/*]*/) -> { + int p= 10; + return ii.foo(renamedF) + p; + }; + } diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda1/in/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda1/in/A.java new file mode 100644 index 0000000000..1f0245f131 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda1/in/A.java @@ -0,0 +1,12 @@ +package p; + @FunctionalInterface + interface I { + int foo (int x); + } + + public class A { + I i= (int f) -> { + int /*[*/p/*]*/= 10; + return ii.foo(f) + p; + }; + } diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda1/out/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda1/out/A.java new file mode 100644 index 0000000000..b2dc1b1630 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda1/out/A.java @@ -0,0 +1,12 @@ +package p; + @FunctionalInterface + interface I { + int foo (int x); + } + + public class A { + I i= (int f) -> { + int /*[*/renamedP/*]*/= 10; + return ii.foo(f) + renamedP; + }; + } diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda2/in/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda2/in/A.java new file mode 100644 index 0000000000..93056d1719 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda2/in/A.java @@ -0,0 +1,13 @@ +package p; +@FunctionalInterface +interface I { + int foo (int x); +} + +public class C1 { + I i= (int x) -> { + int p= 10; + I /*[*/ii/*]*/= (int a) -> a+100; + return ii.foo(x) + p; + }; +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda2/out/A.java b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda2/out/A.java new file mode 100644 index 0000000000..b0b207f177 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/resources/RenameTests18/testLambda2/out/A.java @@ -0,0 +1,13 @@ +package p; +@FunctionalInterface +interface I { + int foo (int x); +} + +public class C1 { + I i= (int x) -> { + int p= 10; + I /*[*/renamedIi/*]*/= (int a) -> a+100; + return renamedIi.foo(x) + p; + }; +}
\ No newline at end of file diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTests.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTests.java index 044b1c7f46..aca6e5931b 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTests.java +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTests.java @@ -1,10 +1,14 @@ /******************************************************************************* - * 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 * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -35,6 +39,7 @@ public class RenameTests { suite.addTest(RenameTypeParameterTests.suite()); suite.addTest(RenameNonPrivateFieldTests.suite()); suite.addTest(RenameJavaProjectTests.suite()); + suite.addTest(RenameTests18.suite()); return suite; } diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTests18.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTests18.java new file mode 100644 index 0000000000..bb7e7183b0 --- /dev/null +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/RenameTests18.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (c) 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.ui.tests.refactoring; + +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.core.runtime.NullProgressMonitor; + +import org.eclipse.ltk.core.refactoring.RefactoringCore; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.core.refactoring.participants.RenameArguments; +import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.ISourceRange; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.SourceRange; +import org.eclipse.jdt.core.refactoring.IJavaRefactorings; +import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor; + +import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory; + +public class RenameTests18 extends RefactoringTest { + + private static final Class clazz= RenameTests18.class; + + private static final String REFACTORING_PATH= "RenameTests18/"; + + + public RenameTests18(String name) { + super(name); + } + + public static Test suite() { + return new RefactoringTestSetup( new TestSuite(clazz)); + } + + public static Test setUpTest(Test someTest) { + return new RefactoringTestSetup(someTest); + } + + protected String getRefactoringPath() { + return REFACTORING_PATH; + } + + protected void setUp() throws Exception { + super.setUp(); + Hashtable options= JavaCore.getOptions(); + JavaCore.setOptions(options); + fIsPreDeltaTest= true; + } + + protected void tearDown() throws Exception { + super.tearDown(); + Hashtable options= JavaCore.getOptions(); + JavaCore.setOptions(options); + } + + private ISourceRange getSelection(ICompilationUnit cu) throws Exception { + String source= cu.getSource(); + //Warning: this *includes* the SQUARE_BRACKET_OPEN! + int offset= source.indexOf(AbstractSelectionTestCase.SQUARE_BRACKET_OPEN); + int end= source.indexOf(AbstractSelectionTestCase.SQUARE_BRACKET_CLOSE); + return new SourceRange(offset + AbstractSelectionTestCase.SQUARE_BRACKET_OPEN.length(), end - offset); + } + + private void helper2(String newFieldName, boolean updateReferences) throws Exception { + ParticipantTesting.reset(); + ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A"); + + ISourceRange selection= getSelection(cu); + IJavaElement[] elements= cu.codeSelect(selection.getOffset(), selection.getLength()); + assertEquals(1, elements.length); + RenameJavaElementDescriptor descriptor= RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_LOCAL_VARIABLE); + descriptor.setJavaElement(elements[0]); + descriptor.setNewName(newFieldName); + descriptor.setUpdateReferences(updateReferences); + descriptor.setUpdateTextualOccurrences(false); + + RenameRefactoring refactoring= (RenameRefactoring) createRefactoring(descriptor); + List list= new ArrayList(); + list.add(elements[0]); + List args= new ArrayList(); + args.add(new RenameArguments(newFieldName, updateReferences)); + String[] renameHandles= ParticipantTesting.createHandles(list.toArray()); + + RefactoringStatus result= performRefactoring(refactoring); + assertEquals("was supposed to pass", null, result); + assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource()); + + ParticipantTesting.testRename( + renameHandles, + (RenameArguments[]) args.toArray(new RenameArguments[args.size()])); + + assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo()); + assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo()); + + RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor()); + assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource()); + + assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo()); + assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo()); + + RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor()); + assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource()); + } + + + + //--------- tests ---------- + public void testLambda0() throws Exception { + helper2("renamedF", true); + } + + public void testLambda1() throws Exception { + helper2("renamedP", true); + } + + public void testLambda2() throws Exception { + helper2("renamedIi", true); + } +} diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java index 2c6aef5c46..779411576f 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java @@ -1,10 +1,14 @@ /******************************************************************************* - * 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 * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation * Benjamin Muskalla <bmuskalla@eclipsesource.com> - [extract method] Extract method and continue https://bugs.eclipse.org/bugs/show_bug.cgi?id=48056 @@ -1807,6 +1811,8 @@ public final class RefactoringCoreMessages extends NLS { public static String RenameTempRefactoring_only_in_methods_and_initializers; + public static String RenameTempRefactoring_only_in_methods_initializers_and_lambda; + public static String RenameTempRefactoring_rename; public static String RenameTypeParameterProcessor_change_name; diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties index 044df0ddbe..bae9afaedf 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties @@ -537,6 +537,7 @@ ReplaceInvocationsRefactoring_cannot_replace_in_binary=Cannot replace invocation RenameTempRefactoring_must_select_local=A local variable declaration or reference must be selected to activate this refactoring RenameTempRefactoring_only_in_methods_and_initializers=Only local variables declared in methods and initializers can be renamed +RenameTempRefactoring_only_in_methods_initializers_and_lambda=Only local variables declared in methods, initializers and lambda expression can be renamed RenameTempRefactoring_lowercase=This name is discouraged. According to convention, names of local variables should start with a lowercase letter. RenameTempRefactoring_lowercase2=The variable name ''{0}'' in ''{1}'' (type ''{2}'') is discouraged. According to convention, names of local variables should start with a lowercase letter. RenameTempRefactoring_rename=Rename Local Variable diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RefactoringAnalyzeUtil.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RefactoringAnalyzeUtil.java index 525e0d308c..a99d43bad7 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RefactoringAnalyzeUtil.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RefactoringAnalyzeUtil.java @@ -1,10 +1,14 @@ /******************************************************************************* - * 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 * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -27,6 +31,7 @@ import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.LambdaExpression; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.NodeFinder; import org.eclipse.jdt.core.dom.SimpleName; @@ -63,6 +68,10 @@ public class RefactoringAnalyzeUtil { return result; } + public static LambdaExpression getLambdaExpression(TextEdit edit, TextChange change, CompilationUnit cuNode) { + ASTNode decl= RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode); + return ((LambdaExpression) ASTNodes.getParent(decl, LambdaExpression.class)); + } public static MethodDeclaration getMethodDeclaration(TextEdit edit, TextChange change, CompilationUnit cuNode){ ASTNode decl= RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode); diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameAnalyzeUtil.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameAnalyzeUtil.java index 376fd243bc..906af468d2 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameAnalyzeUtil.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameAnalyzeUtil.java @@ -1,10 +1,14 @@ /******************************************************************************* - * 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 * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -424,7 +428,7 @@ class RenameAnalyzeUtil { return result; for (int i= 0; i < analyzePackages.length; i++) { - ASTNode enclosing= getEnclosingBlockOrMethod(analyzePackages[i].fDeclarationEdit, cuChange, newCUNode); + ASTNode enclosing= getEnclosingBlockOrMethodOrLambda(analyzePackages[i].fDeclarationEdit, cuChange, newCUNode); // get new declaration IRegion newRegion= RefactoringAnalyzeUtil.getNewTextRange(analyzePackages[i].fDeclarationEdit, cuChange); @@ -452,10 +456,12 @@ class RenameAnalyzeUtil { return null; } - private static ASTNode getEnclosingBlockOrMethod(TextEdit declarationEdit, TextChange change, CompilationUnit newCUNode) { + private static ASTNode getEnclosingBlockOrMethodOrLambda(TextEdit declarationEdit, TextChange change, CompilationUnit newCUNode) { ASTNode enclosing= RefactoringAnalyzeUtil.getBlock(declarationEdit, change, newCUNode); if (enclosing == null) enclosing= RefactoringAnalyzeUtil.getMethodDeclaration(declarationEdit, change, newCUNode); + if (enclosing == null) + enclosing= RefactoringAnalyzeUtil.getLambdaExpression(declarationEdit, change, newCUNode); return enclosing; } diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameLocalVariableProcessor.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameLocalVariableProcessor.java index 723445f767..011bf3f96f 100644 --- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameLocalVariableProcessor.java +++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/rename/RenameLocalVariableProcessor.java @@ -1,10 +1,14 @@ /******************************************************************************* - * 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 * http://www.eclipse.org/legal/epl-v10.html * + * This is an implementation of an early-draft specification developed under the Java + * Community Process (JCP) and is made available for testing and evaluation purposes + * only. The code is not compatible with any specification of the JCP. + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -41,11 +45,12 @@ import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Initializer; +import org.eclipse.jdt.core.dom.LambdaExpression; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.NodeFinder; import org.eclipse.jdt.core.dom.VariableDeclaration; -import org.eclipse.jdt.core.refactoring.IJavaRefactorings; import org.eclipse.jdt.core.refactoring.CompilationUnitChange; +import org.eclipse.jdt.core.refactoring.IJavaRefactorings; import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor; import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory; @@ -62,6 +67,7 @@ import org.eclipse.jdt.internal.corext.refactoring.tagging.IReferenceUpdating; import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil; import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager; +import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.jdt.ui.JavaElementLabels; @@ -218,9 +224,15 @@ public class RenameLocalVariableProcessor extends JavaRenameProcessor implements initAST(); if (fTempDeclarationNode == null || fTempDeclarationNode.resolveBinding() == null) return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_must_select_local); - if (! Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class) - && ! Checks.isDeclaredIn(fTempDeclarationNode, Initializer.class)) + + if (!Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class) + && !Checks.isDeclaredIn(fTempDeclarationNode, Initializer.class) + && !Checks.isDeclaredIn(fTempDeclarationNode, LambdaExpression.class)) { + if (JavaModelUtil.is18OrHigher(fCu.getJavaProject())) + return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_initializers_and_lambda); + return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_and_initializers); + } initNames(); return new RefactoringStatus(); |