From ffafe89d8cb367cd2a7b9bfb5ce3bafd33fadcc1 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 16 May 2017 14:23:33 +0300 Subject: [PATCH] Do not try to load Java classes from incorrect packages #KT-11474 Fixed (cherry picked from commit bfb3b38ebce3acc5dbd9c42912dfe8f8ca5e5b1b) --- compiler/testData/cli/jvm/javaSrcWrongPackage.args | 4 ++++ compiler/testData/cli/jvm/javaSrcWrongPackage.kt | 1 + compiler/testData/cli/jvm/javaSrcWrongPackage.out | 7 +++++++ compiler/testData/cli/jvm/javaSrcWrongPackage/A.java | 9 +++++++++ .../org/jetbrains/kotlin/cli/CliTestGenerated.java | 6 ++++++ .../java/lazy/descriptors/LazyJavaPackageScope.kt | 12 +++++------- 6 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/cli/jvm/javaSrcWrongPackage.args create mode 100644 compiler/testData/cli/jvm/javaSrcWrongPackage.kt create mode 100644 compiler/testData/cli/jvm/javaSrcWrongPackage.out create mode 100644 compiler/testData/cli/jvm/javaSrcWrongPackage/A.java diff --git a/compiler/testData/cli/jvm/javaSrcWrongPackage.args b/compiler/testData/cli/jvm/javaSrcWrongPackage.args new file mode 100644 index 00000000000..0939a00c046 --- /dev/null +++ b/compiler/testData/cli/jvm/javaSrcWrongPackage.args @@ -0,0 +1,4 @@ +$TESTDATA_DIR$/javaSrcWrongPackage.kt +$TESTDATA_DIR$/javaSrcWrongPackage +-d +$TEMP_DIR$ diff --git a/compiler/testData/cli/jvm/javaSrcWrongPackage.kt b/compiler/testData/cli/jvm/javaSrcWrongPackage.kt new file mode 100644 index 00000000000..e8d50074020 --- /dev/null +++ b/compiler/testData/cli/jvm/javaSrcWrongPackage.kt @@ -0,0 +1 @@ +fun test(): A.Nested = A().nested() diff --git a/compiler/testData/cli/jvm/javaSrcWrongPackage.out b/compiler/testData/cli/jvm/javaSrcWrongPackage.out new file mode 100644 index 00000000000..4964af100c6 --- /dev/null +++ b/compiler/testData/cli/jvm/javaSrcWrongPackage.out @@ -0,0 +1,7 @@ +compiler/testData/cli/jvm/javaSrcWrongPackage.kt:1:13: error: unresolved reference: A +fun test(): A.Nested = A().nested() + ^ +compiler/testData/cli/jvm/javaSrcWrongPackage.kt:1:24: error: unresolved reference: A +fun test(): A.Nested = A().nested() + ^ +COMPILATION_ERROR \ No newline at end of file diff --git a/compiler/testData/cli/jvm/javaSrcWrongPackage/A.java b/compiler/testData/cli/jvm/javaSrcWrongPackage/A.java new file mode 100644 index 00000000000..d2dcf519f51 --- /dev/null +++ b/compiler/testData/cli/jvm/javaSrcWrongPackage/A.java @@ -0,0 +1,9 @@ +package foo; + +public class A { + public Nested nested() { + return new Nested(); + } + + public static class Nested {} +} diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 8aea27a3fcd..3466e261314 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -218,6 +218,12 @@ public class CliTestGenerated extends AbstractCliTest { doJvmTest(fileName); } + @TestMetadata("javaSrcWrongPackage.args") + public void testJavaSrcWrongPackage() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/javaSrcWrongPackage.args"); + doJvmTest(fileName); + } + @TestMetadata("jvm8Target.args") public void testJvm8Target() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/jvm8Target.args"); diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaPackageScope.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaPackageScope.kt index 17a9d2201fe..399011d4fce 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaPackageScope.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaPackageScope.kt @@ -82,13 +82,11 @@ class LazyJavaPackageScope( ) } - - val javaClassFqName = javaClass?.fqName ?: return@classByRequest null - assert(!javaClassFqName.isRoot && javaClassFqName.parent() == ownerDescriptor.fqName) { - "Java class by request $requestClassId should be contained in package ${ownerDescriptor.fqName}, but it's fq-name: $javaClassFqName" - } - - LazyJavaClassDescriptor(c, ownerDescriptor, javaClass) + val actualFqName = javaClass?.fqName + if (actualFqName == null || actualFqName.isRoot || actualFqName.parent() != ownerDescriptor.fqName) + null + else + LazyJavaClassDescriptor(c, ownerDescriptor, javaClass) } } }