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:
Alexander Udalov
2014-12-09 00:56:56 +03:00
parent 1e50078f6f
commit 17c930c84f
14 changed files with 247 additions and 116 deletions
@@ -2640,57 +2640,59 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
final ReceiverParameterDescriptor receiverParameter = descriptor.getExtensionReceiverParameter();
final Method factoryMethod;
if (receiverParameter != null) {
Type[] parameterTypes = new Type[] {JAVA_STRING_TYPE, K_PACKAGE_IMPL_TYPE, getType(Class.class)};
Type[] parameterTypes = new Type[] {JAVA_STRING_TYPE, K_PACKAGE_TYPE, getType(Class.class)};
factoryMethod = descriptor.isVar()
? method("mutableTopLevelExtensionProperty", K_MUTABLE_TOP_LEVEL_EXTENSION_PROPERTY_IMPL_TYPE, parameterTypes)
: method("topLevelExtensionProperty", K_TOP_LEVEL_EXTENSION_PROPERTY_IMPL_TYPE, parameterTypes);
? method("mutableTopLevelExtensionProperty", K_MUTABLE_TOP_LEVEL_EXTENSION_PROPERTY_TYPE, parameterTypes)
: method("topLevelExtensionProperty", K_TOP_LEVEL_EXTENSION_PROPERTY_TYPE, parameterTypes);
}
else {
Type[] parameterTypes = new Type[] {JAVA_STRING_TYPE, K_PACKAGE_IMPL_TYPE};
Type[] parameterTypes = new Type[] {JAVA_STRING_TYPE, K_PACKAGE_TYPE};
factoryMethod = descriptor.isVar()
? method("mutableTopLevelVariable", K_MUTABLE_TOP_LEVEL_VARIABLE_IMPL_TYPE, parameterTypes)
: method("topLevelVariable", K_TOP_LEVEL_VARIABLE_IMPL_TYPE, parameterTypes);
? method("mutableTopLevelVariable", K_MUTABLE_TOP_LEVEL_VARIABLE_TYPE, parameterTypes)
: method("topLevelVariable", K_TOP_LEVEL_VARIABLE_TYPE, parameterTypes);
}
return StackValue.operation(factoryMethod.getReturnType(), new Function1<InstructionAdapter, Unit>() {
@Override
public Unit invoke(InstructionAdapter v) {
v.visitLdcInsn(descriptor.getName().asString());
v.getstatic(packageClassInternalName, JvmAbi.KOTLIN_PACKAGE_FIELD_NAME, K_PACKAGE_IMPL_TYPE.getDescriptor());
v.getstatic(packageClassInternalName, JvmAbi.KOTLIN_PACKAGE_FIELD_NAME, K_PACKAGE_TYPE.getDescriptor());
if (receiverParameter != null) {
putJavaLangClassInstance(v, typeMapper.mapType(receiverParameter));
}
v.invokestatic(REFLECTION_INTERNAL_PACKAGE, factoryMethod.getName(), factoryMethod.getDescriptor(), false);
v.invokestatic(REFLECTION, factoryMethod.getName(), factoryMethod.getDescriptor(), false);
return Unit.INSTANCE$;
}
});
}
@NotNull
private StackValue generateMemberPropertyReference(@NotNull final VariableDescriptor descriptor, @NotNull final ClassDescriptor containingClass) {
final Type classAsmType = typeMapper.mapClass(containingClass);
private StackValue generateMemberPropertyReference(
@NotNull final VariableDescriptor descriptor,
@NotNull final ClassDescriptor containingClass
) {
final Method factoryMethod = descriptor.isVar()
? method("mutableMemberProperty", K_MUTABLE_MEMBER_PROPERTY_TYPE, JAVA_STRING_TYPE)
: method("memberProperty", K_MEMBER_PROPERTY_TYPE, JAVA_STRING_TYPE);
? method("mutableMemberProperty", K_MUTABLE_MEMBER_PROPERTY_TYPE, JAVA_STRING_TYPE, K_CLASS_TYPE)
: method("memberProperty", K_MEMBER_PROPERTY_TYPE, JAVA_STRING_TYPE, K_CLASS_TYPE);
return StackValue.operation(factoryMethod.getReturnType(), new Function1<InstructionAdapter, Unit>() {
@Override
public Unit invoke(InstructionAdapter v) {
v.visitLdcInsn(descriptor.getName().asString());
Type classAsmType = typeMapper.mapClass(containingClass);
if (containingClass instanceof JavaClassDescriptor) {
v.aconst(classAsmType);
v.invokestatic(REFLECTION_INTERNAL_PACKAGE, "foreignKotlinClass",
Type.getMethodDescriptor(K_CLASS_IMPL_TYPE, getType(Class.class)), false);
v.invokestatic(REFLECTION, "foreignKotlinClass", Type.getMethodDescriptor(K_CLASS_TYPE, getType(Class.class)), false);
}
else {
v.getstatic(classAsmType.getInternalName(), JvmAbi.KOTLIN_CLASS_FIELD_NAME, K_CLASS_IMPL_TYPE.getDescriptor());
v.getstatic(classAsmType.getInternalName(), JvmAbi.KOTLIN_CLASS_FIELD_NAME, K_CLASS_TYPE.getDescriptor());
}
v.visitLdcInsn(descriptor.getName().asString());
v.invokevirtual(K_CLASS_IMPL_TYPE.getInternalName(), factoryMethod.getName(), factoryMethod.getDescriptor(), false);
v.invokestatic(REFLECTION, factoryMethod.getName(), factoryMethod.getDescriptor(), false);
return Unit.INSTANCE$;
}
@@ -405,7 +405,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
return;
}
generateReflectionObjectField(state, classAsmType, v, method("kClassFromKotlin", K_CLASS_IMPL_TYPE, getType(Class.class)),
generateReflectionObjectField(state, classAsmType, v, method("createKotlinClass", K_CLASS_TYPE, getType(Class.class)),
JvmAbi.KOTLIN_CLASS_FIELD_NAME, createOrGetClInitCodegen().v);
}
@@ -447,7 +447,7 @@ public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclaratio
if (state.getClassBuilderMode() == ClassBuilderMode.LIGHT_CLASSES) return;
v.aconst(thisAsmType);
v.invokestatic(REFLECTION_INTERNAL_PACKAGE, factory.getName(), factory.getDescriptor(), false);
v.invokestatic(REFLECTION, factory.getName(), factory.getDescriptor(), false);
v.putstatic(thisAsmType.getInternalName(), fieldName, type);
}
@@ -67,7 +67,7 @@ import java.util.*;
import static org.jetbrains.kotlin.codegen.AsmUtil.asmDescByFqNameWithoutInnerClasses;
import static org.jetbrains.kotlin.codegen.AsmUtil.method;
import static org.jetbrains.kotlin.load.kotlin.PackageClassUtils.getPackageClassFqName;
import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.K_PACKAGE_IMPL_TYPE;
import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.K_PACKAGE_TYPE;
import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.getType;
import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.*;
import static org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin.NO_ORIGIN;
@@ -253,7 +253,7 @@ public class PackageCodegen {
private void generateKotlinPackageReflectionField() {
MethodVisitor mv = v.newMethod(NO_ORIGIN, ACC_STATIC, "<clinit>", "()V", null, null);
Method method = method("kPackage", K_PACKAGE_IMPL_TYPE, getType(Class.class));
Method method = method("createKotlinPackage", K_PACKAGE_TYPE, getType(Class.class));
InstructionAdapter iv = new InstructionAdapter(mv);
MemberCodegen.generateReflectionObjectField(state, packageClassType, v, method, JvmAbi.KOTLIN_PACKAGE_FIELD_NAME, iv);
iv.areturn(Type.VOID_TYPE);
@@ -102,7 +102,7 @@ public class ScriptCodegen extends MemberCodegen<JetScript> {
"java/lang/Object",
ArrayUtil.EMPTY_STRING_ARRAY);
generateReflectionObjectField(state, classType, v, method("kClassFromKotlin", K_CLASS_IMPL_TYPE, getType(Class.class)),
generateReflectionObjectField(state, classType, v, method("createKotlinClass", K_CLASS_TYPE, getType(Class.class)),
JvmAbi.KOTLIN_CLASS_FIELD_NAME, createOrGetClInitCodegen().v);
}
@@ -33,24 +33,24 @@ public class AsmTypes {
public static final Type PROPERTY_METADATA_TYPE = Type.getObjectType("kotlin/PropertyMetadata");
public static final Type PROPERTY_METADATA_IMPL_TYPE = Type.getObjectType("kotlin/PropertyMetadataImpl");
public static final Type K_MEMBER_PROPERTY_TYPE = Type.getObjectType("kotlin/reflect/KMemberProperty");
public static final Type K_MUTABLE_MEMBER_PROPERTY_TYPE = Type.getObjectType("kotlin/reflect/KMutableMemberProperty");
public static final Type K_CLASS_TYPE = reflect("KClass");
public static final Type K_PACKAGE_TYPE = reflect("KPackage");
public static final Type K_CLASS_IMPL_TYPE = reflectInternal("KClassImpl");
public static final Type K_PACKAGE_IMPL_TYPE = reflectInternal("KPackageImpl");
public static final Type K_TOP_LEVEL_VARIABLE_IMPL_TYPE = reflectInternal("KTopLevelVariableImpl");
public static final Type K_MUTABLE_TOP_LEVEL_VARIABLE_IMPL_TYPE = reflectInternal("KMutableTopLevelVariableImpl");
public static final Type K_TOP_LEVEL_EXTENSION_PROPERTY_IMPL_TYPE = reflectInternal("KTopLevelExtensionPropertyImpl");
public static final Type K_MUTABLE_TOP_LEVEL_EXTENSION_PROPERTY_IMPL_TYPE = reflectInternal("KMutableTopLevelExtensionPropertyImpl");
public static final Type K_MEMBER_PROPERTY_TYPE = reflect("KMemberProperty");
public static final Type K_MUTABLE_MEMBER_PROPERTY_TYPE = reflect("KMutableMemberProperty");
public static final Type K_TOP_LEVEL_VARIABLE_TYPE = reflect("KTopLevelVariable");
public static final Type K_MUTABLE_TOP_LEVEL_VARIABLE_TYPE = reflect("KMutableTopLevelVariable");
public static final Type K_TOP_LEVEL_EXTENSION_PROPERTY_TYPE = reflect("KTopLevelExtensionProperty");
public static final Type K_MUTABLE_TOP_LEVEL_EXTENSION_PROPERTY_TYPE = reflect("KMutableTopLevelExtensionProperty");
public static final String REFLECTION_INTERNAL_PACKAGE = reflectInternal("InternalPackage").getInternalName();
public static final String REFLECTION = "kotlin/jvm/internal/Reflection";
public static final String REF_TYPE_PREFIX = "kotlin/jvm/internal/Ref$";
public static final Type OBJECT_REF_TYPE = Type.getObjectType(REF_TYPE_PREFIX + "ObjectRef");
@NotNull
private static Type reflectInternal(@NotNull String className) {
return Type.getObjectType("kotlin/reflect/jvm/internal/" + className);
private static Type reflect(@NotNull String className) {
return Type.getObjectType("kotlin/reflect/" + className);
}
@NotNull
@@ -0,0 +1,68 @@
/*
* 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.reflect.jvm.internal;
import kotlin.jvm.internal.ReflectionFactory;
import kotlin.reflect.*;
@SuppressWarnings({"UnusedDeclaration", "unchecked"})
public class ReflectionFactoryImpl extends ReflectionFactory {
@Override
public KClass createKotlinClass(Class javaClass) {
return new KClassImpl(javaClass, true);
}
@Override
public KPackage createKotlinPackage(Class javaClass) {
return new KPackageImpl(javaClass);
}
@Override
public KClass foreignKotlinClass(Class javaClass) {
return InternalPackage.foreignKotlinClass(javaClass);
}
@Override
public KMemberProperty memberProperty(String name, KClass owner) {
return ((KClassImpl) owner).memberProperty(name);
}
@Override
public KMutableMemberProperty mutableMemberProperty(String name, KClass owner) {
return ((KClassImpl) owner).mutableMemberProperty(name);
}
@Override
public KTopLevelVariable topLevelVariable(String name, KPackage owner) {
return new KTopLevelVariableImpl(name, ((KPackageImpl) owner));
}
@Override
public KMutableTopLevelVariable mutableTopLevelVariable(String name, KPackage owner) {
return new KMutableTopLevelVariableImpl(name, (KPackageImpl) owner);
}
@Override
public KTopLevelExtensionProperty topLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return new KTopLevelExtensionPropertyImpl(name, (KPackageImpl) owner, receiver);
}
@Override
public KMutableTopLevelExtensionProperty mutableTopLevelExtensionProperty(String name, KPackage owner, Class receiver) {
return new KMutableTopLevelExtensionPropertyImpl(name, (KPackageImpl) owner, receiver);
}
}
@@ -18,12 +18,11 @@ package kotlin.reflect.jvm.internal
import kotlin.reflect.*
// TODO: drop these functions, use ReflectionFactory instead
fun <T> kClass(jClass: Class<T>): KClassImpl<T> =
KClassImpl<T>(jClass, false)
fun <T> kClassFromKotlin(jClass: Class<T>): KClassImpl<T> =
KClassImpl<T>(jClass, true)
fun kPackage(jClass: Class<*>): KPackageImpl =
KPackageImpl(jClass)
@@ -1,33 +0,0 @@
/*
* 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.reflect.jvm.internal;
import kotlin.reflect.jvm.KotlinReflectionNotSupportedError;
public class InternalPackage {
public static KClassImpl foreignKotlinClass(Class jClass) {
throw new KotlinReflectionNotSupportedError();
}
public static KClassImpl kClassFromKotlin(Class jClass) {
return null;
}
public static KPackageImpl kPackage(Class c) {
return null;
}
}
@@ -1,22 +0,0 @@
/*
* 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.reflect.jvm.internal;
import kotlin.reflect.KClass;
public abstract class KClassImpl implements KClass {
}
@@ -1,22 +0,0 @@
/*
* 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.reflect.jvm.internal;
import kotlin.reflect.KPackage;
public abstract class KPackageImpl implements KPackage {
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package kotlin.reflect.jvm;
package kotlin.jvm;
public class KotlinReflectionNotSupportedError extends Error {
public KotlinReflectionNotSupportedError() {
@@ -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();
}
}