From 828b130906ea78d04fd3a089c1a37f8bf8255c98 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 25 Aug 2019 15:24:10 +0100 Subject: [PATCH] Always iterate through the JavaParser tree in POSTORDER I think this is less prone to errors than the default order. --- .../dev/openrs2/deob/ast/transform/BitMaskTransformer.java | 4 +++- .../dev/openrs2/deob/ast/transform/ComplementTransformer.java | 4 +++- .../dev/openrs2/deob/ast/transform/EncloseTransformer.java | 4 +++- .../dev/openrs2/deob/ast/transform/IfElseTransformer.java | 2 +- .../deob/ast/transform/NegativeLiteralTransformer.java | 4 +++- .../dev/openrs2/deob/ast/transform/TernaryTransformer.java | 4 +++- .../dev/openrs2/deob/ast/transform/UnencloseTransformer.java | 4 +++- 7 files changed, 19 insertions(+), 7 deletions(-) diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java index f68569ef..ad404472 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java @@ -1,10 +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.BinaryExpr; import com.github.javaparser.ast.expr.IntegerLiteralExpr; import com.google.common.collect.ImmutableSet; import dev.openrs2.deob.ast.util.ExprUtils; +import dev.openrs2.deob.ast.util.NodeUtils; public final class BitMaskTransformer extends Transformer { private static final ImmutableSet SHIFT_OPS = ImmutableSet.of( @@ -20,7 +22,7 @@ public final class BitMaskTransformer extends Transformer { @Override public void transform(CompilationUnit unit) { - unit.findAll(BinaryExpr.class).forEach(expr -> { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> { var shiftOp = expr.getOperator(); var left = expr.getLeft(); var shamtExpr = expr.getRight(); diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java index 24c17aef..424b9d62 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java @@ -3,11 +3,13 @@ package dev.openrs2.deob.ast.transform; import java.util.Optional; 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; import com.github.javaparser.ast.expr.UnaryExpr; import dev.openrs2.deob.ast.util.ExprUtils; +import dev.openrs2.deob.ast.util.NodeUtils; public final class ComplementTransformer extends Transformer { private static boolean isComplement(Expression expr) { @@ -50,7 +52,7 @@ public final class ComplementTransformer extends Transformer { @Override public void transform(CompilationUnit unit) { - unit.findAll(BinaryExpr.class).forEach(expr -> { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, BinaryExpr.class, expr -> { complement(expr.getOperator()).ifPresent(op -> { var left = expr.getLeft(); var right = expr.getRight(); diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.java index b6cd474d..c922d5fc 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.java @@ -3,8 +3,10 @@ package dev.openrs2.deob.ast.transform; import java.util.Optional; import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.Node; import com.github.javaparser.ast.expr.EnclosedExpr; import com.github.javaparser.ast.expr.Expression; +import dev.openrs2.deob.ast.util.NodeUtils; public final class EncloseTransformer extends Transformer { private enum Associativity { @@ -143,7 +145,7 @@ public final class EncloseTransformer extends Transformer { @Override public void transform(CompilationUnit unit) { - unit.findAll(Expression.class).forEach(expr -> { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, Expression.class, expr -> { if (expr.isArrayAccessExpr()) { var accessExpr = expr.asArrayAccessExpr(); encloseLeft(expr, accessExpr.getName()); diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java index c2db141e..169d498e 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java @@ -37,7 +37,7 @@ public final class IfElseTransformer extends Transformer { @Override public void transform(CompilationUnit unit) { - unit.findAll(IfStmt.class).forEach(stmt -> { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, IfStmt.class, stmt -> { stmt.getElseStmt().ifPresent(elseStmt -> { var condition = stmt.getCondition(); var thenStmt = stmt.getThenStmt(); 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 index d2ac9ab2..0c52a297 100644 --- 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 @@ -1,13 +1,15 @@ 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) { - unit.findAll(UnaryExpr.class).forEach(expr -> { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, UnaryExpr.class, expr -> { var operand = expr.getExpression(); if (!ExprUtils.isIntegerOrLongLiteral(operand)) { return; 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 index 08fd83c2..5f3e5f9e 100644 --- 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 @@ -1,13 +1,15 @@ 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) { - unit.findAll(ConditionalExpr.class).forEach(expr -> { + 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)) { diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.java index 01fd39a1..06962030 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.java @@ -1,12 +1,14 @@ 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; public final class UnencloseTransformer extends Transformer { @Override public void transform(CompilationUnit unit) { - unit.findAll(EnclosedExpr.class).forEach(expr -> { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, EnclosedExpr.class, expr -> { expr.replace(expr.getInner().clone()); }); }