From 6073541e763da17de3d51db0e9ee30ddafc01bb9 Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Sun, 22 Nov 2020 14:14:14 +0100 Subject: Support remapping of mixin targets declared in static fields String literals in annotations can be specified in a static final field instead of inline (so you can e.g. use the same literal in multiple places). This commit adds support for remapping those external literals. --- src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt | 8 +++----- src/main/kotlin/com/replaymod/gradle/remap/PsiUtils.kt | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt b/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt index 9ddb78a..b131478 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt @@ -298,7 +298,7 @@ internal class PsiMapper(private val map: MappingSet, private val file: PsiFile) for (attribute in annotation.parameterList.attributes) { if ("method" != attribute.name) continue // Note: mixin supports multiple targets, we do not (yet) - val literalValue = attribute.literalValue ?: continue + val (literalExpr, literalValue) = attribute.resolvedLiteralValue ?: continue val methodMapping = if ('(' in literalValue) { val signature = MethodSignature.of(literalValue) // mapping.findMethodMapping(signature) @@ -323,8 +323,7 @@ internal class PsiMapper(private val map: MappingSet, private val file: PsiFile) } if (mapped != literalValue) { - val value = attribute.value!! - replace(value, '"'.toString() + mapped + '"'.toString()) + replace(literalExpr, '"'.toString() + mapped + '"'.toString()) } } } @@ -418,10 +417,9 @@ internal class PsiMapper(private val map: MappingSet, private val file: PsiFile) for (attribute in annotation.parameterList.attributes) { if ("target" != attribute.name) continue - val signature = attribute.literalValue ?: continue + val (value, signature) = attribute.resolvedLiteralValue ?: continue val newSignature = remapMixinTarget(signature) if (newSignature != signature) { - val value = attribute.value!! replace(value, "\"$newSignature\"") } } diff --git a/src/main/kotlin/com/replaymod/gradle/remap/PsiUtils.kt b/src/main/kotlin/com/replaymod/gradle/remap/PsiUtils.kt index 63f0e76..936ab89 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/PsiUtils.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/PsiUtils.kt @@ -7,6 +7,7 @@ import org.cadixdev.bombe.type.ObjectType import org.cadixdev.bombe.type.Type import org.cadixdev.bombe.type.VoidType import org.cadixdev.bombe.type.signature.MethodSignature +import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil import org.jetbrains.kotlin.com.intellij.psi.* import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.com.intellij.psi.util.TypeConversionUtil @@ -18,6 +19,19 @@ internal val PsiClass.dollarQualifiedName: String? get() { return "$parentName$$selfName" } +internal val PsiNameValuePair.resolvedLiteralValue: Pair? get () { + var value: PsiElement? = value + while (value is PsiReferenceExpression) { + val resolved = value.resolve() + value = when (resolved) { + is PsiField -> resolved.initializer + else -> resolved + } + } + val literal = value as? PsiLiteralExpression ?: return null + return Pair(literal, StringUtil.unquoteString(literal.text)) +} + internal object PsiUtils { fun getSignature(method: PsiMethod): MethodSignature = MethodSignature(method.name, getDescriptor(method)) -- cgit