Bug 93850 - [inline] Allow inlining of local variable initialized to
null.
Also checks for resulting compile errors, fixing Bug 434747 and Bug
434745.
Change-Id: I8fca67e3d12f309e9430b74f782631437f95f71a
Signed-off-by: Pierre-Yves B. <PyvesDev@gmail.com>
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java
index 132fe38..a750137 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/RefactoringCoreMessages.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,6 +14,7 @@
* Samrat Dhillon <samrat.dhillon@gmail.com> - [introduce factory] Introduce Factory on an abstract class adds a statement to create an instance of that class - https://bugs.eclipse.org/bugs/show_bug.cgi?id=395016
* Yves Joan <yves.joan@oracle.com> - [reorg] Copy action should NOT add 'copy of' prefix - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151668
* Red Hat Inc. - copied to jdt.core.manipulation
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring;
@@ -877,8 +878,6 @@
public static String InlineTemRefactoring_error_message_fieldsCannotBeInlined;
- public static String InlineTemRefactoring_error_message_nulLiteralsCannotBeInlined;
-
public static String IntroduceFactory_addFactoryMethod;
public static String IntroduceFactory_callSitesInBinaryClass;
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties
index ba63c63..90840d3 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties
+++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/refactoring.properties
@@ -1,5 +1,5 @@
###############################################################################
-# Copyright (c) 2000, 2018 IBM Corporation and others.
+# Copyright (c) 2000, 2019 IBM Corporation and others.
#
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
@@ -15,6 +15,7 @@
# Samrat Dhillon <samrat.dhillon@gmail.com> - [introduce factory] Introduce Factory on an abstract class adds a statement to create an instance of that class - https://bugs.eclipse.org/bugs/show_bug.cgi?id=395016
# Yves Joan <yves.joan@oracle.com> - [reorg] Copy action should NOT add 'copy of' prefix - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151668
# Red Hat Inc. - copied to jdt.core.manipulation
+# Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
###############################################################################
# NLS properties for the Refactoring Core
@@ -726,7 +727,6 @@
RenameAnalyzeUtil_reference_shadowed=Problem in ''{0}''. The reference to ''{1}'' will be shadowed by a renamed declaration
CopyRefactoring_update_ref=Update type reference
CodeRefactoringUtil_error_message=The body of the method ''{0}'' cannot be analyzed because of compilation errors in that method. To perform the operation you will need to fix the errors.
-InlineTemRefactoring_error_message_nulLiteralsCannotBeInlined=Null literals cannot be inlined
InlineTemRefactoring_error_message_fieldsCannotBeInlined=Cannot inline fields
HierarchyRefactoring_initializer=initializer
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/canInline/A_test50_in.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/canInline/A_test50_in.java
new file mode 100644
index 0000000..c24a280
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/canInline/A_test50_in.java
@@ -0,0 +1,7 @@
+package p;
+class A{
+ Integer m(){
+ Integer x= null;
+ return x;
+ }
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/canInline/A_test50_out.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/canInline/A_test50_out.java
new file mode 100644
index 0000000..8c9420d
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/canInline/A_test50_out.java
@@ -0,0 +1,6 @@
+package p;
+class A{
+ Integer m(){
+ return null;
+ }
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/cannotInline/A_testFail14.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/cannotInline/A_testFail14.java
new file mode 100644
index 0000000..1b182cf
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/cannotInline/A_testFail14.java
@@ -0,0 +1,8 @@
+//inlining would introduce compile error (method call on null)
+package p;
+class A{
+ String m(){
+ Integer x= null;
+ return x.toString();
+ }
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/cannotInline/A_testFail15.java b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/cannotInline/A_testFail15.java
new file mode 100644
index 0000000..ca3cd83
--- /dev/null
+++ b/org.eclipse.jdt.ui.tests.refactoring/resources/InlineTemp/cannotInline/A_testFail15.java
@@ -0,0 +1,8 @@
+//inlining would introduce compile error (overloaded method ambiguity)
+package p;
+class A{
+ void m(){
+ String x= null;
+ System.out.println(x);
+ }
+}
\ No newline at end of file
diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineTempTests.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineTempTests.java
index 887dc10..ff41517 100644
--- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineTempTests.java
+++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/InlineTempTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2015 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -11,12 +11,10 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Nikolay Metchev <nikolaymetchev@gmail.com> - [inline] Inline local variable with initializer generates assignment where left-hand side is not a variable - https://bugs.eclipse.org/394721
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.ui.tests.refactoring;
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.jdt.core.ICompilationUnit;
@@ -28,6 +26,9 @@
import org.eclipse.jdt.ui.tests.refactoring.infra.TextRangeUtil;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
public class InlineTempTests extends RefactoringTest {
private static final Class<InlineTempTests> clazz= InlineTempTests.class;
@@ -350,6 +351,11 @@
helper1(5, 15, 5, 16);
}
+ public void test50() throws Exception {
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=93850
+ helper1(4, 17, 4, 18);
+ }
+
//------
public void testFail0() throws Exception{
@@ -406,13 +412,26 @@
}
public void testFail12() throws Exception{
+ printTestDisabledMessage("compile errors are ok now");
//test for 19851
- helper2(10, 16, 10, 19);
+// helper2(10, 16, 10, 19);
}
+
public void testFail13() throws Exception{
// printTestDisabledMessage("12106");
helper2(4, 18, 4, 19);
}
+ public void testFail14() throws Exception {
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=93850
+ helper2(5, 17, 5, 18);
+ }
+
+ public void testFail15() throws Exception {
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=93850
+ //https://bugs.eclipse.org/bugs/show_bug.cgi?id=434747
+ helper2(5, 16, 5, 17);
+ }
+
}
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java
index e35122e..19a6752 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -15,6 +15,7 @@
* Lukas Hanke <hanke@yatta.de> - Bug 430818 [1.8][quick fix] Quick fix for "for loop" is not shown for bare local variable/argument/field - https://bugs.eclipse.org/bugs/show_bug.cgi?id=430818
* Mateusz Matela <mateusz.matela@gmail.com> - [formatter] Formatter does not format Java code correctly, especially when max line width is set
* Jens Reimann <jens.reimann@ibh-systems.com>, Fabian Pfaff <fabian.pfaff@vogella.com> - Bug 197850: [quick assist] Add import static field/method - https://bugs.eclipse.org/bugs/show_bug.cgi?id=197850
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.ui.tests.quickfix;
@@ -2813,7 +2814,7 @@
AssistContext context= getCorrectionContext(cu, buf.toString().indexOf(str), 0);
List<IJavaCompletionProposal> proposals= collectAssists(context, false);
- assertNumberOfProposals(proposals, 2);
+ assertNumberOfProposals(proposals, 3);
assertCorrectLabels(proposals);
buf= new StringBuffer();
@@ -2829,15 +2830,23 @@
buf= new StringBuffer();
buf.append("package test1;\n");
buf.append("public class E {\n");
+ buf.append(" public void foo() {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ String ex2= buf.toString();
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class E {\n");
buf.append(" private int is[];\n");
buf.append("\n");
buf.append(" public void foo() {\n");
buf.append(" is = null;\n");
buf.append(" }\n");
buf.append("}\n");
- String ex2= buf.toString();
+ String ex3= buf.toString();
- assertExpectedExistInProposals(proposals, new String[] { ex1, ex2 });
+ assertExpectedExistInProposals(proposals, new String[] { ex1, ex2, ex3 });
}
public void testSplitDeclaration4() throws Exception {
diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractConstantRefactoring.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractConstantRefactoring.java
index 1d5e357..ad95abb 100644
--- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractConstantRefactoring.java
+++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractConstantRefactoring.java
@@ -10,6 +10,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.code;
@@ -26,7 +27,6 @@
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.text.edits.TextEditGroup;
@@ -36,7 +36,6 @@
import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
-import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
@@ -44,7 +43,6 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.SourceRange;
-import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
@@ -85,11 +83,9 @@
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
import org.eclipse.jdt.internal.corext.Corext;
-import org.eclipse.jdt.internal.corext.SourceRangeFactory;
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.Bindings;
-import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer;
import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory;
import org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment;
@@ -101,7 +97,6 @@
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
-import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
import org.eclipse.jdt.internal.corext.refactoring.rename.RefactoringAnalyzeUtil;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
@@ -467,16 +462,11 @@
// lead to a change in behavior
try {
- RefactoringStatus result= new RefactoringStatus();
-
createConstantDeclaration();
replaceExpressionsWithConstant();
fChange= fCuRewrite.createChange(RefactoringCoreMessages.ExtractConstantRefactoring_change_name, true, new SubProgressMonitor(pm, 1));
- if (fCheckResultForCompileProblems) {
- checkSource(new SubProgressMonitor(pm, 1), result);
- }
- return result;
+ return fCheckResultForCompileProblems ? RefactoringAnalyzeUtil.checkNewSource(fChange, fCu, fCuRewrite.getRoot(), pm) : new RefactoringStatus();
} finally {
fConstantTypeCache= null;
fCuRewrite.clearASTAndImportRewrites();
@@ -484,18 +474,6 @@
}
}
- private void checkSource(SubProgressMonitor monitor, RefactoringStatus result) throws CoreException {
- String newCuSource= fChange.getPreviewContent(new NullProgressMonitor());
- CompilationUnit newCUNode= new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL).parse(newCuSource, fCu, true, true, monitor);
-
- IProblem[] newProblems= RefactoringAnalyzeUtil.getIntroducedCompileProblems(newCUNode, fCuRewrite.getRoot());
- for (int i= 0; i < newProblems.length; i++) {
- IProblem problem= newProblems[i];
- if (problem.isError())
- result.addEntry(new RefactoringStatusEntry((problem.isError() ? RefactoringStatus.ERROR : RefactoringStatus.WARNING), problem.getMessage(), new JavaStringStatusContext(newCuSource, SourceRangeFactory.create(problem))));
- }
- }
-
private void createConstantDeclaration() throws CoreException {
Type type= getConstantType();
diff --git a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractTempRefactoring.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractTempRefactoring.java
index dddad04..205ab76 100644
--- a/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractTempRefactoring.java
+++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/ExtractTempRefactoring.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -12,6 +12,7 @@
* IBM Corporation - initial API and implementation
* Nikolay Metchev <nikolaymetchev@gmail.com> - [extract local] Extract to local variable not replacing multiple occurrences in same statement - https://bugs.eclipse.org/406347
* Nicolaj Hoess <nicohoess@gmail.com> - [extract local] puts declaration at wrong position - https://bugs.eclipse.org/65875
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.code;
@@ -30,7 +31,6 @@
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.text.edits.TextEditGroup;
@@ -40,7 +40,6 @@
import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
-import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
@@ -48,7 +47,6 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.SourceRange;
-import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
@@ -112,11 +110,9 @@
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
import org.eclipse.jdt.internal.corext.Corext;
-import org.eclipse.jdt.internal.corext.SourceRangeFactory;
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.Bindings;
-import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer;
import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory;
import org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment;
@@ -128,7 +124,6 @@
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
-import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
import org.eclipse.jdt.internal.corext.refactoring.rename.RefactoringAnalyzeUtil;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
@@ -515,7 +510,7 @@
fChange.setKeepPreviewEdits(true);
if (fCheckResultForCompileProblems) {
- checkNewSource(new SubProgressMonitor(pm, 1), result);
+ result.merge(RefactoringAnalyzeUtil.checkNewSource(fChange, fCu, fCompilationUnitNode, pm));
}
return result;
@@ -546,7 +541,7 @@
comment.addSetting(RefactoringCoreMessages.ExtractTempRefactoring_replace_occurrences);
if (fDeclareFinal)
comment.addSetting(RefactoringCoreMessages.ExtractTempRefactoring_declare_final);
- if (fDeclareVarType)
+ if (fDeclareVarType)
comment.addSetting(RefactoringCoreMessages.ExtractTempRefactoring_declare_var_type);
final ExtractLocalDescriptor descriptor= RefactoringSignatureDescriptorFactory.createExtractLocalDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE);
arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
@@ -572,17 +567,6 @@
}
}
- private void checkNewSource(SubProgressMonitor monitor, RefactoringStatus result) throws CoreException {
- String newCuSource= fChange.getPreviewContent(new NullProgressMonitor());
- CompilationUnit newCUNode= new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL).parse(newCuSource, fCu, true, true, monitor);
- IProblem[] newProblems= RefactoringAnalyzeUtil.getIntroducedCompileProblems(newCUNode, fCompilationUnitNode);
- for (int i= 0; i < newProblems.length; i++) {
- IProblem problem= newProblems[i];
- if (problem.isError())
- result.addEntry(new RefactoringStatusEntry((problem.isError() ? RefactoringStatus.ERROR : RefactoringStatus.WARNING), problem.getMessage(), new JavaStringStatusContext(newCuSource, SourceRangeFactory.create(problem))));
- }
- }
-
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
try {
diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/InlineTempRefactoring.java b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineTempRefactoring.java
similarity index 87%
rename from org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/InlineTempRefactoring.java
rename to org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineTempRefactoring.java
index 64463f2..9534623 100644
--- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/refactoring/code/InlineTempRefactoring.java
+++ b/org.eclipse.jdt.ui/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/InlineTempRefactoring.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2018 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.code;
@@ -97,6 +98,7 @@
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
+import org.eclipse.jdt.internal.corext.refactoring.rename.RefactoringAnalyzeUtil;
import org.eclipse.jdt.internal.corext.refactoring.rename.TempDeclarationFinder;
import org.eclipse.jdt.internal.corext.refactoring.rename.TempOccurrenceAnalyzer;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
@@ -116,6 +118,8 @@
private VariableDeclaration fVariableDeclaration;
private SimpleName[] fReferences;
private CompilationUnit fASTRoot;
+ private boolean fCheckResultForCompileProblems;
+ private CompilationUnitChange fChange;
/**
* Creates a new inline constant refactoring.
@@ -133,6 +137,8 @@
fASTRoot= node;
fVariableDeclaration= null;
+ fCheckResultForCompileProblems= true;
+ fChange= null;
}
/**
@@ -155,6 +161,8 @@
fSelectionStart= decl.getStartPosition();
fSelectionLength= decl.getLength();
fCu= (ICompilationUnit) fASTRoot.getJavaElement();
+ fCheckResultForCompileProblems= true;
+ fChange= null;
}
public InlineTempRefactoring(JavaRefactoringArguments arguments, RefactoringStatus status) {
@@ -163,6 +171,9 @@
status.merge(initializeStatus);
}
+ public void setCheckResultForCompileProblems(boolean checkResultForCompileProblems) {
+ fCheckResultForCompileProblems = checkResultForCompileProblems;
+ }
public RefactoringStatus checkIfTempSelected() {
VariableDeclaration decl= getVariableDeclaration();
@@ -211,22 +222,13 @@
VariableDeclaration declaration= getVariableDeclaration();
result.merge(checkSelection(selected, declaration));
- if (result.hasFatalError())
- return result;
- result.merge(checkInitializer(declaration));
return result;
} finally {
pm.done();
}
}
- private RefactoringStatus checkInitializer(VariableDeclaration decl) {
- if (decl.getInitializer().getNodeType() == ASTNode.NULL_LITERAL)
- return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTemRefactoring_error_message_nulLiteralsCannotBeInlined);
- return null;
- }
-
private RefactoringStatus checkSelection(ASTNode selectedNode, VariableDeclaration decl) {
ASTNode parent= decl.getParent();
if (parent instanceof MethodDeclaration) {
@@ -283,7 +285,14 @@
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
try {
pm.beginTask("", 1); //$NON-NLS-1$
- return new RefactoringStatus();
+ CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(fCu, fASTRoot);
+
+ inlineTemp(cuRewrite);
+ removeTemp(cuRewrite);
+
+ fChange= cuRewrite.createChange(RefactoringCoreMessages.InlineTempRefactoring_inline, false, new SubProgressMonitor(pm, 1));
+
+ return fCheckResultForCompileProblems ? RefactoringAnalyzeUtil.checkNewSource(fChange, fCu, fASTRoot, pm) : new RefactoringStatus();
} finally {
pm.done();
}
@@ -295,40 +304,39 @@
public Change createChange(IProgressMonitor pm) throws CoreException {
try {
pm.beginTask(RefactoringCoreMessages.InlineTempRefactoring_preview, 2);
- final Map<String, String> arguments= new HashMap<>();
- String project= null;
- IJavaProject javaProject= fCu.getJavaProject();
- if (javaProject != null)
- project= javaProject.getElementName();
- final IVariableBinding binding= getVariableDeclaration().resolveBinding();
- String text= null;
- final IMethodBinding method= binding.getDeclaringMethod();
- if (method != null)
- text= BindingLabelProviderCore.getBindingLabel(method, JavaElementLabelsCore.ALL_FULLY_QUALIFIED);
- else
- text= BasicElementLabels.getJavaElementName('{' + JavaElementLabelsCore.ELLIPSIS_STRING + '}');
- final String description= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName()));
- final String header= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description, new String[] { BindingLabelProviderCore.getBindingLabel(binding, JavaElementLabelsCore.ALL_FULLY_QUALIFIED), text});
- final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
- comment.addSetting(Messages.format(RefactoringCoreMessages.InlineTempRefactoring_original_pattern, BindingLabelProviderCore.getBindingLabel(binding, JavaElementLabelsCore.ALL_FULLY_QUALIFIED)));
- final InlineLocalVariableDescriptor descriptor= RefactoringSignatureDescriptorFactory.createInlineLocalVariableDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE);
- arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
- arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, String.valueOf(fSelectionStart) + ' ' + String.valueOf(fSelectionLength));
-
- CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(fCu, fASTRoot);
-
- inlineTemp(cuRewrite);
- removeTemp(cuRewrite);
-
- final CompilationUnitChange result= cuRewrite.createChange(RefactoringCoreMessages.InlineTempRefactoring_inline, false, new SubProgressMonitor(pm, 1));
- result.setDescriptor(new RefactoringChangeDescriptor(descriptor));
- return result;
+ final InlineLocalVariableDescriptor descriptor= createRefactoringDescriptor();
+ fChange.setDescriptor(new RefactoringChangeDescriptor(descriptor));
+ return fChange;
} finally {
pm.done();
}
}
+ private InlineLocalVariableDescriptor createRefactoringDescriptor() {
+ final Map<String, String> arguments= new HashMap<>();
+ String project= null;
+ IJavaProject javaProject= fCu.getJavaProject();
+ if (javaProject != null)
+ project= javaProject.getElementName();
+
+ final IVariableBinding binding= getVariableDeclaration().resolveBinding();
+ String text= null;
+ final IMethodBinding method= binding.getDeclaringMethod();
+ if (method != null)
+ text= BindingLabelProviderCore.getBindingLabel(method, JavaElementLabelsCore.ALL_FULLY_QUALIFIED);
+ else
+ text= BasicElementLabels.getJavaElementName('{' + JavaElementLabelsCore.ELLIPSIS_STRING + '}');
+ final String description= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName()));
+ final String header= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_descriptor_description, new String[] { BindingLabelProviderCore.getBindingLabel(binding, JavaElementLabelsCore.ALL_FULLY_QUALIFIED), text});
+ final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header);
+ comment.addSetting(Messages.format(RefactoringCoreMessages.InlineTempRefactoring_original_pattern, BindingLabelProviderCore.getBindingLabel(binding, JavaElementLabelsCore.ALL_FULLY_QUALIFIED)));
+ final InlineLocalVariableDescriptor descriptor= RefactoringSignatureDescriptorFactory.createInlineLocalVariableDescriptor(project, description, comment.asString(), arguments, RefactoringDescriptor.NONE);
+ arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
+ arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, String.valueOf(fSelectionStart) + ' ' + String.valueOf(fSelectionLength));
+ return descriptor;
+ }
+
private void inlineTemp(CompilationUnitRewrite cuRewrite) throws JavaModelException {
SimpleName[] references= getReferences();
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 cc7f502..6a7f9ee 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,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.internal.corext.refactoring.rename;
@@ -18,14 +19,20 @@
import java.util.Iterator;
import java.util.Set;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+
import org.eclipse.text.edits.TextEdit;
import org.eclipse.jface.text.IRegion;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
+import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.eclipse.ltk.core.refactoring.TextChange;
+import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
@@ -35,14 +42,16 @@
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclaration;
-
-import org.eclipse.jdt.internal.corext.SourceRangeFactory;
-import org.eclipse.jdt.internal.corext.dom.ASTNodes;
-import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
-import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
-import org.eclipse.jdt.internal.corext.util.Messages;
+import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
+import org.eclipse.jdt.internal.corext.SourceRangeFactory;
+import org.eclipse.jdt.internal.corext.dom.ASTNodes;
+import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
+import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
+import org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext;
+import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
+import org.eclipse.jdt.internal.corext.util.Messages;
public class RefactoringAnalyzeUtil {
@@ -94,6 +103,20 @@
return subResult.toArray(new IProblem[subResult.size()]);
}
+ public static RefactoringStatus checkNewSource(CompilationUnitChange change, ICompilationUnit originalCu, CompilationUnit oldCuNode, IProgressMonitor pm) throws CoreException {
+ RefactoringStatus result= new RefactoringStatus();
+ String newCuSource= change.getPreviewContent(new NullProgressMonitor());
+ CompilationUnit newCuNode= new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL).parse(newCuSource, originalCu, true, true, pm);
+ IProblem[] newProblems= getIntroducedCompileProblems(newCuNode, oldCuNode);
+ for (int i= 0; i < newProblems.length; i++) {
+ IProblem problem= newProblems[i];
+ if (problem.isError())
+ result.addEntry(new RefactoringStatusEntry((problem.isError() ? RefactoringStatus.ERROR : RefactoringStatus.WARNING), problem.getMessage(),
+ new JavaStringStatusContext(newCuSource, SourceRangeFactory.create(problem))));
+ }
+ return result;
+ }
+
public static IRegion getNewTextRange(TextEdit edit, TextChange change){
return change.getPreviewEdit(edit).getRegion();
}
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
index 11725db..bfa7ba3 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
@@ -18,6 +18,7 @@
* Lukas Hanke <hanke@yatta.de> - Bug 430818 [1.8][quick fix] Quick fix for "for loop" is not shown for bare local variable/argument/field - https://bugs.eclipse.org/bugs/show_bug.cgi?id=430818
* Jeremie Bresson <dev@jmini.fr> - Bug 439912: [1.8][quick assist] Add quick assists to add and remove parentheses around single lambda parameter - https://bugs.eclipse.org/439912
* Jens Reimann <jens.reimann@ibh-systems.com>, Fabian Pfaff <fabian.pfaff@vogella.com> - Bug 197850: [quick assist] Add import static field/method - https://bugs.eclipse.org/bugs/show_bug.cgi?id=197850
+ * Pierre-Yves B. <pyvesdev@gmail.com> - [inline] Allow inlining of local variable initialized to null. - https://bugs.eclipse.org/93850
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.correction;
@@ -4250,6 +4251,7 @@
InlineTempRefactoring refactoring= new InlineTempRefactoring((VariableDeclaration) decl);
if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
+ refactoring.setCheckResultForCompileProblems(false);
String label= CorrectionMessages.QuickAssistProcessor_inline_local_description;
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, context.getCompilationUnit(), refactoring, IProposalRelevance.INLINE_LOCAL, image);