diff options
author | Jonas Herzig <me@johni0702.de> | 2022-05-27 08:47:10 +0200 |
---|---|---|
committer | Jonas Herzig <me@johni0702.de> | 2022-05-27 08:55:53 +0200 |
commit | 97d6082a172ca1c3872f81470bf64c6a42d1a571 (patch) | |
tree | 2878d74abf154be3ab6f5daf2ced825a495457c3 | |
parent | 5504b2ff877609d3800d5fce4fbc36a3b01911a5 (diff) | |
download | Remap-97d6082a172ca1c3872f81470bf64c6a42d1a571.tar.gz Remap-97d6082a172ca1c3872f81470bf64c6a42d1a571.tar.bz2 Remap-97d6082a172ca1c3872f81470bf64c6a42d1a571.zip |
Fix pattern applying non-field to left side of assignment
3 files changed, 45 insertions, 3 deletions
diff --git a/src/main/kotlin/com/replaymod/gradle/remap/PsiPattern.kt b/src/main/kotlin/com/replaymod/gradle/remap/PsiPattern.kt index 8573560..4e3a60a 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/PsiPattern.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/PsiPattern.kt @@ -10,7 +10,8 @@ internal class PsiPattern( private val parameters: Set<PsiParameter>, private val varArgs: Boolean, private val pattern: PsiStatement, - private val replacement: List<String> + private val replacement: List<String>, + private val replacementCanBeAssigned: Boolean, ) { private fun find(pattern: PsiElement, tree: PsiElement, result: MutableList<Matcher>) { tree.accept(object : JavaRecursiveElementVisitor() { @@ -66,7 +67,13 @@ internal class PsiPattern( return changes.filterNot { it.first.isEmpty && it.second.isEmpty() } } - fun match(pattern: PsiElement): Boolean = match(pattern, root) + fun match(pattern: PsiElement): Boolean { + val parent = root.parent + if (parent is PsiAssignmentExpression && parent.lExpression == root && !replacementCanBeAssigned) { + return false + } + return match(pattern, root) + } private fun match(pattern: PsiElement?, expr: PsiElement?): Boolean = when (pattern) { null -> expr == null diff --git a/src/main/kotlin/com/replaymod/gradle/remap/PsiPatterns.kt b/src/main/kotlin/com/replaymod/gradle/remap/PsiPatterns.kt index fe6040c..ac5ea10 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/PsiPatterns.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/PsiPatterns.kt @@ -72,7 +72,9 @@ internal class PsiPatterns(private val annotationFQN: String) { replacement.push(replacementFile.slice(start until replacementExpression.endOffset)) } - patterns.add(PsiPattern(parameters, varArgs, body.statements.last(), replacement)) + val replacementCanBeAssigned = replacementExpression is PsiReferenceExpression + + patterns.add(PsiPattern(parameters, varArgs, body.statements.last(), replacement, replacementCanBeAssigned)) } fun find(block: PsiCodeBlock): MutableList<Matcher> { diff --git a/src/test/kotlin/com/replaymod/gradle/remap/pattern/TestFieldReference.kt b/src/test/kotlin/com/replaymod/gradle/remap/pattern/TestFieldReference.kt index a3a6efc..d24d2e6 100644 --- a/src/test/kotlin/com/replaymod/gradle/remap/pattern/TestFieldReference.kt +++ b/src/test/kotlin/com/replaymod/gradle/remap/pattern/TestFieldReference.kt @@ -37,4 +37,37 @@ class TestFieldReference { } """.trimIndent() } + + @Test + fun `should not match final field on left side of assignment when replaced by non-field`() { + TestData.remap("test/Test.java", """ + class Test { + Test field; + private void test() { + field = field; + this.field = this.field; + this.field.field = this.field.field; + } + } + """.trimIndent(), """ + @remap.Pattern + private test.Test pattern(test.Test obj) { + return obj.field; + } + """.trimIndent(), """ + @remap.Pattern + private test.Test pattern(test.Test obj) { + return matched(obj); + } + """.trimIndent()) shouldBe """ + class Test { + Test field; + private void test() { + field = field; + this.field = matched(this); + matched(this).field = matched(matched(this)); + } + } + """.trimIndent() + } }
\ No newline at end of file |