Always iterate through the JavaParser tree in POSTORDER

I think this is less prone to errors than the default order.
bzip2
Graham 5 years ago
parent 6742120ab6
commit 828b130906
  1. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.java
  2. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.java
  3. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.java
  4. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.java
  5. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.java
  6. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.java
  7. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.java

@ -1,10 +1,12 @@
package dev.openrs2.deob.ast.transform; package dev.openrs2.deob.ast.transform;
import com.github.javaparser.ast.CompilationUnit; 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.BinaryExpr;
import com.github.javaparser.ast.expr.IntegerLiteralExpr; import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import dev.openrs2.deob.ast.util.ExprUtils; import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.util.NodeUtils;
public final class BitMaskTransformer extends Transformer { public final class BitMaskTransformer extends Transformer {
private static final ImmutableSet<BinaryExpr.Operator> SHIFT_OPS = ImmutableSet.of( private static final ImmutableSet<BinaryExpr.Operator> SHIFT_OPS = ImmutableSet.of(
@ -20,7 +22,7 @@ public final class BitMaskTransformer extends Transformer {
@Override @Override
public void transform(CompilationUnit unit) { 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 shiftOp = expr.getOperator();
var left = expr.getLeft(); var left = expr.getLeft();
var shamtExpr = expr.getRight(); var shamtExpr = expr.getRight();

@ -3,11 +3,13 @@ package dev.openrs2.deob.ast.transform;
import java.util.Optional; import java.util.Optional;
import com.github.javaparser.ast.CompilationUnit; 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.BinaryExpr;
import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.IntegerLiteralExpr; import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.UnaryExpr; import com.github.javaparser.ast.expr.UnaryExpr;
import dev.openrs2.deob.ast.util.ExprUtils; import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.util.NodeUtils;
public final class ComplementTransformer extends Transformer { public final class ComplementTransformer extends Transformer {
private static boolean isComplement(Expression expr) { private static boolean isComplement(Expression expr) {
@ -50,7 +52,7 @@ public final class ComplementTransformer extends Transformer {
@Override @Override
public void transform(CompilationUnit unit) { 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 -> { complement(expr.getOperator()).ifPresent(op -> {
var left = expr.getLeft(); var left = expr.getLeft();
var right = expr.getRight(); var right = expr.getRight();

@ -3,8 +3,10 @@ package dev.openrs2.deob.ast.transform;
import java.util.Optional; import java.util.Optional;
import com.github.javaparser.ast.CompilationUnit; 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.EnclosedExpr;
import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.expr.Expression;
import dev.openrs2.deob.ast.util.NodeUtils;
public final class EncloseTransformer extends Transformer { public final class EncloseTransformer extends Transformer {
private enum Associativity { private enum Associativity {
@ -143,7 +145,7 @@ public final class EncloseTransformer extends Transformer {
@Override @Override
public void transform(CompilationUnit unit) { public void transform(CompilationUnit unit) {
unit.findAll(Expression.class).forEach(expr -> { NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, Expression.class, expr -> {
if (expr.isArrayAccessExpr()) { if (expr.isArrayAccessExpr()) {
var accessExpr = expr.asArrayAccessExpr(); var accessExpr = expr.asArrayAccessExpr();
encloseLeft(expr, accessExpr.getName()); encloseLeft(expr, accessExpr.getName());

@ -37,7 +37,7 @@ public final class IfElseTransformer extends Transformer {
@Override @Override
public void transform(CompilationUnit unit) { public void transform(CompilationUnit unit) {
unit.findAll(IfStmt.class).forEach(stmt -> { NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, IfStmt.class, stmt -> {
stmt.getElseStmt().ifPresent(elseStmt -> { stmt.getElseStmt().ifPresent(elseStmt -> {
var condition = stmt.getCondition(); var condition = stmt.getCondition();
var thenStmt = stmt.getThenStmt(); var thenStmt = stmt.getThenStmt();

@ -1,13 +1,15 @@
package dev.openrs2.deob.ast.transform; package dev.openrs2.deob.ast.transform;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.UnaryExpr; import com.github.javaparser.ast.expr.UnaryExpr;
import dev.openrs2.deob.ast.util.ExprUtils; import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.util.NodeUtils;
public final class NegativeLiteralTransformer extends Transformer { public final class NegativeLiteralTransformer extends Transformer {
@Override @Override
public void transform(CompilationUnit unit) { public void transform(CompilationUnit unit) {
unit.findAll(UnaryExpr.class).forEach(expr -> { NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, UnaryExpr.class, expr -> {
var operand = expr.getExpression(); var operand = expr.getExpression();
if (!ExprUtils.isIntegerOrLongLiteral(operand)) { if (!ExprUtils.isIntegerOrLongLiteral(operand)) {
return; return;

@ -1,13 +1,15 @@
package dev.openrs2.deob.ast.transform; package dev.openrs2.deob.ast.transform;
import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.expr.ConditionalExpr; import com.github.javaparser.ast.expr.ConditionalExpr;
import dev.openrs2.deob.ast.util.ExprUtils; import dev.openrs2.deob.ast.util.ExprUtils;
import dev.openrs2.deob.ast.util.NodeUtils;
public final class TernaryTransformer extends Transformer { public final class TernaryTransformer extends Transformer {
@Override @Override
public void transform(CompilationUnit unit) { 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 condition = expr.getCondition();
var notCondition = ExprUtils.not(condition); var notCondition = ExprUtils.not(condition);
if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) { if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) {

@ -1,12 +1,14 @@
package dev.openrs2.deob.ast.transform; package dev.openrs2.deob.ast.transform;
import com.github.javaparser.ast.CompilationUnit; 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.EnclosedExpr;
import dev.openrs2.deob.ast.util.NodeUtils;
public final class UnencloseTransformer extends Transformer { public final class UnencloseTransformer extends Transformer {
@Override @Override
public void transform(CompilationUnit unit) { 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()); expr.replace(expr.getInner().clone());
}); });
} }

Loading…
Cancel
Save