From faec1f1a26d723f2ee24785fd467bbaa9302e3aa Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 19:08:39 +0000 Subject: [PATCH] Convert TernaryTransformer to Kotlin --- .../ast/transform/TernaryTransformer.java | 27 ------------------- .../deob/ast/transform/TernaryTransformer.kt | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 27 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java deleted file mode 100644 index 5f3e5f9e..00000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java +++ /dev/null @@ -1,27 +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.ConditionalExpr; -import dev.openrs2.deob.ast.util.ExprUtils; -import dev.openrs2.deob.ast.util.NodeUtils; - -public final class TernaryTransformer extends Transformer { - @Override - public void transform(CompilationUnit unit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, ConditionalExpr.class, expr -> { - var condition = expr.getCondition(); - var notCondition = ExprUtils.not(condition); - if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) { - return; - } - - var thenExpr = expr.getThenExpr(); - var elseExpr = expr.getElseExpr(); - - expr.setCondition(notCondition); - expr.setThenExpr(elseExpr.clone()); - expr.setElseExpr(thenExpr.clone()); - }); - } -} 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 new file mode 100644 index 00000000..e18084c1 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt @@ -0,0 +1,27 @@ +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.ExprUtils +import dev.openrs2.deob.ast.util.NodeUtils + +class TernaryTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, ConditionalExpr::class.java) { expr -> + val condition = expr.condition + val notCondition = ExprUtils.not(condition) + if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) { + return@walk + } + + val thenExpr = expr.thenExpr + val elseExpr = expr.elseExpr + + expr.condition = notCondition + + expr.thenExpr = elseExpr.clone() + expr.elseExpr = thenExpr.clone() + } + } +}