Skip to content

Commit

Permalink
fix(generator-accessor): group method descriptors exactly when tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed May 30, 2024
1 parent 488eba8 commit a33e970
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,17 +443,18 @@ abstract class AbstractGenerationContext(
* Groups the generator's mappings by version.
*
* @param node the ancestry node
* @param exact whether descriptors should be matched exactly or without return types
* @return the grouped method mappings
*/
protected open fun groupMethodNames(node: MethodAncestryNode): Map<MethodKey, List<Version>> = buildMap<MethodKey, MutableList<Version>> {
protected open fun groupMethodNames(node: MethodAncestryNode, exact: Boolean = false): Map<MethodKey, List<Version>> = buildMap<MethodKey, MutableList<Version>> {
node.forEach { (version, method) ->
val nmsVersion = method.tree.craftBukkitNmsVersion

generator.config.accessedNamespaces.forEach { ns ->
method.getDesc(ns)?.let { desc ->
val name = method.getName(ns) ?: method.srcName

getOrPut(MethodKey(ns, name, desc.replaceCraftBukkitNMSVersion(nmsVersion)), ::mutableListOf) += version
getOrPut(MethodKey(ns, name, desc.replaceCraftBukkitNMSVersion(nmsVersion), exact), ::mutableListOf) += version
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ open class TracingGenerationContext(
output.println(header)
output.printNodeHistory(mergedAccessor.mergedNode)

groupMethodNames(mergedAccessor.mergedNode).forEach { (methodKey, versions) ->
groupMethodNames(mergedAccessor.mergedNode, exact = true).forEach { (methodKey, versions) ->
val (ns, name, desc) = methodKey

output.println("namespace: $ns, mapping: $name, descriptor: $desc, versions: [${versions.joinToString(transform = Version::id)}]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,17 @@ data class ConstructorKey(override val namespace: String, val descriptor: String
/**
* A method mapping key.
*
* *The method return type is not used when checking equality, mimicking reflective lookups.*
*
* @property namespace the mapping namespace
* @property name the mapped method name
* @property descriptor the mapped method descriptor
* @property exact whether descriptors should be matched exactly or without return types
*/
data class MethodKey(override val namespace: String, val name: String, val descriptor: String) : NamespacedKey {
data class MethodKey(override val namespace: String, val name: String, val descriptor: String, val exact: Boolean) : NamespacedKey {
/**
* [descriptor], but with the return type removed.
* The descriptor used for matching.
*/
private val partialDescriptor: String

init {
val parenthesisIndex = descriptor.lastIndexOf(')')
this.partialDescriptor = if (parenthesisIndex != -1) descriptor.substring(0, parenthesisIndex + 1) else descriptor
}
private val matchableDescriptor: String = descriptor.takeIf { exact }
?: descriptor.substringBeforeLast(')')

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -79,15 +74,15 @@ data class MethodKey(override val namespace: String, val name: String, val descr

if (namespace != other.namespace) return false
if (name != other.name) return false
if (partialDescriptor != other.partialDescriptor) return false
if (matchableDescriptor != other.matchableDescriptor) return false

return true
}

override fun hashCode(): Int {
var result = namespace.hashCode()
result = 31 * result + name.hashCode()
result = 31 * result + partialDescriptor.hashCode()
result = 31 * result + matchableDescriptor.hashCode()
return result
}
}

0 comments on commit a33e970

Please sign in to comment.