From 38a3e752f9884135c9fc705a3297b0a423d70761 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 19:04:21 +0000 Subject: [PATCH] Convert NegativeLiteralTransformer to Kotlin --- .../transform/NegativeLiteralTransformer.java | 26 ----------------- .../transform/NegativeLiteralTransformer.kt | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 26 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java deleted file mode 100644 index 0c52a297..00000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java +++ /dev/null @@ -1,26 +0,0 @@ -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.ExprUtils; -import dev.openrs2.deob.ast.util.NodeUtils; - -public final class NegativeLiteralTransformer extends Transformer { - @Override - public void transform(CompilationUnit unit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, UnaryExpr.class, expr -> { - var operand = expr.getExpression(); - if (!ExprUtils.isIntegerOrLongLiteral(operand)) { - return; - } - - var op = expr.getOperator(); - if (op == UnaryExpr.Operator.PLUS) { - expr.replace(operand); - } else if (op == UnaryExpr.Operator.MINUS) { - expr.replace(ExprUtils.negate(operand)); - } - }); - } -} 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 new file mode 100644 index 00000000..6389e40a --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt @@ -0,0 +1,28 @@ +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.ExprUtils +import dev.openrs2.deob.ast.util.NodeUtils + +class NegativeLiteralTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, UnaryExpr::class.java) { expr -> + val operand = expr.expression + if (!ExprUtils.isIntegerOrLongLiteral(operand)) { + return@walk + } + + @Suppress("NON_EXHAUSTIVE_WHEN") + when (expr.operator) { + UnaryExpr.Operator.PLUS -> { + expr.replace(operand) + } + UnaryExpr.Operator.MINUS -> { + expr.replace(ExprUtils.negate(operand)) + } + } + } + } +}