inline parameter: evaluate this expression (IDEA-40666)

This commit is contained in:
anna
2010-02-02 16:16:35 +03:00
parent a2242b8c42
commit 0cc203b176
8 changed files with 106 additions and 4 deletions
@@ -220,6 +220,23 @@ public class InlineParameterExpressionProcessor {
refCannotEvaluate.set(Boolean.TRUE);
}
}
@Override
public void visitThisExpression(PsiThisExpression thisExpression) {
super.visitThisExpression(thisExpression);
final PsiJavaCodeReferenceElement qualifier = thisExpression.getQualifier();
PsiElement containingClass;
if (qualifier != null) {
containingClass = qualifier.resolve();
} else {
containingClass = PsiTreeUtil.getParentOfType(myMethodCall, PsiClass.class);
}
final PsiClass methodContainingClass = myMethod.getContainingClass();
LOG.assertTrue(methodContainingClass != null);
if (!PsiTreeUtil.isAncestor(containingClass, methodContainingClass, false)) {
refCannotEvaluate.set(Boolean.TRUE);
}
}
});
return refCannotEvaluate.isNull();
}
@@ -49,10 +49,6 @@ public class InlineParameterHandler extends JavaInlineActionHandler {
public static final String REFACTORING_NAME = RefactoringBundle.message("inline.parameter.refactoring");
public boolean canInlineElement(PsiElement element) {
return false;
}
public boolean canInlineElementInEditor(PsiElement element) {
return element instanceof PsiParameter && element.getParent() instanceof PsiParameterList;
}
@@ -0,0 +1,14 @@
public class Subject {
private int myInt;
public void withClass(Object <caret>o) {
myInt += o.hashCode();
}
}
class User {
private void oper() {
Subject subj = new Subject();
subj.withClass(this);
}
}
@@ -0,0 +1,14 @@
class Outer {
class User {
public class Subject {
public void withClass(Object <caret>o) {
System.out.println(o.toString());
}
}
private void oper() {
Subject subj = new Subject();
subj.withClass(Outer.this);
}
}
}
@@ -0,0 +1,14 @@
class Outer {
class User {
public class Subject {
public void withClass() {
System.out.println(Outer.this.toString());
}
}
private void oper() {
Subject subj = new Subject();
subj.withClass();
}
}
}
@@ -0,0 +1,15 @@
class User {
public class Subject {
private int myInt;
public void withClass(Object <caret>o) {
myInt += o.hashCode();
}
}
private void oper() {
Subject subj = new Subject();
subj.withClass(this);
}
}
@@ -0,0 +1,15 @@
class User {
public class Subject {
private int myInt;
public void withClass() {
myInt += User.this.hashCode();
}
}
private void oper() {
Subject subj = new Subject();
subj.withClass();
}
}
@@ -5,6 +5,7 @@ import com.intellij.codeInsight.TargetElementUtilBase;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.testFramework.LightCodeInsightTestCase;
import org.jetbrains.annotations.NonNls;
@@ -77,6 +78,22 @@ public class InlineParameterTest extends LightCodeInsightTestCase {
doTest(false);
}
public void testRefOuterThis() throws Exception {
try {
doTest(false);
}
catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
assertEquals("Parameter initializer depends on values which are not available inside the method and cannot be inlined", e.getMessage());
}
}
public void testRefThis() throws Exception {
doTest(false);
}
public void testRefQualifiedThis() throws Exception {
doTest(false);
}
private void doTest(final boolean createLocal) throws Exception {
getProject().putUserData(InlineParameterExpressionProcessor.CREATE_LOCAL_FOR_TESTS,createLocal);