diff options
author | Jonas Herzig <me@johni0702.de> | 2020-11-22 14:14:14 +0100 |
---|---|---|
committer | Jonas Herzig <me@johni0702.de> | 2020-11-22 14:27:51 +0100 |
commit | 6073541e763da17de3d51db0e9ee30ddafc01bb9 (patch) | |
tree | 7e69a921155aa83bdc66d81da7ddc8c0d2f50e53 /src/main | |
parent | e49dc899920dce06383cb7f9bd3cba5316cda09b (diff) | |
download | Remap-6073541e763da17de3d51db0e9ee30ddafc01bb9.tar.gz Remap-6073541e763da17de3d51db0e9ee30ddafc01bb9.tar.bz2 Remap-6073541e763da17de3d51db0e9ee30ddafc01bb9.zip |
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.
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/kotlin/com/replaymod/gradle/remap/PsiMapper.kt | 8 | ||||
-rw-r--r-- | src/main/kotlin/com/replaymod/gradle/remap/PsiUtils.kt | 14 |
2 files changed, 17 insertions, 5 deletions
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<PsiLiteralExpression, String>? 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)) |