introduce constant: check forward reference fix ( IDEA-84139)

This commit is contained in:
anna
2012-04-06 15:01:10 +02:00
parent efec72b024
commit 8b81df9ef0
4 changed files with 51 additions and 2 deletions
@@ -855,7 +855,7 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
}
@Nullable
private static PsiField checkForwardRefs(@Nullable PsiExpression initializer, final PsiClass parentClass) {
private static PsiField checkForwardRefs(@Nullable final PsiExpression initializer, final PsiClass parentClass) {
if (initializer == null) return null;
final PsiField[] refConstantFields = new PsiField[1];
initializer.accept(new JavaRecursiveElementWalkingVisitor() {
@@ -865,7 +865,8 @@ public abstract class BaseExpressionToFieldHandler extends IntroduceHandlerBase
final PsiElement resolve = expression.resolve();
if (resolve instanceof PsiField &&
((PsiField)resolve).hasModifierProperty(PsiModifier.FINAL) &&
PsiTreeUtil.isAncestor(parentClass, resolve, false) && ((PsiField)resolve).hasInitializer()) {
PsiTreeUtil.isAncestor(parentClass, resolve, false) && ((PsiField)resolve).hasInitializer() &&
!PsiTreeUtil.isAncestor(initializer, resolve, false)) {
if (refConstantFields[0] == null || refConstantFields[0].getTextOffset() < resolve.getTextOffset()) {
refConstantFields[0] = (PsiField)resolve;
}
@@ -0,0 +1,18 @@
public class Res {
void bar(R r){}
interface R<S, T> {
void la(S s, T t);
}
private void validateStructuresCookie(Res cookie) {
cookie.bar(new <caret>R<String, Object>(){
final String param = "";
@Override
public void la(String s, Object o) {
System.out.println(param);
}
});
}
}
@@ -0,0 +1,21 @@
public class Res {
public static final Res.R<String,Object> R = new Res.R<String, Object>() {
final String param = "";
@Override
public void la(String s, Object o) {
System.out.println(param);
}
};
void bar(R r){}
interface R<S, T> {
void la(S s, T t);
}
private void validateStructuresCookie(Res cookie) {
cookie.bar(<caret>R);
}
}
@@ -92,6 +92,15 @@ public class InplaceIntroduceConstantTest extends AbstractJavaInplaceIntroduceTe
}
});
}
public void testCorrectConstantPosition() throws Exception {
doTest(new Pass<AbstractInplaceIntroducer>() {
@Override
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
type("R");
}
});
}
public void testEscapePosition() throws Exception {
doTestEscape();