From e3baede5410991ed0665164c73dc154534627073 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 20:34:17 +0000 Subject: [PATCH] Convert NodeUtils to Kotlin This commit also changes walk() to be an extension function and use reified generics. --- .../deob/ast/transform/AddSubTransformer.kt | 4 ++-- .../transform/BinaryExprOrderTransformer.kt | 4 ++-- .../deob/ast/transform/BitMaskTransformer.kt | 4 ++-- .../ast/transform/ComplementTransformer.kt | 4 ++-- .../deob/ast/transform/EncloseTransformer.kt | 4 ++-- .../deob/ast/transform/IfElseTransformer.kt | 8 ++++---- .../transform/NegativeLiteralTransformer.kt | 4 ++-- .../ast/transform/NewInstanceTransformer.kt | 4 ++-- .../deob/ast/transform/TernaryTransformer.kt | 4 ++-- .../ast/transform/UnencloseTransformer.kt | 4 ++-- .../deob/ast/transform/ValueOfTransformer.kt | 4 ++-- .../dev/openrs2/deob/ast/util/NodeUtils.java | 20 ------------------- .../dev/openrs2/deob/ast/util/NodeUtils.kt | 12 +++++++++++ 13 files changed, 36 insertions(+), 44 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.kt 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 75f1d6b901..c3ff319301 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 @@ -6,12 +6,12 @@ import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.UnaryExpr import dev.openrs2.deob.ast.util.ExprUtils -import dev.openrs2.deob.ast.util.NodeUtils import dev.openrs2.deob.ast.util.TypeUtils +import dev.openrs2.deob.ast.util.walk class AddSubTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { 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 80c0fddd6a..8cfada6788 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 @@ -3,12 +3,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.BinaryExpr -import dev.openrs2.deob.ast.util.NodeUtils import dev.openrs2.deob.ast.util.TypeUtils +import dev.openrs2.deob.ast.util.walk class BinaryExprOrderTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { 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 85173dfa41..309580d280 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 @@ -5,11 +5,11 @@ 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.ExprUtils -import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.walk class BitMaskTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { 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 38686c7617..786a05d37e 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 @@ -7,11 +7,11 @@ import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.IntegerLiteralExpr import com.github.javaparser.ast.expr.UnaryExpr import dev.openrs2.deob.ast.util.ExprUtils -import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.walk class ComplementTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { 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 58b295a017..5a6c340ae5 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 @@ -5,7 +5,7 @@ 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 -import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.walk class EncloseTransformer : Transformer() { private enum class Associativity { @@ -73,7 +73,7 @@ class EncloseTransformer : Transformer() { } override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, Expression::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { expr: Expression -> when { expr.isArrayAccessExpr -> { val accessExpr = expr.asArrayAccessExpr() 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 359b541b28..f65bfca5bf 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 @@ -6,11 +6,11 @@ import com.github.javaparser.ast.stmt.BlockStmt import com.github.javaparser.ast.stmt.IfStmt import com.github.javaparser.ast.stmt.Statement import dev.openrs2.deob.ast.util.ExprUtils -import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.walk class IfElseTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, IfStmt::class.java) { stmt -> + unit.walk(Node.TreeTraversal.POSTORDER) { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt: Statement -> val condition = stmt.condition val thenStmt = stmt.thenStmt @@ -43,7 +43,7 @@ class IfElseTransformer : Transformer() { } } - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, IfStmt::class.java) { stmt -> + unit.walk(Node.TreeTraversal.POSTORDER) { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt -> if (isIf(elseStmt)) { stmt.setElseStmt(getIf(elseStmt)) @@ -73,7 +73,7 @@ class IfElseTransformer : Transformer() { * throw ...; * } */ - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, IfStmt::class.java) { stmt -> + unit.walk(Node.TreeTraversal.POSTORDER) { stmt: IfStmt -> stmt.elseStmt.ifPresent { elseStmt -> // match if (!elseStmt.isBlockStmt) { 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 6389e40a6f..d575359304 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 @@ -4,11 +4,11 @@ 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.ExprUtils -import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.walk class NegativeLiteralTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, UnaryExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { expr: UnaryExpr -> val operand = expr.expression if (!ExprUtils.isIntegerOrLongLiteral(operand)) { 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 0d0bce97c2..04f1f185b1 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 @@ -3,11 +3,11 @@ 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.NodeUtils +import dev.openrs2.deob.ast.util.walk class NewInstanceTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, MethodCallExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { 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 e18084c149..d1a0616995 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 @@ -4,11 +4,11 @@ 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.ExprUtils -import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.walk class TernaryTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, ConditionalExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { expr: ConditionalExpr -> val condition = expr.condition val notCondition = ExprUtils.not(condition) if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) { 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 820fe6819f..294fee0bb1 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 @@ -3,11 +3,11 @@ 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.NodeUtils +import dev.openrs2.deob.ast.util.walk class UnencloseTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, EnclosedExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { 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 618324e8c9..e93f9c7b44 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 @@ -5,11 +5,11 @@ 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 -import dev.openrs2.deob.ast.util.NodeUtils +import dev.openrs2.deob.ast.util.walk class ValueOfTransformer : Transformer() { override fun transform(unit: CompilationUnit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, ObjectCreationExpr::class.java) { expr -> + unit.walk(Node.TreeTraversal.POSTORDER) { 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.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.java deleted file mode 100644 index 5182761636..0000000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.java +++ /dev/null @@ -1,20 +0,0 @@ -package dev.openrs2.deob.ast.util; - -import java.util.function.Consumer; - -import com.github.javaparser.ast.Node; - -public final class NodeUtils { - @SuppressWarnings("unchecked") - public static void walk(Node node, Node.TreeTraversal traversal, Class type, Consumer consumer) { - node.walk(traversal, n -> { - if (type.isAssignableFrom(n.getClass())) { - consumer.accept((T) n); - } - }); - } - - private NodeUtils() { - /* empty */ - } -} 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 new file mode 100644 index 0000000000..4bd09b871d --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.kt @@ -0,0 +1,12 @@ +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) { + if (it is T) { + consumer(it) + } + } +}