diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/AnnotationDescriptorLoader.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/AnnotationDescriptorLoader.kt index 0484913c0e5..fc5ca27d493 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/AnnotationDescriptorLoader.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/AnnotationDescriptorLoader.kt @@ -40,7 +40,12 @@ import java.util.* import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.javaClassIdToKotlinClassId -public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, storage: DescriptorLoadersStorage, kotlinClassFinder: KotlinClassFinder, errorReporter: ErrorReporter) : BaseDescriptorLoader(kotlinClassFinder, errorReporter, storage), AnnotationLoader { +public class AnnotationDescriptorLoader( + private val module: ModuleDescriptor, + storage: DescriptorLoadersStorage, + kotlinClassFinder: KotlinClassFinder, + errorReporter: ErrorReporter +) : BaseDescriptorLoader(kotlinClassFinder, errorReporter, storage), AnnotationLoader { override fun loadClassAnnotations(classProto: ProtoBuf.Class, nameResolver: NameResolver): List { val classId = nameResolver.getClassId(classProto.getFqName()) @@ -48,7 +53,7 @@ public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, st if (kotlinClass == null) { // This means that the resource we're constructing the descriptor from is no longer present: KotlinClassFinder had found the // class earlier, but it can't now - errorReporter.reportLoadingError("Kotlin class for loading class annotations is not found: " + classId.asSingleFqName(), null) + errorReporter.reportLoadingError("Kotlin class for loading class annotations is not found: ${classId.asSingleFqName()}", null) return listOf() } @@ -66,29 +71,43 @@ public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, st return result } - override fun loadCallableAnnotations(container: ProtoContainer, proto: ProtoBuf.Callable, nameResolver: NameResolver, kind: AnnotatedCallableKind): List { - val signature = getCallableSignature(proto, nameResolver, kind) - if (signature == null) return listOf() - + override fun loadCallableAnnotations( + container: ProtoContainer, + proto: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind + ): List { + val signature = getCallableSignature(proto, nameResolver, kind) ?: return listOf() return findClassAndLoadMemberAnnotations(container, proto, nameResolver, kind, signature) } - private fun findClassAndLoadMemberAnnotations(container: ProtoContainer, proto: ProtoBuf.Callable, nameResolver: NameResolver, kind: AnnotatedCallableKind, signature: MemberSignature): List { + private fun findClassAndLoadMemberAnnotations( + container: ProtoContainer, + proto: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind, + signature: MemberSignature + ): List { val kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind) if (kotlinClass == null) { - errorReporter.reportLoadingError("Kotlin class for loading member annotations is not found: " + container, null) + errorReporter.reportLoadingError("Kotlin class for loading member annotations is not found: $container", null) return listOf() } - val descriptors = storage.getStorageForClass(kotlinClass).memberAnnotations.get(signature) - return if (descriptors == null) listOf() else descriptors + return storage.getStorageForClass(kotlinClass).memberAnnotations[signature] ?: listOf() } - override fun loadValueParameterAnnotations(container: ProtoContainer, callable: ProtoBuf.Callable, nameResolver: NameResolver, kind: AnnotatedCallableKind, proto: ProtoBuf.Callable.ValueParameter): List { + override fun loadValueParameterAnnotations( + container: ProtoContainer, + callable: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind, + proto: ProtoBuf.Callable.ValueParameter + ): List { val methodSignature = getCallableSignature(callable, nameResolver, kind) if (methodSignature != null) { - if (proto.hasExtension(JavaProtoBuf.index)) { - val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, proto.getExtension(JavaProtoBuf.index)) + if (proto.hasExtension(JavaProtoBuf.index)) { + val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, proto.getExtension(JavaProtoBuf.index)) return findClassAndLoadMemberAnnotations(container, callable, nameResolver, kind, paramSignature) } } @@ -98,7 +117,11 @@ public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, st class object { - public fun resolveAnnotation(classId: ClassId, result: MutableList, moduleDescriptor: ModuleDescriptor): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { + public fun resolveAnnotation( + classId: ClassId, + result: MutableList, + moduleDescriptor: ModuleDescriptor + ): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { if (JvmAnnotationNames.isSpecialAnnotation(classId, true)) return null val annotationClass = resolveClass(classId, moduleDescriptor) @@ -132,7 +155,7 @@ public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, st val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass) if (parameter != null) { elements.trimToSize() - arguments.put(parameter, ArrayValue(elements, parameter.getType(), true, false)) + arguments[parameter] = ArrayValue(elements, parameter.getType(), true, false) } } } @@ -143,10 +166,10 @@ public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, st if (enumClass.getKind() == ClassKind.ENUM_CLASS) { val classifier = enumClass.getUnsubstitutedInnerClassesScope().getClassifier(name) if (classifier is ClassDescriptor) { - return EnumValue(classifier as ClassDescriptor, false) + return EnumValue(classifier, false) } } - return ErrorValue.create("Unresolved enum entry: " + enumClassId + "." + name) + return ErrorValue.create("Unresolved enum entry: $enumClassId.$name") } override fun visitEnd() { @@ -154,14 +177,14 @@ public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, st } private fun createConstant(name: Name?, value: Any?): CompileTimeConstant<*> { - val argument = createCompileTimeConstant(value, true, false, false, null) - return if (argument != null) argument else ErrorValue.create("Unsupported annotation argument: " + name) + return createCompileTimeConstant(value, true, false, false, null) + ?: ErrorValue.create("Unsupported annotation argument: $name") } private fun setArgumentValueByName(name: Name, argumentValue: CompileTimeConstant<*>) { val parameter = DescriptorResolverUtils.getAnnotationParameterByName(name, annotationClass) if (parameter != null) { - arguments.put(parameter, argumentValue) + arguments[parameter] = argumentValue } } } @@ -169,8 +192,8 @@ public class AnnotationDescriptorLoader(private val module: ModuleDescriptor, st private fun resolveClass(javaClassId: ClassId, moduleDescriptor: ModuleDescriptor): ClassDescriptor { val classId = javaClassIdToKotlinClassId(javaClassId) - val classDescriptor = moduleDescriptor.findClassAcrossModuleDependencies(classId) - return if (classDescriptor != null) classDescriptor else ErrorUtils.createErrorClass(classId.asSingleFqName().asString()) + return moduleDescriptor.findClassAcrossModuleDependencies(classId) + ?: ErrorUtils.createErrorClass(classId.asSingleFqName().asString()) } } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/ConstantDescriptorLoader.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/ConstantDescriptorLoader.kt index 89022b947ac..675943e5015 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/ConstantDescriptorLoader.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/ConstantDescriptorLoader.kt @@ -26,18 +26,26 @@ import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter import org.jetbrains.jet.lang.resolve.kotlin.DescriptorLoadersStorage.MemberSignature -public class ConstantDescriptorLoader(storage: DescriptorLoadersStorage, kotlinClassFinder: KotlinClassFinder, errorReporter: ErrorReporter) : BaseDescriptorLoader(kotlinClassFinder, errorReporter, storage), ConstantLoader { +public class ConstantDescriptorLoader( + storage: DescriptorLoadersStorage, + kotlinClassFinder: KotlinClassFinder, + errorReporter: ErrorReporter +) : BaseDescriptorLoader(kotlinClassFinder, errorReporter, storage), ConstantLoader { - override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Callable, nameResolver: NameResolver, kind: AnnotatedCallableKind): CompileTimeConstant<*>? { - val signature = getCallableSignature(proto, nameResolver, kind) - if (signature == null) return null + override fun loadPropertyConstant( + container: ProtoContainer, + proto: ProtoBuf.Callable, + nameResolver: NameResolver, + kind: AnnotatedCallableKind + ): CompileTimeConstant<*>? { + val signature = getCallableSignature(proto, nameResolver, kind) ?: return null val kotlinClass = findClassWithAnnotationsAndInitializers(container, proto, nameResolver, kind) if (kotlinClass == null) { - errorReporter.reportLoadingError("Kotlin class for loading property constant is not found: " + container, null) + errorReporter.reportLoadingError("Kotlin class for loading property constant is not found: $container", null) return null } - return storage.getStorageForClass(kotlinClass).propertyConstants.get(signature) + return storage.getStorageForClass(kotlinClass).propertyConstants[signature] } } diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DescriptorLoadersStorage.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DescriptorLoadersStorage.kt index c943b0fb932..dbcfda80ba3 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DescriptorLoadersStorage.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/DescriptorLoadersStorage.kt @@ -21,26 +21,18 @@ import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor import org.jetbrains.jet.lang.resolve.constants.* import org.jetbrains.jet.lang.resolve.name.ClassId import org.jetbrains.jet.lang.resolve.name.Name -import org.jetbrains.jet.storage.MemoizedFunctionToNotNull import org.jetbrains.jet.storage.StorageManager import java.util.* import kotlin.platform.platformStatic public class DescriptorLoadersStorage(storageManager: StorageManager, private val module: ModuleDescriptor) { - private val storage: MemoizedFunctionToNotNull - - { - this.storage = storageManager.createMemoizedFunction(object : Function1 { - override fun invoke(kotlinClass: KotlinJvmBinaryClass): Storage { - return loadAnnotationsAndInitializers(kotlinClass) - } - }) + private val storage = storageManager.createMemoizedFunction{ + kotlinClass -> + loadAnnotationsAndInitializers(kotlinClass) } - public fun getStorageForClass(kotlinClass: KotlinJvmBinaryClass): Storage { - return storage.invoke(kotlinClass) - } + public fun getStorageForClass(kotlinClass: KotlinJvmBinaryClass): Storage = storage(kotlinClass) private fun loadAnnotationsAndInitializers(kotlinClass: KotlinJvmBinaryClass): Storage { val memberAnnotations = HashMap>() @@ -56,7 +48,7 @@ public class DescriptorLoadersStorage(storageManager: StorageManager, private va if (initializer != null) { val normalizedValue: Any - if ("ZBCS".contains(desc)) { + if (desc in "ZBCS") { val intValue = initializer as Int if ("Z" == desc) { normalizedValue = intValue != 0 @@ -78,7 +70,10 @@ public class DescriptorLoadersStorage(storageManager: StorageManager, private va normalizedValue = initializer } - propertyConstants.put(signature, createCompileTimeConstant(normalizedValue, /* canBeUsedInAnnotation */ true, /* isPureIntConstant */ true, /* usesVariableAsConstant */ true, /* expectedType */ null)) + propertyConstants[signature] = createCompileTimeConstant( + normalizedValue, canBeUsedInAnnotation = true, isPureIntConstant = true, + usesVariableAsConstant = true, expectedType = null + ) } return MemberAnnotationVisitor(signature) } @@ -87,10 +82,10 @@ public class DescriptorLoadersStorage(storageManager: StorageManager, private va override fun visitParameterAnnotation(index: Int, classId: ClassId): KotlinJvmBinaryClass.AnnotationArgumentVisitor? { val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(signature, index) - var result = memberAnnotations.get(paramSignature) + var result = memberAnnotations[paramSignature] if (result == null) { result = ArrayList() - memberAnnotations.put(paramSignature, result) + memberAnnotations[paramSignature] = result } return AnnotationDescriptorLoader.resolveAnnotation(classId, result, module) } @@ -104,8 +99,8 @@ public class DescriptorLoadersStorage(storageManager: StorageManager, private va } override fun visitEnd() { - if (!result.isEmpty()) { - memberAnnotations.put(signature, result) + if (result.isNotEmpty()) { + memberAnnotations[signature] = result } } } @@ -116,22 +111,13 @@ public class DescriptorLoadersStorage(storageManager: StorageManager, private va // The purpose of this class is to hold a unique signature of either a method or a field, so that annotations on a member can be put // into a map indexed by these signatures - public class MemberSignature private(private val signature: String) { - - override fun hashCode(): Int { - return signature.hashCode() - } - - override fun equals(o: Any?): Boolean { - return o is MemberSignature && signature == (o as MemberSignature).signature - } + public data class MemberSignature private(private val signature: String) { override fun toString(): String { return signature } class object { - platformStatic public fun fromMethodNameAndDesc(nameAndDesc: String): MemberSignature { return MemberSignature(nameAndDesc) } @@ -148,10 +134,10 @@ public class DescriptorLoadersStorage(storageManager: StorageManager, private va class Storage( public val memberAnnotations: Map>, - public val propertyConstants: Map>) { + public val propertyConstants: Map> + ) { class object { - - public val EMPTY: Storage = Storage(mapOf>(), mapOf>()) + public val EMPTY: Storage = Storage(mapOf(), mapOf()) } } }