inline parameter: make it public; allow to inline refs to the same non final fields; allow to inline different constants with the same value (IDEA-40700)

This commit is contained in:
anna
2010-02-01 19:47:39 +03:00
parent 0cc203b176
commit 41b97a4f75
8 changed files with 132 additions and 10 deletions

View File

@@ -15,10 +15,10 @@
*/
package com.intellij.refactoring.inline;
import com.intellij.codeInsight.PsiEquivalenceUtil;
import com.intellij.codeInspection.sameParameterValue.SameParameterValueInspection;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.ex.ApplicationEx;
import com.intellij.openapi.command.UndoConfirmationPolicy;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
@@ -76,13 +76,16 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
boolean result = ReferencesSearch.search(method).forEach(new Processor<PsiReference>() {
public boolean process(final PsiReference psiReference) {
PsiElement element = psiReference.getElement();
if (element.getParent() instanceof PsiCallExpression) {
final PsiElement parent = element.getParent();
if (parent instanceof PsiCallExpression) {
final PsiCallExpression methodCall = (PsiCallExpression)parent;
occurrences.add(psiReference);
containingFiles.add(element.getContainingFile());
PsiCallExpression methodCall = (PsiCallExpression) element.getParent();
PsiExpression argument = methodCall.getArgumentList().getExpressions()[index];
if (!refInitializer.isNull()) {
return false;
return argument != null
&& PsiEquivalenceUtil.areElementsEquivalent(refInitializer.get(), argument)
&& PsiEquivalenceUtil.areElementsEquivalent(refMethodCall.get(), methodCall);
}
if (InlineToAnonymousConstructorProcessor.isConstant(argument) || getReferencedFinalField(argument) != null) {
if (refConstantInitializer.isNull()) {
@@ -93,6 +96,7 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
}
}
else {
if (!refConstantInitializer.isNull()) return false;
refInitializer.set(argument);
refMethodCall.set(methodCall);
}
@@ -108,8 +112,7 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
CommonRefactoringUtil.showErrorHint(project, editor, "Cannot find constant initializer for parameter", RefactoringBundle.message("inline.parameter.refactoring"), null);
return;
}
final ApplicationEx app = (ApplicationEx)ApplicationManager.getApplication();
if ((app.isInternal() || app.isUnitTestMode()) && !refInitializer.isNull()) {
if (!refInitializer.isNull()) {
try {
new InlineParameterExpressionProcessor(refMethodCall.get(), method, psiParameter, refInitializer.get(), editor).run();
}
@@ -174,10 +177,10 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
if (expr1Null || expr2Null) {
return expr1Null && expr2Null;
}
PsiField field1 = getReferencedFinalField(expr1);
PsiField field2 = getReferencedFinalField(expr2);
if (field1 != null || field2 != null) {
return field1 == field2;
final PsiField field1 = getReferencedFinalField(expr1);
final PsiField field2 = getReferencedFinalField(expr2);
if (field1 != null && field1 == field2) {
return true;
}
Object value1 = JavaPsiFacade.getInstance(expr1.getProject()).getConstantEvaluationHelper().computeConstantExpression(expr1);
Object value2 = JavaPsiFacade.getInstance(expr2.getProject()).getConstantEvaluationHelper().computeConstantExpression(expr2);

View File

@@ -0,0 +1,18 @@
public class Subject {
private int myInt;
public void wp(int <caret>p) {
myInt += p;
}
}
class User {
public static final int OUR_CONST = 2;
public static final int OUR_CONST2 = 2;
private void oper() {
Subject subj = new Subject();
subj.wp(OUR_CONST);
subj.wp(OUR_CONST2);
}
}

View File

@@ -0,0 +1,18 @@
public class Subject {
private int myInt;
public void wp() {
myInt += User.OUR_CONST;
}
}
class User {
public static final int OUR_CONST = 2;
public static final int OUR_CONST2 = 2;
private void oper() {
Subject subj = new Subject();
subj.wp();
subj.wp();
}
}

View File

@@ -0,0 +1,18 @@
public class Subject {
private int myInt;
public void wp(int <caret>p) {
myInt += p;
}
}
class User {
public static int ourStatic = 0;
public static final int OUR_CONST = 0;
private void oper() {
Subject subj = new Subject();
subj.wp(OUR_CONST);
subj.wp(ourStatic);
}
}

View File

@@ -0,0 +1,13 @@
public class Subject {
private int myInt;
private int t;
public void wp(int <caret>p) {
myInt += p;
}
void foo() {
wp(t);
wp(t);
}
}

View File

@@ -0,0 +1,13 @@
public class Subject {
private int myInt;
private int t;
public void wp() {
myInt += t;
}
void foo() {
wp();
wp();
}
}

View File

@@ -0,0 +1,13 @@
public class Subject {
private int myInt;
private int t;
public void wp(int <caret>p) {
myInt += p;
}
void foo(Subject s) {
wp(s.t);
wp(t);
}
}

View File

@@ -95,6 +95,32 @@ public class InlineParameterTest extends LightCodeInsightTestCase {
doTest(false);
}
public void testRefSameNonFinalField() throws Exception {
doTest(false);
}
public void testRefSameNonFinalFieldOtherObject() throws Exception {
try {
doTest(false);
}
catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
assertEquals("Cannot find constant initializer for parameter", e.getMessage());
}
}
public void testRef2ConstantsWithTheSameValue() throws Exception {
doTest(false);
}
public void testRefConstantAndField() throws Exception {
try {
doTest(false);
}
catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
assertEquals("Cannot find constant initializer for parameter", e.getMessage());
}
}
private void doTest(final boolean createLocal) throws Exception {
getProject().putUserData(InlineParameterExpressionProcessor.CREATE_LOCAL_FOR_TESTS,createLocal);