From 628bb774fd404a307499575fd30c7325e6660d18 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 29 Jul 2015 23:20:35 +0300 Subject: [PATCH] Support class reference expressions without kotlin-reflect.jar in classpath Currently not much you can do with them, but in the future '.java' extension property will allow to get the Class instance from it. Also introduce codegen tests with stdlib but without reflection in the classpath. Based on the work by @dnpetrov --- .../noKotlinReflect/simpleClassLiterals.kt | 14 +++++++ .../AbstractBlackBoxCodegenTest.java | 9 ++++- .../kotlin/test/ConfigurationKind.kt | 28 ++++++++++++++ .../jetbrains/kotlin/test/JetTestUtils.java | 9 +++-- .../jvm/internal/KCallableContainerImpl.kt | 6 +-- .../src/kotlin/jvm/internal/ClassReference.kt | 37 +++++++++++++++++++ .../jvm/internal/DeclarationContainerImpl.kt | 10 ++--- .../jvm/internal/ReflectionFactory.java | 9 +---- 8 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/simpleClassLiterals.kt create mode 100644 compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.kt create mode 100644 core/runtime.jvm/src/kotlin/jvm/internal/ClassReference.kt rename compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.java => core/runtime.jvm/src/kotlin/jvm/internal/DeclarationContainerImpl.kt (66%) diff --git a/compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/simpleClassLiterals.kt b/compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/simpleClassLiterals.kt new file mode 100644 index 00000000000..48a845d11bd --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/reflection/noKotlinReflect/simpleClassLiterals.kt @@ -0,0 +1,14 @@ +// NO_KOTLIN_REFLECT + +import kotlin.test.assertNotNull + +class Klass + +fun box(): String { + assertNotNull(Int::class) + assertNotNull(String::class) + assertNotNull(Klass::class) + assertNotNull(Error::class) + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java index e17efabab17..fe3bef52010 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/AbstractBlackBoxCodegenTest.java @@ -20,7 +20,6 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.util.ArrayUtil; import com.intellij.util.Processor; import kotlin.Charsets; -import kotlin.KotlinPackage; import kotlin.io.IoPackage; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.cli.common.output.outputUtils.OutputUtilsPackage; @@ -68,11 +67,17 @@ public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase { public void doTestWithStdlib(@NotNull String filename) { myEnvironment = JetTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea( - getTestRootDisposable(), ConfigurationKind.ALL, getTestJdkKind(filename) + getTestRootDisposable(), getTestConfigurationKind(filename), getTestJdkKind(filename) ); blackBoxFileByFullPath(filename); } + private static ConfigurationKind getTestConfigurationKind(String filename) { + return InTextDirectivesUtils.isDirectiveDefined( + IoPackage.readText(new File(filename), Charsets.UTF_8), "NO_KOTLIN_REFLECT" + ) ? ConfigurationKind.NO_KOTLIN_REFLECT : ConfigurationKind.ALL; + } + public void doTestMultiFile(@NotNull String folderName) { final List files = new ArrayList(2); FileUtil.processFilesRecursively(new File(folderName), new Processor() { diff --git a/compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.kt b/compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.kt new file mode 100644 index 00000000000..bcb9d2f83e2 --- /dev/null +++ b/compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2015 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.kotlin.test + +public enum class ConfigurationKind( + public val withJdkAnnotations: Boolean, + public val withRuntime: Boolean, + public val withReflection: Boolean +) { + JDK_ONLY(withJdkAnnotations = false, withRuntime = false, withReflection = false), + JDK_AND_ANNOTATIONS(withJdkAnnotations = true, withRuntime = false, withReflection = false), + NO_KOTLIN_REFLECT(withJdkAnnotations = true, withRuntime = true, withReflection = false), + ALL(withJdkAnnotations = true, withRuntime = true, withReflection = true), +} diff --git a/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java b/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java index f32fed6bda9..203e0487449 100644 --- a/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/kotlin/test/JetTestUtils.java @@ -94,7 +94,6 @@ import static org.jetbrains.kotlin.cli.jvm.config.JVMConfigurationKeys.ANNOTATIO import static org.jetbrains.kotlin.jvm.compiler.LoadDescriptorUtil.compileKotlinToDirAndGetAnalysisResult; import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; import static org.jetbrains.kotlin.test.ConfigurationKind.ALL; -import static org.jetbrains.kotlin.test.ConfigurationKind.JDK_AND_ANNOTATIONS; public class JetTestUtils { public static final String TEST_GENERATOR_NAME = "org.jetbrains.kotlin.generators.tests.TestsPackage"; @@ -459,13 +458,17 @@ public class JetTestUtils { else { addJvmClasspathRoots(configuration, PathUtil.getJdkClassesRoots()); } - if (configurationKind == ALL) { + + if (configurationKind.getWithRuntime()) { addJvmClasspathRoot(configuration, ForTestCompileRuntime.runtimeJarForTests()); + } + if (configurationKind.getWithReflection()) { addJvmClasspathRoot(configuration, ForTestCompileRuntime.reflectJarForTests()); } + addJvmClasspathRoots(configuration, classpath); - if (configurationKind == ALL || configurationKind == JDK_AND_ANNOTATIONS) { + if (configurationKind.getWithJdkAnnotations()) { if (jdkKind == TestJdkKind.ANDROID_API) { configuration.add(ANNOTATIONS_PATH_KEY, getAndroidSdkAnnotationsJar()); } diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt index 628b09a9623..e09968efeb6 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableContainerImpl.kt @@ -31,18 +31,16 @@ import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf.JvmType.PrimitiveType. import java.lang.reflect.Constructor import java.lang.reflect.Field import java.lang.reflect.Method +import kotlin.jvm.internal.DeclarationContainerImpl import kotlin.reflect.KCallable -import kotlin.reflect.KDeclarationContainer import kotlin.reflect.KotlinReflectionInternalError -abstract class KCallableContainerImpl : KDeclarationContainer { +abstract class KCallableContainerImpl : DeclarationContainerImpl { // Note: this is stored here on a soft reference to prevent GC from destroying the weak reference to it in the moduleByClassLoader cache val moduleData by ReflectProperties.lazySoft { jClass.getOrCreateModule() } - abstract val jClass: Class<*> - abstract val scope: JetScope abstract val constructorDescriptors: Collection diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/ClassReference.kt b/core/runtime.jvm/src/kotlin/jvm/internal/ClassReference.kt new file mode 100644 index 00000000000..93c19b27d63 --- /dev/null +++ b/core/runtime.jvm/src/kotlin/jvm/internal/ClassReference.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2015 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 kotlin.jvm.internal + +import kotlin.reflect.KCallable +import kotlin.reflect.KClass +import kotlin.reflect.KFunction + +public class ClassReference(override val jClass: Class<*>) : KClass, DeclarationContainerImpl { + override val simpleName: String? + get() = error() + + override val qualifiedName: String? + get() = error() + + override val members: Collection> + get() = error() + + override val constructors: Collection> + get() = error() + + private fun error(): Nothing = throw KotlinReflectionNotSupportedError() +} diff --git a/compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.java b/core/runtime.jvm/src/kotlin/jvm/internal/DeclarationContainerImpl.kt similarity index 66% rename from compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.java rename to core/runtime.jvm/src/kotlin/jvm/internal/DeclarationContainerImpl.kt index 1c55cc415b1..ef4041ec53f 100644 --- a/compiler/tests/org/jetbrains/kotlin/test/ConfigurationKind.java +++ b/core/runtime.jvm/src/kotlin/jvm/internal/DeclarationContainerImpl.kt @@ -14,10 +14,10 @@ * limitations under the License. */ -package org.jetbrains.kotlin.test; +package kotlin.jvm.internal -public enum ConfigurationKind { - JDK_ONLY, // Java runtime classes - JDK_AND_ANNOTATIONS, // Java runtime classes with Kotlin's external annotations - ALL, // Java runtime classes with Kotlin's external annotations and Kotlin stdlib +import kotlin.reflect.KDeclarationContainer + +public interface DeclarationContainerImpl : KDeclarationContainer { + public val jClass: Class<*> } diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/ReflectionFactory.java b/core/runtime.jvm/src/kotlin/jvm/internal/ReflectionFactory.java index 05adf0b59b7..e6a2cc273b8 100644 --- a/core/runtime.jvm/src/kotlin/jvm/internal/ReflectionFactory.java +++ b/core/runtime.jvm/src/kotlin/jvm/internal/ReflectionFactory.java @@ -16,12 +16,11 @@ package kotlin.jvm.internal; -import kotlin.jvm.KotlinReflectionNotSupportedError; import kotlin.reflect.*; public class ReflectionFactory { public KClass createKotlinClass(Class javaClass) { - return null; + return new ClassReference(javaClass); } public KPackage createKotlinPackage(Class javaClass) { @@ -29,7 +28,7 @@ public class ReflectionFactory { } public KClass foreignKotlinClass(Class javaClass) { - throw error(); + return new ClassReference(javaClass); } // Functions @@ -63,8 +62,4 @@ public class ReflectionFactory { public KMutableProperty2 mutableProperty2(MutablePropertyReference2 p) { return p; } - - private Error error() { - throw new KotlinReflectionNotSupportedError(); - } }