inline parameter: check static access (IDEA-52170)

This commit is contained in:
anna
2010-02-11 17:56:31 +03:00
parent 7c2d78c47c
commit 292373e213
5 changed files with 102 additions and 0 deletions
@@ -173,6 +173,11 @@ public class InlineParameterExpressionProcessor extends BaseRefactoringProcessor
public void visitReferenceExpression(final PsiReferenceExpression expression) {
super.visitReferenceExpression(expression);
final PsiElement element = expression.resolve();
if (element instanceof PsiMember && !((PsiModifierListOwner)element).hasModifierProperty(PsiModifier.STATIC)) {
if (myMethod.hasModifierProperty(PsiModifier.STATIC)) {
conflicts.putValue(expression, "Parameter initializer depends on " + RefactoringUIUtil.getDescription(element, false) + " which is not available inside the static method");
}
}
if (element instanceof PsiMethod || element instanceof PsiField) {
if (!mySameClass && !((PsiModifierListOwner)element).hasModifierProperty(PsiModifier.STATIC)) {
conflicts.putValue(expression, "Parameter initializer depend on non static member from some other class");
@@ -199,6 +204,20 @@ public class InlineParameterExpressionProcessor extends BaseRefactoringProcessor
conflicts.putValue(thisExpression,
"Parameter initializer depends on this which is not available inside the method and cannot be inlined");
}
if (myMethod.hasModifierProperty(PsiModifier.STATIC)) {
conflicts.putValue(thisExpression, "Parameter initializer depends on this which is not available inside the static method");
}
}
@Override
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
super.visitReferenceElement(reference);
if (myMethod.hasModifierProperty(PsiModifier.STATIC)) {
final PsiElement resolved = reference.resolve();
if (resolved instanceof PsiClass && !((PsiClass)resolved).hasModifierProperty(PsiModifier.STATIC)) {
conflicts.putValue(reference, "Parameter initializer depends on non static class which is not available inside static method");
}
}
}
@Override
@@ -0,0 +1,18 @@
public class ExpData {
private Object provideObject() {
return new Object();
}
public static void useStatic(Object p) {
System.out.println(p);
}
public void context() {
inlineE(provideObject());
}
public static void inlineE(Object <caret>subj) {
useStatic(subj);
}
}
@@ -0,0 +1,20 @@
public class ExpData {
private Object provideObject() {
return new Object();
}
public static void useStatic(Object p) {
System.out.println(p);
}
public void context() {
inlineE(new DD());
}
public static void inlineE(Object <caret>subj) {
useStatic(subj);
}
class DD {}
}
@@ -0,0 +1,18 @@
public class ExpData {
private Object provideObject() {
return new Object();
}
public static void useStatic(Object p) {
System.out.println(p);
}
public void context() {
inlineE(this);
}
public static void inlineE(Object <caret>subj) {
useStatic(subj);
}
}
@@ -192,6 +192,33 @@ public class InlineParameterTest extends LightCodeInsightTestCase {
}
}
public void testRefNonStatic() throws Exception {
try {
doTest(false);
}
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
assertEquals("Parameter initializer depends on method <b><code>provideObject()</code></b> which is not available inside the static method", e.getMessage());
}
}
public void testRefNonStaticClass() throws Exception {
try {
doTest(false);
}
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
assertEquals("Parameter initializer depends on non static class which is not available inside static method", e.getMessage());
}
}
public void testRefThisFromStatic() throws Exception {
try {
doTest(false);
}
catch (BaseRefactoringProcessor.ConflictsInTestsException e) {
assertEquals("Parameter initializer depends on this which is not available inside the static method", e.getMessage());
}
}
private void doTest(final boolean createLocal) throws Exception {
getProject().putUserData(InlineParameterExpressionProcessor.CREATE_LOCAL_FOR_TESTS,createLocal);