From 5487a74eb8fc13aaacaea8b8108474d1b007354f Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 15 Feb 2020 20:53:16 +0000 Subject: [PATCH] Remove traversal argument from walk extension method --- .../dev/openrs2/deob/ast/transform/AddSubTransformer.kt | 3 +-- .../deob/ast/transform/BinaryExprOrderTransformer.kt | 3 +-- .../dev/openrs2/deob/ast/transform/BitMaskTransformer.kt | 3 +-- .../openrs2/deob/ast/transform/ComplementTransformer.kt | 3 +-- .../dev/openrs2/deob/ast/transform/EncloseTransformer.kt | 3 +-- .../deob/ast/transform/ForLoopConditionTransformer.kt | 3 +-- .../dev/openrs2/deob/ast/transform/IdentityTransformer.kt | 3 +-- .../dev/openrs2/deob/ast/transform/IfElseTransformer.kt | 7 +++---- .../dev/openrs2/deob/ast/transform/IncrementTransformer.kt | 5 ++--- .../deob/ast/transform/NegativeLiteralTransformer.kt | 3 +-- .../openrs2/deob/ast/transform/NewInstanceTransformer.kt | 3 +-- .../dev/openrs2/deob/ast/transform/TernaryTransformer.kt | 3 +-- .../dev/openrs2/deob/ast/transform/UnencloseTransformer.kt | 3 +-- .../dev/openrs2/deob/ast/transform/ValueOfTransformer.kt | 3 +-- .../src/main/java/dev/openrs2/deob/ast/util/NodeUtils.kt | 5 ++--- 15 files changed, 19 insertions(+), 34 deletions(-) diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt index f3685960..eee041ed 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.UnaryExpr @@ -12,7 +11,7 @@ import dev.openrs2.deob.ast.util.walk class AddSubTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: BinaryExpr -> + unit.walk { expr: BinaryExpr -> val op = expr.operator val left = expr.left val right = expr.right diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt index f6ea3b78..64f59a3d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt @@ -1,14 +1,13 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.BinaryExpr import dev.openrs2.deob.ast.util.isString import dev.openrs2.deob.ast.util.walk class BinaryExprOrderTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: BinaryExpr -> + unit.walk { expr: BinaryExpr -> val op = flip(expr.operator) ?: return@walk val type = expr.calculateResolvedType() diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt index ba1802ad..e8e1701b 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.IntegerLiteralExpr import dev.openrs2.deob.ast.util.createLong @@ -10,7 +9,7 @@ import dev.openrs2.deob.ast.util.walk class BitMaskTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: BinaryExpr -> + unit.walk { expr: BinaryExpr -> val shiftOp = expr.operator val left = expr.left val shamtExpr = expr.right diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt index 355cf101..2ce1f82d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.IntegerLiteralExpr @@ -12,7 +11,7 @@ import dev.openrs2.deob.ast.util.walk class ComplementTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: BinaryExpr -> + unit.walk { expr: BinaryExpr -> val op = complement(expr.operator) ?: return@walk val left = expr.left diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt index 5a6c340a..21feaae9 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.EnclosedExpr import com.github.javaparser.ast.expr.Expression @@ -73,7 +72,7 @@ class EncloseTransformer : Transformer() { } override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: Expression -> + unit.walk { expr: Expression -> when { expr.isArrayAccessExpr -> { val accessExpr = expr.asArrayAccessExpr() diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt index 1d014417..54213cf5 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ForLoopConditionTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.stmt.ForStmt import dev.openrs2.deob.ast.util.hasSideEffects @@ -9,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk class ForLoopConditionTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt -> + unit.walk { stmt: ForStmt -> stmt.compare.ifPresent { compare -> if (!compare.isBinaryExpr) { return@ifPresent diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt index 772ab54c..c4fdd662 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IdentityTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.UnaryExpr @@ -9,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk class IdentityTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: BinaryExpr -> + unit.walk { expr: BinaryExpr -> @Suppress("NON_EXHAUSTIVE_WHEN") when (expr.operator) { BinaryExpr.Operator.PLUS -> { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt index a02b7a12..efd005e1 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.stmt.BlockStmt import com.github.javaparser.ast.stmt.IfStmt import com.github.javaparser.ast.stmt.Statement @@ -11,7 +10,7 @@ import dev.openrs2.deob.ast.util.walk class IfElseTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { stmt: IfStmt -> + unit.walk { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt: Statement -> val condition = stmt.condition val thenStmt = stmt.thenStmt @@ -44,7 +43,7 @@ class IfElseTransformer : Transformer() { } } - unit.walk(Node.TreeTraversal.POSTORDER) { stmt: IfStmt -> + unit.walk { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt -> if (isIf(elseStmt)) { stmt.setElseStmt(getIf(elseStmt)) @@ -74,7 +73,7 @@ class IfElseTransformer : Transformer() { * throw ...; * } */ - unit.walk(Node.TreeTraversal.POSTORDER) { stmt: IfStmt -> + unit.walk { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt -> // match if (!elseStmt.isBlockStmt) { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt index c25e7459..bd577826 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IncrementTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.UnaryExpr import com.github.javaparser.ast.stmt.ExpressionStmt import com.github.javaparser.ast.stmt.ForStmt @@ -9,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk class IncrementTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ExpressionStmt -> + unit.walk { stmt: ExpressionStmt -> if (!stmt.expression.isUnaryExpr) { return@walk } @@ -18,7 +17,7 @@ class IncrementTransformer : Transformer() { unaryExpr.operator = prefixToPostfix(unaryExpr.operator) } - unit.walk(Node.TreeTraversal.POSTORDER) { stmt: ForStmt -> + unit.walk { stmt: ForStmt -> for (expr in stmt.update) { if (!expr.isUnaryExpr) { continue diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt index 621c227b..0cac0b77 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.UnaryExpr import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.negate @@ -9,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk class NegativeLiteralTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: UnaryExpr -> + unit.walk { expr: UnaryExpr -> val operand = expr.expression if (!operand.isIntegerOrLongLiteral()) { return@walk diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt index 25e7cd24..f855f9ee 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt @@ -1,13 +1,12 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.MethodCallExpr import dev.openrs2.deob.ast.util.walk class NewInstanceTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: MethodCallExpr -> + unit.walk { expr: MethodCallExpr -> if (expr.nameAsString != "newInstance") { return@walk } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt index b38ccd7b..07adf92c 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.ConditionalExpr import dev.openrs2.deob.ast.util.countNots import dev.openrs2.deob.ast.util.not @@ -9,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk class TernaryTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: ConditionalExpr -> + unit.walk { expr: ConditionalExpr -> val condition = expr.condition val notCondition = condition.not() if (notCondition.countNots() >= condition.countNots()) { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt index 294fee0b..07ee4849 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt @@ -1,13 +1,12 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.EnclosedExpr import dev.openrs2.deob.ast.util.walk class UnencloseTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: EnclosedExpr -> + unit.walk { expr: EnclosedExpr -> expr.replace(expr.inner.clone()) } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt index e93f9c7b..e1658b4c 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt @@ -1,7 +1,6 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit -import com.github.javaparser.ast.Node import com.github.javaparser.ast.expr.MethodCallExpr import com.github.javaparser.ast.expr.ObjectCreationExpr import com.github.javaparser.ast.expr.TypeExpr @@ -9,7 +8,7 @@ import dev.openrs2.deob.ast.util.walk class ValueOfTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - unit.walk(Node.TreeTraversal.POSTORDER) { expr: ObjectCreationExpr -> + unit.walk { expr: ObjectCreationExpr -> if (expr.type.isBoxedType) { expr.replace(MethodCallExpr(TypeExpr(expr.type), "valueOf", expr.arguments)) } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.kt index 4bd09b87..2cae102e 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.kt @@ -1,10 +1,9 @@ package dev.openrs2.deob.ast.util import com.github.javaparser.ast.Node -import com.github.javaparser.ast.Node.TreeTraversal -inline fun Node.walk(traversal: TreeTraversal, crossinline consumer: (T) -> Unit) { - this.walk(traversal) { +inline fun Node.walk(crossinline consumer: (T) -> Unit) { + this.walk(Node.TreeTraversal.POSTORDER) { if (it is T) { consumer(it) }