Skip to content

Commit

Permalink
fix transformed annotation and opt
Browse files Browse the repository at this point in the history
  • Loading branch information
Anamorphosee committed Nov 30, 2024
1 parent ce977c6 commit f150a07
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
53 changes: 25 additions & 28 deletions common/src/main/kotlin/internal/stacktraceElementsFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import java.lang.invoke.VarHandle
import java.lang.reflect.Field
import java.lang.reflect.GenericSignatureFormatError
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

internal object StacktraceElementsFactoryImpl: StacktraceElementsFactory {
@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -54,22 +52,27 @@ internal object StacktraceElementsFactoryImpl: StacktraceElementsFactory {

private val specsByClassName: MutableMap<String, BaseContinuationClassSpec> = ConcurrentHashMap()
@Volatile private var specsByClassNameSnapshot: Map<String, BaseContinuationClassSpec> = emptyMap()
private val updateSpecsByClassNameSnapshotLock = ReentrantLock()

private fun updateSpecsByClassNameSnapshot() {
updateSpecsByClassNameSnapshotLock.withLock {
specsByClassNameSnapshot = HashMap(specsByClassName)
private fun getBaseContinuationClassSpec(
baseContinuationClassName: String,
new: () -> BaseContinuationClassSpec = { BaseContinuationClassSpec(ReflectionLabelExtractor()) },
updateSnapshot: Boolean = true
): BaseContinuationClassSpec {
var updated = false
val result = specsByClassNameSnapshot[baseContinuationClassName] ?: run {
updated = true
specsByClassName.computeIfAbsent(baseContinuationClassName) { _ -> new() }
}
}

private fun getBaseContinuationClassSpec(baseContinuationClassName: String): BaseContinuationClassSpec =
specsByClassNameSnapshot[baseContinuationClassName] ?: run {
val result = specsByClassName.computeIfAbsent(baseContinuationClassName) { _ ->
BaseContinuationClassSpec(ReflectionLabelExtractor())
if (updateSnapshot && updated) {
while (true) {
try {
specsByClassNameSnapshot = HashMap(specsByClassName)
break
} catch (_: ConcurrentModificationException) { }
}
updateSpecsByClassNameSnapshot()
result
}
return result
}

init {
if (supportsVarHandles) {
Expand Down Expand Up @@ -203,21 +206,15 @@ internal object StacktraceElementsFactoryImpl: StacktraceElementsFactory {
}
}

private fun updateLabelExtractor(baseContinuationClassName: String, lookup: MethodHandles.Lookup) {
val newLabelExtractor = VarHandleLabelExtractor(lookup)
specsByClassName.compute(baseContinuationClassName) { _, oldSpec: BaseContinuationClassSpec? ->
if (oldSpec != null) {
oldSpec.labelExtractor = newLabelExtractor
oldSpec
} else {
BaseContinuationClassSpec(newLabelExtractor)
}
}
updateSpecsByClassNameSnapshot()
}

private fun updateLabelExtractor(spec: TransformedClassesRegistry.TransformedClassSpec) {
spec.baseContinuationClasses.forEach { updateLabelExtractor(it, spec.lookup) }
spec.baseContinuationClasses.forEach { baseContinuationClassName ->
val newLabelExtractor = VarHandleLabelExtractor(spec.lookup)
getBaseContinuationClassSpec(
baseContinuationClassName = baseContinuationClassName,
new = { BaseContinuationClassSpec(newLabelExtractor) },
updateSnapshot = false
).labelExtractor = newLabelExtractor
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion generator/src/main/kotlin/internal/classTransformer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ private fun ClassTransformationInfo.getTransformedAnnotation(clazz: ClassNode):
add(lineNumbers.flatMap { it.value })

add(DecoroutinatorTransformed::baseContinuationClasses.name)
add(baseContinuationInternalClassNames.toList())
add(baseContinuationInternalClassNames.map { Type.getObjectType(it).className })

add(DecoroutinatorTransformed::version.name)
add(TRANSFORMED_VERSION)
Expand Down
2 changes: 1 addition & 1 deletion gradle-plugin/src/main/kotlin/common-gradle-plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

val decoroutinatorTransformedVersionAttribute: Attribute<Int> = Attribute.of(
"dev.reformator.stacktracedecoroutinator.transformedVersion3",
"dev.reformator.stacktracedecoroutinator.transformedVersion4",
Int::class.javaObjectType
)

Expand Down

0 comments on commit f150a07

Please sign in to comment.