mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
qualify field access in initializer when shadowed by new declaration (IDEA-173780)
This commit is contained in:
@@ -1,18 +1,4 @@
|
||||
/*
|
||||
* Copyright 2000-2017 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.
|
||||
*/
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilCore;
|
||||
@@ -22,6 +8,7 @@ import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
||||
import com.intellij.codeInsight.template.*;
|
||||
import com.intellij.codeInsight.template.impl.TemplateState;
|
||||
import com.intellij.ide.DataManager;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
@@ -39,6 +26,7 @@ import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.refactoring.JavaRefactoringSettings;
|
||||
import com.intellij.refactoring.introduceVariable.IntroduceVariableBase;
|
||||
import com.intellij.refactoring.util.RefactoringChangeUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.EquivalenceChecker;
|
||||
@@ -221,6 +209,44 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
||||
newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
|
||||
|
||||
CreateFromUsageBaseFix.startTemplate(newEditor, template, project, new TemplateEditingAdapter() {
|
||||
|
||||
@Override
|
||||
public void beforeTemplateFinished(@NotNull TemplateState state, Template template) {
|
||||
final TextResult value = state.getVariableValue("");
|
||||
assert value != null;
|
||||
|
||||
final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(project).getResolveHelper();
|
||||
final PsiVariable target = resolveHelper.resolveAccessibleReferencedVariable(value.getText(), instanceOfExpression);
|
||||
if (target instanceof PsiField) {
|
||||
final PsiField field = (PsiField)target;
|
||||
final CaretModel caretModel = editor.getCaretModel();
|
||||
final PsiElement elementAt = file.findElementAt(caretModel.getOffset());
|
||||
final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementAt, PsiDeclarationStatement.class);
|
||||
if (declarationStatement != null) {
|
||||
final PsiLocalVariable variable = (PsiLocalVariable)declarationStatement.getDeclaredElements()[0];
|
||||
final PsiExpression initializer = variable.getInitializer();
|
||||
assert initializer != null;
|
||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||
initializer.accept(new JavaRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitReferenceExpression(PsiReferenceExpression expression) {
|
||||
final PsiExpression qualifierExpression = expression.getQualifierExpression();
|
||||
if (qualifierExpression != null) {
|
||||
qualifierExpression.accept(this);
|
||||
}
|
||||
else if (expression.resolve() == variable) {
|
||||
RefactoringChangeUtil.qualifyReference(expression, field, field.hasModifierProperty(PsiModifier.STATIC)
|
||||
? field.getContainingClass()
|
||||
: null);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void templateFinished(@NotNull Template template, boolean brokenOff) {
|
||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Insert '(String)x' declaration" "true"
|
||||
|
||||
class C {
|
||||
Object x = new Object();
|
||||
|
||||
void x() {
|
||||
if (x instanceof String) {
|
||||
String x = (String) this.x;
|
||||
<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Insert '(String)x' declaration" "true"
|
||||
|
||||
class C {
|
||||
Object x = new Object();
|
||||
|
||||
void x() {
|
||||
if (x insta<caret>nceof String) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user