From 0e8e0d3cb80777c87512e477f218c7e23dea41ae Mon Sep 17 00:00:00 2001 From: Egor Ushakov Date: Fri, 22 Dec 2017 11:15:53 +0300 Subject: [PATCH] IDEA-184127 Field reference is highlighted with red in Evaluate window in debugger --- .../resolve/VariableResolverProcessor.java | 24 ++++------ ...solveFieldInInnerWithVisiblityChecker.java | 11 +++++ ...ieldInStaticInnerWithVisiblityChecker.java | 11 +++++ .../resolve/ResolveInCodeFragmentTest.java | 47 ++++++++++++------- 4 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInInnerWithVisiblityChecker.java create mode 100644 java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInStaticInnerWithVisiblityChecker.java diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/VariableResolverProcessor.java b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/VariableResolverProcessor.java index e61d44821186..9ae2fb067963 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/VariableResolverProcessor.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/VariableResolverProcessor.java @@ -1,17 +1,5 @@ /* - * Copyright 2000-2016 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-2017 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.psi.impl.source.resolve; @@ -66,9 +54,13 @@ public class VariableResolverProcessor extends ConflictFilterProcessor implement @Override public final void handleEvent(@NotNull PsiScopeProcessor.Event event, Object associated) { - // do not resolve conflicts on CHANGE_LEVEL if VisibilityChecker is present - if (event == JavaScopeProcessorEvent.CHANGE_LEVEL && - (!(myPlaceFile instanceof JavaCodeFragment) || ((JavaCodeFragment)myPlaceFile).getVisibilityChecker() == null)) { + // Special handling when VisibilityChecker is present + if (myPlaceFile instanceof JavaCodeFragment && ((JavaCodeFragment)myPlaceFile).getVisibilityChecker() != null) { + if (event == JavaScopeProcessorEvent.SET_CURRENT_FILE_CONTEXT && myName != null) { + getResult(); + } + } + else { super.handleEvent(event, associated); } if(event == JavaScopeProcessorEvent.START_STATIC){ diff --git a/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInInnerWithVisiblityChecker.java b/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInInnerWithVisiblityChecker.java new file mode 100644 index 000000000000..581c706939c9 --- /dev/null +++ b/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInInnerWithVisiblityChecker.java @@ -0,0 +1,11 @@ +public class FieldVsOuter2 { + public final String field = "xxx"; + + private class Inner { + public final String field = "yyy"; + + public void foo() { + System.out.println(field); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInStaticInnerWithVisiblityChecker.java b/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInStaticInnerWithVisiblityChecker.java new file mode 100644 index 000000000000..115b1a737ac5 --- /dev/null +++ b/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldInStaticInnerWithVisiblityChecker.java @@ -0,0 +1,11 @@ +public class FieldVsOuter { + public final String field = "xxx"; + + private static class Inner { + public final String field = "yyy"; + + public void foo() { + System.out.println(field); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/psi/resolve/ResolveInCodeFragmentTest.java b/java/java-tests/testSrc/com/intellij/java/psi/resolve/ResolveInCodeFragmentTest.java index c157da1c14a7..afec9ee17c8a 100644 --- a/java/java-tests/testSrc/com/intellij/java/psi/resolve/ResolveInCodeFragmentTest.java +++ b/java/java-tests/testSrc/com/intellij/java/psi/resolve/ResolveInCodeFragmentTest.java @@ -1,4 +1,6 @@ -// Copyright 2000-2017 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. +/* + * Copyright 2000-2017 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.java.psi.resolve; import com.intellij.codeInsight.AnnotationUtil; @@ -10,6 +12,8 @@ import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.testFramework.ResolveTestCase; +import java.util.function.Consumer; + /** * @author max */ @@ -49,35 +53,42 @@ public class ResolveInCodeFragmentTest extends ResolveTestCase { } public void testResolveFieldVsLocalWithVisiblityChecker() throws Exception { - PsiReference iRef = configure(); - - JavaCodeFragment codeFragment = JavaCodeFragmentFactory.getInstance(myProject).createExpressionCodeFragment( - "xxx", iRef.getElement(), null, true); - codeFragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE); - - PsiElement[] fileContent = codeFragment.getChildren(); - assertEquals(1, fileContent.length); - assertTrue(fileContent[0] instanceof PsiExpression); - - PsiExpression expr = (PsiExpression) fileContent[0]; - PsiElement resolve = ((PsiReferenceExpressionImpl)expr).resolve(); - assertInstanceOf(resolve, PsiLocalVariable.class); + doTestResolveWithVisibilityChecker("xxx", e -> assertInstanceOf(e, PsiLocalVariable.class)); } public void testResolveFieldVsParamWithVisiblityChecker() throws Exception { + doTestResolveWithVisibilityChecker("field", e -> assertInstanceOf(e, PsiParameter.class)); + } + + public void testResolveFieldInStaticInnerWithVisiblityChecker() throws Exception { + doTestResolveWithVisibilityChecker("field", e -> { + assertInstanceOf(e, PsiField.class); + assertEquals("Inner", ((PsiField)e).getContainingClass().getName()); + }); + } + + public void testResolveFieldInInnerWithVisiblityChecker() throws Exception { + doTestResolveWithVisibilityChecker("field", e -> { + assertInstanceOf(e, PsiField.class); + assertEquals("Inner", ((PsiField)e).getContainingClass().getName()); + }); + } + + private void doTestResolveWithVisibilityChecker(String field, Consumer checker) throws Exception { PsiReference iRef = configure(); JavaCodeFragment codeFragment = JavaCodeFragmentFactory.getInstance(myProject).createExpressionCodeFragment( - "field", iRef.getElement(), null, true); + field, iRef.getElement(), null, true); codeFragment.setVisibilityChecker(JavaCodeFragment.VisibilityChecker.EVERYTHING_VISIBLE); PsiElement[] fileContent = codeFragment.getChildren(); assertEquals(1, fileContent.length); assertTrue(fileContent[0] instanceof PsiExpression); - PsiExpression expr = (PsiExpression) fileContent[0]; - PsiElement resolve = ((PsiReferenceExpressionImpl)expr).resolve(); - assertInstanceOf(resolve, PsiParameter.class); + PsiExpression expr = (PsiExpression)fileContent[0]; + JavaResolveResult[] results = ((PsiReferenceExpressionImpl)expr).multiResolve(false); + assertSize(1, results); + checker.accept(results[0].getElement()); } private PsiReference configure() throws Exception {