inline parameter: check access conflicts (IDEA-40703)

This commit is contained in:
anna
2010-02-02 16:16:36 +03:00
parent 41b97a4f75
commit 107ab35006
8 changed files with 161 additions and 12 deletions
@@ -237,6 +237,27 @@ public class InlineParameterExpressionProcessor {
refCannotEvaluate.set(Boolean.TRUE);
}
}
@Override
public void visitNewExpression(PsiNewExpression expression) {
super.visitNewExpression(expression);
final PsiJavaCodeReferenceElement reference = expression.getClassOrAnonymousClassReference();
if (reference != null) {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiClass) {
final PsiClass refClass = (PsiClass)resolved;
if (!PsiUtil.isAccessible(refClass, myMethod, null)) {
refCannotEvaluate.set(Boolean.TRUE);
} else {
final PsiClass methodContainingClass = myMethod.getContainingClass();
LOG.assertTrue(methodContainingClass != null);
if (!(refClass.getParent() instanceof PsiFile) && !PsiTreeUtil.isAncestor(methodContainingClass, refClass, false)) {
refCannotEvaluate.set(Boolean.TRUE);
}
}
}
}
}
});
return refCannotEvaluate.isNull();
}
@@ -0,0 +1,17 @@
public class Subject {
private int myInt;
public void withClass(Object <caret>o) {
myInt += o.hashCode();
}
}
class User {
private void oper() throws IOException {
Subject subj = new Subject();
subj.withClass(new Local());
}
class Local {
}
}
@@ -0,0 +1,21 @@
public class Subject {
private int myInt;
public void withClass(Object <caret>o) {
myInt += o.hashCode();
}
class User {
private void oper() throws IOException {
Subject subj = new Subject();
subj.withClass(new Local());
}
}
class Local {
}
}
@@ -0,0 +1,21 @@
public class Subject {
private int myInt;
public void withClass() {
myInt += new Local().hashCode();
}
class User {
private void oper() throws IOException {
Subject subj = new Subject();
subj.withClass();
}
}
class Local {
}
}
@@ -0,0 +1,18 @@
public class Subject {
private int myInt;
public void withClass(Object <caret>o) {
myInt += o.hashCode();
}
}
class User {
private void oper() throws IOException {
Subject subj = new Subject();
class Local {
}
subj.withClass(new Local());
}
}
@@ -0,0 +1,16 @@
public class Subject {
private int myInt;
public void withClass(Object <caret>o) {
myInt += o.hashCode();
}
}
class User {
private void oper() throws IOException {
Subject subj = new Subject();
subj.withClass(new TopLevel());
}
}
class TopLevel {}
@@ -0,0 +1,16 @@
public class Subject {
private int myInt;
public void withClass() {
myInt += new TopLevel().hashCode();
}
}
class User {
private void oper() throws IOException {
Subject subj = new Subject();
subj.withClass();
}
}
class TopLevel {}
@@ -79,12 +79,7 @@ public class InlineParameterTest extends LightCodeInsightTestCase {
}
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());
}
doTestParamInitializerDependsOnUnavalableValues();
}
public void testRefThis() throws Exception {
@@ -100,12 +95,7 @@ public class InlineParameterTest extends LightCodeInsightTestCase {
}
public void testRefSameNonFinalFieldOtherObject() throws Exception {
try {
doTest(false);
}
catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
assertEquals("Cannot find constant initializer for parameter", e.getMessage());
}
doTestCannotFindInitializer();
}
public void testRef2ConstantsWithTheSameValue() throws Exception {
@@ -113,6 +103,35 @@ public class InlineParameterTest extends LightCodeInsightTestCase {
}
public void testRefConstantAndField() throws Exception {
doTestCannotFindInitializer();
}
public void testRefNewInner() throws Exception {
doTestParamInitializerDependsOnUnavalableValues();
}
private void doTestParamInitializerDependsOnUnavalableValues() 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 testRefNewInnerForMethod() throws Exception {
doTest(false);
}
public void testRefNewTopLevel() throws Exception {
doTest(false);
}
public void testRefNewLocal() throws Exception {
doTestParamInitializerDependsOnUnavalableValues();
}
private void doTestCannotFindInitializer() throws Exception {
try {
doTest(false);
}