Bug 570057 - Code var cleanup causes compilation error with method refs
- modify VarCleanUp to not perform cleanup if we are initializing a
variable with a method reference
- update CleanUpTest10 with new test
Change-Id: Ibf0d08e57b30ce08d71a35a4577587f3a919319f
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java
index 7733260..daf213b 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest10.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2020 Fabrice TIERCELIN and others.
+ * Copyright (c) 2020, 2021 Fabrice TIERCELIN and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -654,4 +654,25 @@
assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
}
+
+ @Test
+ public void testDoNotUseVarOnFromLambdaMethodReference() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ String sample= "" //
+ + "package test1;\n" //
+ + "\n" //
+ + "import java.util.function.Function;\n" //
+ + "\n" //
+ + "public class E1 {\n" //
+ + " Function<String, Integer> field = String::length;\n" //
+ + " public void foo() {\n" //
+ + " Function<String, Integer> doNotUseVarOnFromLambdaMethodReference = String::length;\n" //
+ + " }\n" //
+ + "}\n";
+ ICompilationUnit cu1= pack1.createCompilationUnit("E1.java", sample, false, null);
+
+ enable(CleanUpConstants.USE_VAR);
+
+ assertRefactoringHasNoChange(new ICompilationUnit[] { cu1 });
+ }
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java
index 1438d13..666abe0 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/VarCleanUp.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2020 Fabrice TIERCELIN and others.
+ * Copyright (c) 2020, 2021 Fabrice TIERCELIN and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -35,6 +35,7 @@
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
+import org.eclipse.jdt.core.dom.MethodReference;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
@@ -167,6 +168,7 @@
SuperMethodInvocation superMethodInvocation= ASTNodes.as(initializer, SuperMethodInvocation.class);
LambdaExpression lambdaExpression= ASTNodes.as(initializer, LambdaExpression.class);
Expression expression= ASTNodes.as(initializer, Expression.class);
+ MethodReference methodReference= ASTNodes.as(initializer, MethodReference.class);
if (!variableType.isParameterizedType() && lambdaExpression == null
|| (classInstanceCreation != null
@@ -194,6 +196,7 @@
&& methodInvocation == null
&& superMethodInvocation == null
&& lambdaExpression == null
+ && methodReference == null
&& expression != null
&& expression.resolveTypeBinding() != null
&& expression.resolveTypeBinding().isParameterizedType()
@@ -201,6 +204,7 @@
rewriteOperations.add(new VarOperation(type));
return false;
} else if (variableType.isParameterizedType()
+ && methodReference == null
&& classInstanceCreation != null
&& classInstanceCreation.getType().isParameterizedType()
&& ((ParameterizedType) classInstanceCreation.getType()).typeArguments().isEmpty()) {