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
This commit is contained in:
Alexander Udalov
2015-07-29 23:20:35 +03:00
parent 0b30758987
commit 628bb774fd
8 changed files with 101 additions and 21 deletions
@@ -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"
}
@@ -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<String> files = new ArrayList<String>(2);
FileUtil.processFilesRecursively(new File(folderName), new Processor<File>() {
@@ -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),
}
@@ -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());
}
@@ -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<ConstructorDescriptor>
@@ -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<Any>, DeclarationContainerImpl {
override val simpleName: String?
get() = error()
override val qualifiedName: String?
get() = error()
override val members: Collection<KCallable<*>>
get() = error()
override val constructors: Collection<KFunction<Any>>
get() = error()
private fun error(): Nothing = throw KotlinReflectionNotSupportedError()
}
@@ -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<*>
}
@@ -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();
}
}