Use abstract factory for reflection objects on JVM
Introduce an abstract factory class ReflectionFactory which is responsible for creating reflection objects (KClass, KProperty, ...). The meaningful implementation is located in "reflection.jvm" where KClassImpl/KPropertyImpl/... are accessible and can be instantiated. The default implementation will be used in the lite runtime with no reflection and will return nulls / throw exceptions there. Put all functions, calls to which are generated by JVM back-end, in one place: the class named Reflection which contains only static methods. Previously these functions were scattered across different files in module "reflection.jvm". The code using reflection may now be compiled against either runtime, but reflection features will work if and only if reflection is accessible at runtime
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
public class KotlinReflectionNotSupportedError extends Error {
|
||||
public KotlinReflectionNotSupportedError() {
|
||||
super("Kotlin reflection implementation is not found at runtime. " +
|
||||
"Make sure you do have kotlin-runtime.jar and do not have kotlin-runtime-minimal.jar in the classpath");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.*;
|
||||
|
||||
/**
|
||||
* This class serves as a facade to the actual reflection implementation. JVM back-end generates calls to static methods of this class
|
||||
* on any reflection-using construct.
|
||||
*/
|
||||
public class Reflection {
|
||||
private static final ReflectionFactory factory;
|
||||
|
||||
static {
|
||||
ReflectionFactory impl;
|
||||
try {
|
||||
Class<?> implClass = Class.forName("kotlin.reflect.jvm.internal.ReflectionFactoryImpl");
|
||||
impl = (ReflectionFactory) implClass.newInstance();
|
||||
}
|
||||
catch (ClassCastException e) { impl = null; }
|
||||
catch (ClassNotFoundException e) { impl = null; }
|
||||
catch (InstantiationException e) { impl = null; }
|
||||
catch (IllegalAccessException e) { impl = null; }
|
||||
|
||||
factory = impl != null ? impl : new ReflectionFactory();
|
||||
}
|
||||
|
||||
public static KClass createKotlinClass(Class javaClass) {
|
||||
return factory.createKotlinClass(javaClass);
|
||||
}
|
||||
|
||||
public static KPackage createKotlinPackage(Class javaClass) {
|
||||
return factory.createKotlinPackage(javaClass);
|
||||
}
|
||||
|
||||
public static KClass foreignKotlinClass(Class javaClass) {
|
||||
return factory.foreignKotlinClass(javaClass);
|
||||
}
|
||||
|
||||
public static KMemberProperty memberProperty(String name, KClass owner) {
|
||||
return factory.memberProperty(name, owner);
|
||||
}
|
||||
|
||||
public static KMutableMemberProperty mutableMemberProperty(String name, KClass owner) {
|
||||
return factory.mutableMemberProperty(name, owner);
|
||||
}
|
||||
|
||||
public static KTopLevelVariable topLevelVariable(String name, KPackage owner) {
|
||||
return factory.topLevelVariable(name, owner);
|
||||
}
|
||||
|
||||
public static KMutableTopLevelVariable mutableTopLevelVariable(String name, KPackage owner) {
|
||||
return factory.mutableTopLevelVariable(name, owner);
|
||||
}
|
||||
|
||||
public static KTopLevelExtensionProperty topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
|
||||
return factory.topLevelExtensionProperty(name, owner, receiver);
|
||||
}
|
||||
|
||||
public static KMutableTopLevelExtensionProperty mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
|
||||
return factory.mutableTopLevelExtensionProperty(name, owner, receiver);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.jvm.KotlinReflectionNotSupportedError;
|
||||
import kotlin.reflect.*;
|
||||
|
||||
public class ReflectionFactory {
|
||||
public KClass createKotlinClass(Class javaClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public KPackage createKotlinPackage(Class javaClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public KClass foreignKotlinClass(Class javaClass) {
|
||||
throw error();
|
||||
}
|
||||
|
||||
public KMemberProperty memberProperty(String name, KClass owner) {
|
||||
throw error();
|
||||
}
|
||||
|
||||
public KMutableMemberProperty mutableMemberProperty(String name, KClass owner) {
|
||||
throw error();
|
||||
}
|
||||
|
||||
public KTopLevelVariable topLevelVariable(String name, KPackage owner) {
|
||||
throw error();
|
||||
}
|
||||
|
||||
public KMutableTopLevelVariable mutableTopLevelVariable(String name, KPackage owner) {
|
||||
throw error();
|
||||
}
|
||||
|
||||
public KTopLevelExtensionProperty topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
|
||||
throw error();
|
||||
}
|
||||
|
||||
public KMutableTopLevelExtensionProperty mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
|
||||
throw error();
|
||||
}
|
||||
|
||||
private Error error() {
|
||||
throw new KotlinReflectionNotSupportedError();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user