Evaluate expression: allow to call invisible members

#KT-4935 Fixed
This commit is contained in:
Natalia Ukhorskaya
2014-05-06 17:21:04 +04:00
parent 5237674139
commit a0549f0ff5
22 changed files with 194 additions and 12 deletions
@@ -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
@@ -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");
@@ -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");