From bd8e7b33fd10b5270a22434d6cf98988f4679139 Mon Sep 17 00:00:00 2001 From: anna Date: Fri, 27 May 2011 15:42:39 +0400 Subject: [PATCH] qualify with this refs in super calls when containing class has fields with the same names --- .../daemon/impl/analysis/HighlightUtil.java | 13 +++- .../impl/analysis/QualifyWithThisFix.java | 66 +++++++++++++++++++ .../afterQualifyWithThis.java | 15 +++++ .../beforeAlreadyQualified.java | 15 +++++ .../beforeNoCandidate.java | 14 ++++ .../beforeQualifyWithThis.java | 15 +++++ .../beforeThisInaccessible.java | 15 +++++ .../quickFix/QualifyWithThisFixTest.java | 29 ++++++++ 8 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/afterQualifyWithThis.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeAlreadyQualified.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeNoCandidate.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeQualifyWithThis.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeThisInaccessible.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/QualifyWithThisFixTest.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java index bd16d6051fc5..dd55d137604e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -1648,7 +1648,18 @@ public class HighlightUtil { !thisOrSuperReference(((PsiReferenceExpression)expression).getQualifierExpression(), aClass)) { return null; } - return createMemberReferencedError(resolvedName, expression.getTextRange()); + final HighlightInfo highlightInfo = createMemberReferencedError(resolvedName, expression.getTextRange()); + if (expression instanceof PsiReferenceExpression && PsiUtil.isInnerClass(aClass)) { + final String referenceName = ((PsiReferenceExpression)expression).getReferenceName(); + final PsiClass containingClass = aClass.getContainingClass(); + LOG.assertTrue(containingClass != null); + final PsiField fieldInContainingClass = containingClass.findFieldByName(referenceName, true); + if (fieldInContainingClass != null && ((PsiReferenceExpression)expression).getQualifierExpression() == null) { + QuickFixAction.registerQuickFixAction(highlightInfo, new QualifyWithThisFix(containingClass, expression)); + } + } + + return highlightInfo; } element = element.getParent(); } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java new file mode 100644 index 000000000000..123dbdad9363 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/QualifyWithThisFix.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.daemon.impl.analysis; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.refactoring.util.RefactoringUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; + +/** +* User: anna +*/ +class QualifyWithThisFix implements IntentionAction { + private final PsiClass myContainingClass; + private final PsiElement myExpression; + + public QualifyWithThisFix(PsiClass containingClass, PsiElement expression) { + myContainingClass = containingClass; + myExpression = expression; + } + + @NotNull + @Override + public String getText() { + return "Qualify with " + myContainingClass.getName() + ".this"; + } + + @NotNull + @Override + public String getFamilyName() { + return getText(); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return true; + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + final PsiThisExpression thisExpression = + RefactoringUtil.createThisExpression(PsiManager.getInstance(project), myContainingClass); + ((PsiReferenceExpression)myExpression).setQualifierExpression(thisExpression); + } + + @Override + public boolean startInWriteAction() { + return true; + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/afterQualifyWithThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/afterQualifyWithThis.java new file mode 100644 index 000000000000..3a51862e7320 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/afterQualifyWithThis.java @@ -0,0 +1,15 @@ +// "Qualify with Test.this" "true" +class Test { + String myStr; + class Foo extends Super { + Foo() { + super(Test.this.myStr); + } + } +} + +class Super { + protected String myStr; + Super(String s) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeAlreadyQualified.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeAlreadyQualified.java new file mode 100644 index 000000000000..516b42996f68 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeAlreadyQualified.java @@ -0,0 +1,15 @@ +// "Qualify with Test.this" "false" +class Test { + String myStr; + class Foo extends Super { + Foo(Test t) { + super(t.myStr); + } + } +} + +class Super { + protected String myStr; + Super(String s) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeNoCandidate.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeNoCandidate.java new file mode 100644 index 000000000000..8ebd0438a3db --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeNoCandidate.java @@ -0,0 +1,14 @@ +// "Qualify with Test.this" "false" +class Test { + class Foo extends Super { + Foo() { + super(myStr); + } + } +} + +class Super { + protected String myStr; + Super(String s) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeQualifyWithThis.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeQualifyWithThis.java new file mode 100644 index 000000000000..9d579aa45bd1 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeQualifyWithThis.java @@ -0,0 +1,15 @@ +// "Qualify with Test.this" "true" +class Test { + String myStr; + class Foo extends Super { + Foo() { + super(myStr); + } + } +} + +class Super { + protected String myStr; + Super(String s) { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeThisInaccessible.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeThisInaccessible.java new file mode 100644 index 000000000000..fa5ba50cb89b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall/beforeThisInaccessible.java @@ -0,0 +1,15 @@ +// "Qualify with Test.this" "false" +class Test { + String myStr; + static class Foo extends Super { + Foo() { + super(myStr); + } + } +} + +class Super { + protected String myStr; + Super(String s) { + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/QualifyWithThisFixTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/QualifyWithThisFixTest.java new file mode 100644 index 000000000000..1e8d2ede18e2 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/QualifyWithThisFixTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.daemon.quickFix; + +/** + * @author anna + */ +public class QualifyWithThisFixTest extends LightQuickFixTestCase { + + public void test() throws Exception { doAllTests(); } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/qualifyWithThisInSuperCall"; + } +}