Minor, make CompanionObjectMapping a singleton

This commit is contained in:
Alexander Udalov
2016-08-08 18:35:54 +03:00
parent 85a951db52
commit 611899e9d3
5 changed files with 11 additions and 24 deletions
@@ -27,24 +27,20 @@ import org.jetbrains.org.objectweb.asm.Type;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isNonCompanionObject;
public class FieldInfo {
private static final CompanionObjectMapping COMPANION_OBJECT_MAPPING = new CompanionObjectMapping();
@NotNull
public static FieldInfo createForSingleton(@NotNull ClassDescriptor classDescriptor, @NotNull KotlinTypeMapper typeMapper) {
if (!classDescriptor.getKind().isSingleton() || DescriptorUtils.isEnumEntry(classDescriptor)) {
throw new UnsupportedOperationException("Can't create singleton field for class: " + classDescriptor);
}
if (isNonCompanionObject(classDescriptor) || COMPANION_OBJECT_MAPPING.hasMappingToObject(classDescriptor)) {
if (isNonCompanionObject(classDescriptor) || CompanionObjectMapping.INSTANCE.hasMappingToObject(classDescriptor)) {
return createSingletonViaInstance(classDescriptor, typeMapper);
}
else {
ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
Type ownerType = typeMapper.mapType(ownerDescriptor);
return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true);
}
ClassDescriptor ownerDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class);
assert ownerDescriptor != null : "Owner not found for class: " + classDescriptor;
Type ownerType = typeMapper.mapType(ownerDescriptor);
return new FieldInfo(ownerType, typeMapper.mapType(classDescriptor), classDescriptor.getName().asString(), true);
}
@NotNull
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.load.java;
import kotlin.text.Regex;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.builtins.CompanionObjectMapping;
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
@@ -25,7 +26,6 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
import org.jetbrains.kotlin.name.ClassId;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap;
import org.jetbrains.kotlin.util.capitalizeDecapitalize.CapitalizeDecapitalizeKt;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isClassOrEnumClass;
@@ -103,7 +103,6 @@ public final class JvmAbi {
public static boolean isCompanionObjectWithBackingFieldsInOuter(@NotNull DeclarationDescriptor companionObject) {
return isCompanionObject(companionObject) &&
isClassOrEnumClass(companionObject.getContainingDeclaration()) &&
!JavaToKotlinClassMap.INSTANCE.isMappedCompanion((ClassDescriptor) companionObject);
!CompanionObjectMapping.INSTANCE.hasMappingToObject((ClassDescriptor) companionObject);
}
}
@@ -43,7 +43,6 @@ public class JavaToKotlinClassMap implements PlatformToKotlinClassMap {
private final Map<FqNameUnsafe, FqName> mutableToReadOnly = new HashMap<FqNameUnsafe, FqName>();
private final Map<FqNameUnsafe, FqName> readOnlyToMutable = new HashMap<FqNameUnsafe, FqName>();
private final CompanionObjectMapping companionObjectMapping;
private JavaToKotlinClassMap() {
add(Object.class, FQ_NAMES.any);
@@ -69,8 +68,7 @@ public class JavaToKotlinClassMap implements PlatformToKotlinClassMap {
add(ClassId.topLevel(jvmType.getWrapperFqName()), KotlinBuiltIns.getPrimitiveFqName(jvmType.getPrimitiveType()));
}
companionObjectMapping = new CompanionObjectMapping();
for (FqName fqName : companionObjectMapping.allClassesWithIntrinsicCompanions()) {
for (FqName fqName : CompanionObjectMapping.INSTANCE.allClassesWithIntrinsicCompanions()) {
add(ClassId.topLevel(
new FqName("kotlin.jvm.internal." + fqName.shortName().asString() + "CompanionObject")),
fqName.child(SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT));
@@ -119,10 +117,6 @@ public class JavaToKotlinClassMap implements PlatformToKotlinClassMap {
return kotlinToJava.get(kotlinFqName);
}
public boolean isMappedCompanion(@NotNull ClassDescriptor descriptor) {
return companionObjectMapping.hasMappingToObject(descriptor);
}
private void add(
@NotNull Class<?> javaClass,
@NotNull FqName kotlinReadOnlyFqName,
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.DescriptorUtils
import java.util.*
class CompanionObjectMapping {
object CompanionObjectMapping {
private val classesFqNames = linkedSetOf<FqName>()
init {
@@ -33,10 +33,8 @@ class DefaultClassObjectIntrinsic(val fqName: FqName): ObjectIntrinsic {
}
class ObjectIntrinsics {
private val companionObjectMapping = CompanionObjectMapping()
fun getIntrinsic(classDescriptor: ClassDescriptor): ObjectIntrinsic {
if (!companionObjectMapping.hasMappingToObject(classDescriptor)) return NO_OBJECT_INTRINSIC
if (!CompanionObjectMapping.hasMappingToObject(classDescriptor)) return NO_OBJECT_INTRINSIC
val containingDeclaration = classDescriptor.containingDeclaration
val name = Name.identifier(containingDeclaration.name.asString() + "CompanionObject")