From fb3a6056378c94a949f6326eaf2ec39bfeeb2f9c Mon Sep 17 00:00:00 2001 From: Egor Ushakov Date: Tue, 10 Oct 2017 10:00:33 +0300 Subject: [PATCH] IDEA-180061 Debugger evaluates private field instead of captured local --- .../JavaVariableConflictResolver.java | 23 ++----------- ...solveFieldVsParamWithVisiblityChecker.java | 20 ++++++++++++ .../resolve/ResolveInCodeFragmentTest.java | 32 ++++++++++--------- 3 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldVsParamWithVisiblityChecker.java diff --git a/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaVariableConflictResolver.java b/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaVariableConflictResolver.java index 76b6d5fd43ce..e7ebcaa6f0bd 100644 --- a/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaVariableConflictResolver.java +++ b/java/java-psi-impl/src/com/intellij/psi/scope/conflictResolvers/JavaVariableConflictResolver.java @@ -1,24 +1,7 @@ -/* - * 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.scope.conflictResolvers; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiField; -import com.intellij.psi.PsiLocalVariable; +import com.intellij.psi.*; import com.intellij.psi.infos.CandidateInfo; import com.intellij.psi.scope.PsiConflictResolver; import org.jetbrains.annotations.NotNull; @@ -45,7 +28,7 @@ public class JavaVariableConflictResolver implements PsiConflictResolver{ final PsiElement otherElement = candidate.getElement(); if (!(otherElement instanceof PsiField)) { - if (otherElement instanceof PsiLocalVariable) { + if (otherElement instanceof PsiLocalVariable || otherElement instanceof PsiParameter) { return candidate; } else { diff --git a/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldVsParamWithVisiblityChecker.java b/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldVsParamWithVisiblityChecker.java new file mode 100644 index 000000000000..b826534799f7 --- /dev/null +++ b/java/java-tests/testData/psi/resolve/codeFragment/ResolveFieldVsParamWithVisiblityChecker.java @@ -0,0 +1,20 @@ +public class PrivateFieldVsParam { + public static void main(String[] args) { + start(5); + } + + static void start(int field) { + new Cls() { + @Override + void foo() { + System.out.println(field); + } + }.foo(); + } + + private static abstract class Cls { + private String field = "xxx"; + + abstract void foo(); + } +} \ 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 d6bcf06e6911..c157da1c14a7 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,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-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; @@ -78,6 +64,22 @@ public class ResolveInCodeFragmentTest extends ResolveTestCase { assertInstanceOf(resolve, PsiLocalVariable.class); } + public void testResolveFieldVsParamWithVisiblityChecker() throws Exception { + PsiReference iRef = configure(); + + JavaCodeFragment codeFragment = JavaCodeFragmentFactory.getInstance(myProject).createExpressionCodeFragment( + "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); + } + private PsiReference configure() throws Exception { return configureByFile("codeFragment/" + getTestName(false) + ".java"); }