Address JDK8/JDK6 issues with computeIfAbsent
This commit is contained in:
committed by
Dmitriy Novozhilov
parent
374d287d08
commit
3c4989b672
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.types
|
||||
|
||||
import org.jetbrains.kotlin.fir.util.ConeTypeRegistry
|
||||
import org.jetbrains.kotlin.util.AttributeArrayOwner
|
||||
import org.jetbrains.kotlin.util.TypeRegistry
|
||||
import org.jetbrains.kotlin.types.model.AnnotationMarker
|
||||
@@ -36,7 +37,7 @@ abstract class ConeAttribute<T : ConeAttribute<T>> : AnnotationMarker {
|
||||
class ConeAttributes private constructor(attributes: List<ConeAttribute<*>>) : AttributeArrayOwner<ConeAttribute<*>, ConeAttribute<*>>(),
|
||||
Iterable<ConeAttribute<*>> {
|
||||
|
||||
companion object : TypeRegistry<ConeAttribute<*>, ConeAttribute<*>>() {
|
||||
companion object : ConeTypeRegistry<ConeAttribute<*>, ConeAttribute<*>>() {
|
||||
inline fun <reified T : ConeAttribute<T>> attributeAccessor(): ReadOnlyProperty<ConeAttributes, T?> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return generateNullableAccessor<ConeAttribute<*>, T>(T::class) as ReadOnlyProperty<ConeAttributes, T?>
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.util
|
||||
|
||||
import org.jetbrains.kotlin.util.TypeRegistry
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
abstract class ConeTypeRegistry<K : Any, V : Any> : TypeRegistry<K, V>() {
|
||||
override fun <T : K> ConcurrentHashMap<KClass<out K>, Int>.customComputeIfAbsent(
|
||||
kClass: KClass<T>,
|
||||
compute: (KClass<out K>) -> Int
|
||||
): Int {
|
||||
return this.computeIfAbsent(kClass, compute)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.fir
|
||||
|
||||
import org.jetbrains.kotlin.fir.types.impl.*
|
||||
import org.jetbrains.kotlin.fir.util.ConeTypeRegistry
|
||||
import org.jetbrains.kotlin.util.ArrayMapAccessor
|
||||
import org.jetbrains.kotlin.util.ComponentArrayOwner
|
||||
import org.jetbrains.kotlin.util.NullableArrayMapAccessor
|
||||
@@ -18,7 +19,7 @@ abstract class FirSession @PrivateSessionConstructor constructor(
|
||||
val sessionProvider: FirSessionProvider?,
|
||||
val kind: Kind
|
||||
) : ComponentArrayOwner<FirSessionComponent, FirSessionComponent>() {
|
||||
companion object : TypeRegistry<FirSessionComponent, FirSessionComponent>() {
|
||||
companion object : ConeTypeRegistry<FirSessionComponent, FirSessionComponent>() {
|
||||
inline fun <reified T : FirSessionComponent> sessionComponentAccessor(): ArrayMapAccessor<FirSessionComponent, FirSessionComponent, T> {
|
||||
return generateAccessor(T::class)
|
||||
}
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.declarations
|
||||
|
||||
import org.jetbrains.kotlin.fir.util.ConeTypeRegistry
|
||||
import org.jetbrains.kotlin.util.ArrayMap
|
||||
import org.jetbrains.kotlin.util.AttributeArrayOwner
|
||||
import org.jetbrains.kotlin.util.NullableArrayMapAccessor
|
||||
@@ -47,7 +48,7 @@ class FirDeclarationAttributes : AttributeArrayOwner<FirDeclarationDataKey, Any>
|
||||
* object SomeKey : FirDeclarationDataKey()
|
||||
* var FirDeclaration.someString: String? by FirDeclarationDataRegistry.data(SomeKey)
|
||||
*/
|
||||
object FirDeclarationDataRegistry : TypeRegistry<FirDeclarationDataKey, Any>() {
|
||||
object FirDeclarationDataRegistry : ConeTypeRegistry<FirDeclarationDataKey, Any>() {
|
||||
fun <K : FirDeclarationDataKey> data(key: K): DeclarationDataAccessor {
|
||||
val kClass = key::class
|
||||
return DeclarationDataAccessor(generateAnyNullableAccessor(kClass), kClass)
|
||||
|
||||
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.extensions
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
import org.jetbrains.kotlin.fir.FirSessionComponent
|
||||
import org.jetbrains.kotlin.fir.NoMutableState
|
||||
import org.jetbrains.kotlin.fir.util.ConeTypeRegistry
|
||||
import org.jetbrains.kotlin.util.ArrayMapAccessor
|
||||
import org.jetbrains.kotlin.util.ComponentArrayOwner
|
||||
import org.jetbrains.kotlin.util.TypeRegistry
|
||||
@@ -18,7 +19,7 @@ annotation class PluginServicesInitialization
|
||||
|
||||
@NoMutableState
|
||||
class FirExtensionService(val session: FirSession) : ComponentArrayOwner<FirExtension, List<FirExtension>>(), FirSessionComponent {
|
||||
companion object : TypeRegistry<FirExtension, List<FirExtension>>() {
|
||||
companion object : ConeTypeRegistry<FirExtension, List<FirExtension>>() {
|
||||
inline fun <reified P : FirExtension, V : List<P>> registeredExtensions(): ArrayMapAccessor<FirExtension, List<FirExtension>, V> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return generateAccessor(P::class, default = emptyList<P>() as V)
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.test.services
|
||||
|
||||
import org.jetbrains.kotlin.fir.util.ConeTypeRegistry
|
||||
import org.jetbrains.kotlin.util.ArrayMapAccessor
|
||||
import org.jetbrains.kotlin.util.ComponentArrayOwner
|
||||
import org.jetbrains.kotlin.util.NullableArrayMapAccessor
|
||||
@@ -28,7 +29,7 @@ class TestServices : ComponentArrayOwner<TestService, TestService>(){
|
||||
override val typeRegistry: TypeRegistry<TestService, TestService>
|
||||
get() = Companion
|
||||
|
||||
companion object : TypeRegistry<TestService, TestService>() {
|
||||
companion object : ConeTypeRegistry<TestService, TestService>() {
|
||||
inline fun <reified T : TestService> testServiceAccessor(): ArrayMapAccessor<TestService, TestService, T> {
|
||||
return generateAccessor(T::class)
|
||||
}
|
||||
|
||||
@@ -78,9 +78,14 @@ abstract class TypeRegistry<K : Any, V : Any> {
|
||||
}
|
||||
|
||||
fun <T : K> getId(kClass: KClass<T>): Int {
|
||||
return idPerType.computeIfAbsent(kClass) { idCounter.getAndIncrement() }
|
||||
return idPerType.customComputeIfAbsent(kClass) { idCounter.getAndIncrement() }
|
||||
}
|
||||
|
||||
abstract fun <T : K> ConcurrentHashMap<KClass<out K>, Int>.customComputeIfAbsent(
|
||||
kClass: KClass<T>,
|
||||
compute: (KClass<out K>) -> Int
|
||||
): Int
|
||||
|
||||
fun allValuesThreadUnsafeForRendering(): Map<KClass<out K>, Int> {
|
||||
return idPerType
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.types.model.AnnotationMarker
|
||||
import org.jetbrains.kotlin.util.AttributeArrayOwner
|
||||
import org.jetbrains.kotlin.util.TypeRegistry
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@@ -41,6 +42,15 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
|
||||
return generateNullableAccessor<TypeAttribute<*>, T>(T::class) as ReadOnlyProperty<TypeAttributes, T?>
|
||||
}
|
||||
|
||||
override fun <T : TypeAttribute<*>> ConcurrentHashMap<KClass<out TypeAttribute<*>>, Int>.customComputeIfAbsent(
|
||||
kClass: KClass<T>,
|
||||
compute: (KClass<out TypeAttribute<*>>) -> Int
|
||||
): Int {
|
||||
return this[kClass] ?: synchronized(this) {
|
||||
this[kClass] ?: compute(kClass).also { this.putIfAbsent(kClass, it) }
|
||||
}
|
||||
}
|
||||
|
||||
val Empty: TypeAttributes = TypeAttributes(listOf(AnnotationsTypeAttribute(Annotations.EMPTY)))
|
||||
|
||||
fun create(attributes: List<TypeAttribute<*>>): TypeAttributes {
|
||||
|
||||
Reference in New Issue
Block a user