Evaluate expression: allow to call invisible members
#KT-4935 Fixed
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2010-2014 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 org.jetbrains.jet.lang.psi.codeFragmentUtil
|
||||
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
import org.jetbrains.jet.lang.psi.JetCodeFragment
|
||||
|
||||
public val SKIP_VISIBILITY_CHECK: Key<Boolean> = Key.create<Boolean>("SKIP_VISIBILITY_CHECK")
|
||||
|
||||
fun JetFile.skipVisibilityCheck() =
|
||||
when (this) {
|
||||
is JetCodeFragment -> true
|
||||
is JetFile -> getUserData(SKIP_VISIBILITY_CHECK) ?: false
|
||||
else -> false
|
||||
}
|
||||
|
||||
fun JetFile.setSkipVisibilityCheck(skip: Boolean) {
|
||||
putUserData(SKIP_VISIBILITY_CHECK, skip)
|
||||
}
|
||||
|
||||
+3
-2
@@ -25,6 +25,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.diagnostics.Errors;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.psi.codeFragmentUtil.CodeFragmentUtilPackage;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
|
||||
@@ -420,8 +421,8 @@ public class QualifiedExpressionResolver {
|
||||
|
||||
private void checkVisibility(@NotNull DeclarationDescriptorWithVisibility descriptor, @NotNull BindingTrace trace,
|
||||
@NotNull JetSimpleNameExpression referenceExpression, @NotNull JetScope scopeToCheckVisibility) {
|
||||
|
||||
if (!Visibilities.isVisible(descriptor, scopeToCheckVisibility.getContainingDeclaration())) {
|
||||
if (!CodeFragmentUtilPackage.skipVisibilityCheck(referenceExpression.getContainingJetFile()) &&
|
||||
!Visibilities.isVisible(descriptor, scopeToCheckVisibility.getContainingDeclaration())) {
|
||||
trace.report(INVISIBLE_REFERENCE.on(referenceExpression, descriptor, descriptor.getVisibility(), descriptor.getContainingDeclaration()));
|
||||
}
|
||||
}
|
||||
|
||||
+5
-4
@@ -24,10 +24,8 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.evaluate.ConstantExpressionEvaluator;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
|
||||
import org.jetbrains.jet.lang.psi.codeFragmentUtil.CodeFragmentUtilPackage;
|
||||
import org.jetbrains.jet.lang.resolve.*;
|
||||
import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext;
|
||||
import org.jetbrains.jet.lang.resolve.calls.context.CheckValueArgumentsMode;
|
||||
import org.jetbrains.jet.lang.resolve.calls.context.ResolutionContext;
|
||||
@@ -141,6 +139,9 @@ public class CallExpressionResolver {
|
||||
@NotNull ExpressionTypingContext context
|
||||
) {
|
||||
if (!(classifier instanceof ClassDescriptor)) return;
|
||||
|
||||
if (CodeFragmentUtilPackage.skipVisibilityCheck(expression.getContainingJetFile())) return;
|
||||
|
||||
ClassDescriptor classObject = ((ClassDescriptor) classifier).getClassObjectDescriptor();
|
||||
assert classObject != null : "This check should be done only for classes with class objects: " + classifier;
|
||||
DeclarationDescriptor from = context.containingDeclaration;
|
||||
|
||||
@@ -98,8 +98,6 @@ public class CandidateResolver {
|
||||
if (invisibleMember != null) {
|
||||
candidateCall.addStatus(OTHER_ERROR);
|
||||
context.tracing.invisibleMember(context.trace, invisibleMember);
|
||||
markAllArgumentsAsUnmapped(context);
|
||||
return;
|
||||
}
|
||||
|
||||
if (task.checkArguments == CheckValueArgumentsMode.ENABLED) {
|
||||
|
||||
+4
-2
@@ -25,6 +25,7 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorWithVisibility;
|
||||
import org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.psi.codeFragmentUtil.CodeFragmentUtilPackage;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.calls.inference.*;
|
||||
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||
@@ -41,7 +42,6 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.diagnostics.Errors.*;
|
||||
import static org.jetbrains.jet.lang.diagnostics.Errors.TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER;
|
||||
import static org.jetbrains.jet.lang.resolve.BindingContext.AMBIGUOUS_REFERENCE_TARGET;
|
||||
import static org.jetbrains.jet.lang.types.TypeUtils.noExpectedType;
|
||||
|
||||
@@ -175,7 +175,9 @@ public abstract class AbstractTracingStrategy implements TracingStrategy {
|
||||
|
||||
@Override
|
||||
public void invisibleMember(@NotNull BindingTrace trace, @NotNull DeclarationDescriptorWithVisibility descriptor) {
|
||||
trace.report(INVISIBLE_MEMBER.on(call.getCallElement(), descriptor, descriptor.getVisibility(), descriptor.getContainingDeclaration()));
|
||||
if (!CodeFragmentUtilPackage.skipVisibilityCheck(call.getCallElement().getContainingJetFile())) {
|
||||
trace.report(INVISIBLE_MEMBER.on(call.getCallElement(), descriptor, descriptor.getVisibility(), descriptor.getContainingDeclaration()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -39,7 +39,7 @@ class B {
|
||||
|
||||
fun test3(a: A) {
|
||||
a.<!INVISIBLE_MEMBER!>v<!> //todo .bMethod()
|
||||
a.<!INVISIBLE_MEMBER!>f<!>(0, 1) //todo .bMethod()
|
||||
a.<!INVISIBLE_MEMBER!>f<!>(0, <!TOO_MANY_ARGUMENTS!>1<!>) //todo .bMethod()
|
||||
}
|
||||
|
||||
trait T
|
||||
|
||||
@@ -62,6 +62,7 @@ import org.jetbrains.jet.plugin.caches.resolve.getAnalysisResults
|
||||
import org.jetbrains.jet.lang.psi.JetCodeFragment
|
||||
import org.jetbrains.jet.lang.psi.JetImportList
|
||||
import org.jetbrains.jet.lang.psi.JetExpression
|
||||
import org.jetbrains.jet.lang.psi.codeFragmentUtil.setSkipVisibilityCheck
|
||||
import org.jetbrains.jet.plugin.refactoring.extractFunction.AnalysisResult.Status
|
||||
|
||||
object KotlinEvaluationBuilder: EvaluatorBuilder {
|
||||
@@ -225,8 +226,10 @@ private fun createFileForDebugger(codeFragment: JetExpressionCodeFragment,
|
||||
|
||||
val virtualFile = LightVirtualFile("debugFile.kt", JetLanguage.INSTANCE, fileText)
|
||||
virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET)
|
||||
return (PsiFileFactory.getInstance(codeFragment.getProject()) as PsiFileFactoryImpl)
|
||||
val jetFile = (PsiFileFactory.getInstance(codeFragment.getProject()) as PsiFileFactoryImpl)
|
||||
.trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false) as JetFile
|
||||
jetFile.setSkipVisibilityCheck(true)
|
||||
return jetFile
|
||||
}
|
||||
|
||||
fun addImportsToFile(newImportList: JetImportList?, tmpFile: JetFile) {
|
||||
@@ -272,6 +275,8 @@ private fun getFunctionForExtractedFragment(
|
||||
if (lineStart == null) return null
|
||||
|
||||
val tmpFile = originalFile.createTempCopy { it }
|
||||
tmpFile.setSkipVisibilityCheck(true)
|
||||
|
||||
val elementAtOffset = tmpFile.findElementAt(lineStart)
|
||||
if (elementAtOffset == null) return null
|
||||
|
||||
|
||||
@@ -98,6 +98,8 @@ import org.jetbrains.jet.lang.psi.JetTypeReference
|
||||
import org.jetbrains.jet.lang.psi.JetTypeParameterListOwner
|
||||
import org.jetbrains.jet.plugin.refactoring.extractFunction.AnalysisResult.Status
|
||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.jet.lang.psi.codeFragmentUtil.skipVisibilityCheck
|
||||
import org.jetbrains.jet.lang.psi.codeFragmentUtil.setSkipVisibilityCheck
|
||||
|
||||
private val DEFAULT_FUNCTION_NAME = "myFun"
|
||||
private val DEFAULT_RETURN_TYPE = KotlinBuiltIns.getInstance().getUnitType()
|
||||
@@ -677,6 +679,9 @@ fun ExtractionDescriptor.generateFunction(
|
||||
val tmpFile = originalFile.createTempCopy { text ->
|
||||
StringBuilder(text).insert(position, getFunctionText() + "\n").toString()
|
||||
}
|
||||
if (originalFile.skipVisibilityCheck()) {
|
||||
tmpFile.setSkipVisibilityCheck(true)
|
||||
}
|
||||
tmpFile.findElementAt(position)?.getParentByType(javaClass<JetNamedFunction>())!!
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<caret>val a = 1
|
||||
}
|
||||
|
||||
class MyClass {
|
||||
private fun privateFun(i: Int) = 1
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
MyClass().privateFun(<error>"s"</error>)
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
<caret>val a = 1
|
||||
}
|
||||
|
||||
class MyClass {
|
||||
private fun <T> privateFun(i: T): T = i
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
"${MyClass().privateFun(1)} ${MyClass().privateFun<Int>(<error>"s"</error>)}"
|
||||
@@ -0,0 +1,12 @@
|
||||
fun foo() {
|
||||
<caret>val a = 1
|
||||
}
|
||||
|
||||
class MyClass {
|
||||
private fun privateFun() = 1
|
||||
private val privateVal = 1
|
||||
|
||||
private class PrivateClass {
|
||||
val a = 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
MyClass().privateFun() + MyClass().privateVal + MyClass.PrivateClass().a
|
||||
@@ -0,0 +1,12 @@
|
||||
fun foo() {
|
||||
<caret>val a = 1
|
||||
}
|
||||
|
||||
class MyClass {
|
||||
protected fun protectedFun(): Int = 1
|
||||
protected val protectedVal: Int = 1
|
||||
|
||||
protected class ProtectedClass {
|
||||
val a = 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
MyClass().protectedFun() + MyClass().protectedVal + MyClass.ProtectedClass().a
|
||||
@@ -0,0 +1,7 @@
|
||||
LineBreakpoint created at privateMember.kt:5
|
||||
!JDK_HOME!\bin\java -agentlib:jdwp=transport=dt_socket,address=!HOST_NAME!:!HOST_PORT!,suspend=y,server=n -Dfile.encoding=!FILE_ENCODING! -classpath !APP_PATH!\classes;!KOTLIN_RUNTIME!;!RT_JAR! privateMember.PrivateMemberPackage
|
||||
Connected to the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
|
||||
privateMember.kt:4
|
||||
Disconnected from the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
|
||||
|
||||
Process finished with exit code 0
|
||||
@@ -0,0 +1,7 @@
|
||||
LineBreakpoint created at protectedMember.kt:5
|
||||
!JDK_HOME!\bin\java -agentlib:jdwp=transport=dt_socket,address=!HOST_NAME!:!HOST_PORT!,suspend=y,server=n -Dfile.encoding=!FILE_ENCODING! -classpath !APP_PATH!\classes;!KOTLIN_RUNTIME!;!RT_JAR! protectedMember.ProtectedMemberPackage
|
||||
Connected to the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
|
||||
protectedMember.kt:4
|
||||
Disconnected from the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
|
||||
|
||||
Process finished with exit code 0
|
||||
@@ -0,0 +1,24 @@
|
||||
package privateMember
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
//Breakpoint!
|
||||
args.size
|
||||
}
|
||||
|
||||
class MyClass {
|
||||
private fun privateFun() = 1
|
||||
private val privateVal = 1
|
||||
|
||||
private class PrivateClass {
|
||||
val a = 1
|
||||
}
|
||||
}
|
||||
|
||||
// EXPRESSION: MyClass().privateFun()
|
||||
// RESULT: 1: I
|
||||
|
||||
// EXPRESSION: MyClass().privateVal
|
||||
// RESULT: 1: I
|
||||
|
||||
// EXPRESSION: MyClass.PrivateClass().a
|
||||
// RESULT: 1: I
|
||||
@@ -0,0 +1,24 @@
|
||||
package protectedMember
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
//Breakpoint!
|
||||
args.size
|
||||
}
|
||||
|
||||
class MyClass {
|
||||
protected fun protectedFun(): Int = 1
|
||||
protected val protectedVal: Int = 1
|
||||
|
||||
protected class ProtectedClass {
|
||||
val a = 1
|
||||
}
|
||||
}
|
||||
|
||||
// EXPRESSION: MyClass().protectedFun()
|
||||
// RESULT: 1: I
|
||||
|
||||
// EXPRESSION: MyClass().protectedVal
|
||||
// RESULT: 1: I
|
||||
|
||||
// EXPRESSION: MyClass.ProtectedClass().a
|
||||
// RESULT: 1: I
|
||||
+20
@@ -53,6 +53,26 @@ public class CodeFragmentHighlightingTestGenerated extends AbstractCodeFragmentH
|
||||
doTest("idea/testData/checker/codeFragments/contextElementAsStatement.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("privateFunArgumentsResolve.kt")
|
||||
public void testPrivateFunArgumentsResolve() throws Exception {
|
||||
doTest("idea/testData/checker/codeFragments/privateFunArgumentsResolve.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("privateFunTypeArguments.kt")
|
||||
public void testPrivateFunTypeArguments() throws Exception {
|
||||
doTest("idea/testData/checker/codeFragments/privateFunTypeArguments.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("privateMember.kt")
|
||||
public void testPrivateMember() throws Exception {
|
||||
doTest("idea/testData/checker/codeFragments/privateMember.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("protectedMember.kt")
|
||||
public void testProtectedMember() throws Exception {
|
||||
doTest("idea/testData/checker/codeFragments/protectedMember.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simpleNameExpression.kt")
|
||||
public void testSimpleNameExpression() throws Exception {
|
||||
doTest("idea/testData/checker/codeFragments/simpleNameExpression.kt");
|
||||
|
||||
+10
@@ -96,6 +96,16 @@ public class KotlinEvaluateExpressionTestGenerated extends AbstractKotlinEvaluat
|
||||
doTest("idea/testData/debugger/tinyApp/src/evaluate/multilineExpressionAtBreakpoint.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("privateMember.kt")
|
||||
public void testPrivateMember() throws Exception {
|
||||
doTest("idea/testData/debugger/tinyApp/src/evaluate/privateMember.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("protectedMember.kt")
|
||||
public void testProtectedMember() throws Exception {
|
||||
doTest("idea/testData/debugger/tinyApp/src/evaluate/protectedMember.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
doTest("idea/testData/debugger/tinyApp/src/evaluate/simple.kt");
|
||||
|
||||
Reference in New Issue
Block a user