diff options
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 |