Convert NodeUtils to Kotlin

This commit also changes walk() to be an extension function and use reified
generics.
master
Graham 5 years ago
parent 9f09a81db1
commit e3baede541
  1. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt
  2. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BinaryExprOrderTransformer.kt
  3. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt
  4. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt
  5. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/EncloseTransformer.kt
  6. 8
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/IfElseTransformer.kt
  7. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NegativeLiteralTransformer.kt
  8. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt
  9. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/TernaryTransformer.kt
  10. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/UnencloseTransformer.kt
  11. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ValueOfTransformer.kt
  12. 20
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.java
  13. 12
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/NodeUtils.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.Expression
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
import dev.openrs2.deob.ast.util.TypeUtils import dev.openrs2.deob.ast.util.TypeUtils
import dev.openrs2.deob.ast.util.walk
class AddSubTransformer : Transformer() { class AddSubTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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 op = expr.operator
val left = expr.left val left = expr.left
val right = expr.right val right = expr.right

@ -3,12 +3,12 @@ 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.Node
import com.github.javaparser.ast.expr.BinaryExpr 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.TypeUtils
import dev.openrs2.deob.ast.util.walk
class BinaryExprOrderTransformer : Transformer() { class BinaryExprOrderTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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 op = flip(expr.operator) ?: return@walk
val type = expr.calculateResolvedType() val type = expr.calculateResolvedType()

@ -5,11 +5,11 @@ 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 dev.openrs2.deob.ast.util.ExprUtils import dev.openrs2.deob.ast.util.ExprUtils
import dev.openrs2.deob.ast.util.NodeUtils import dev.openrs2.deob.ast.util.walk
class BitMaskTransformer : Transformer() { class BitMaskTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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 shiftOp = expr.operator
val left = expr.left val left = expr.left
val shamtExpr = expr.right val shamtExpr = expr.right

@ -7,11 +7,11 @@ 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 import dev.openrs2.deob.ast.util.walk
class ComplementTransformer : Transformer() { class ComplementTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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 op = complement(expr.operator) ?: return@walk
val left = expr.left val left = expr.left

@ -5,7 +5,7 @@ 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.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 import dev.openrs2.deob.ast.util.walk
class EncloseTransformer : Transformer() { class EncloseTransformer : Transformer() {
private enum class Associativity { private enum class Associativity {
@ -73,7 +73,7 @@ class EncloseTransformer : Transformer() {
} }
override fun transform(unit: CompilationUnit) { override fun transform(unit: CompilationUnit) {
NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, Expression::class.java) { expr -> unit.walk(Node.TreeTraversal.POSTORDER) { expr: Expression ->
when { when {
expr.isArrayAccessExpr -> { expr.isArrayAccessExpr -> {
val accessExpr = expr.asArrayAccessExpr() val accessExpr = expr.asArrayAccessExpr()

@ -6,11 +6,11 @@ import com.github.javaparser.ast.stmt.BlockStmt
import com.github.javaparser.ast.stmt.IfStmt import com.github.javaparser.ast.stmt.IfStmt
import com.github.javaparser.ast.stmt.Statement import com.github.javaparser.ast.stmt.Statement
import dev.openrs2.deob.ast.util.ExprUtils import dev.openrs2.deob.ast.util.ExprUtils
import dev.openrs2.deob.ast.util.NodeUtils import dev.openrs2.deob.ast.util.walk
class IfElseTransformer : Transformer() { class IfElseTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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 -> stmt.elseStmt.ifPresent { elseStmt: Statement ->
val condition = stmt.condition val condition = stmt.condition
val thenStmt = stmt.thenStmt 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 -> stmt.elseStmt.ifPresent { elseStmt ->
if (isIf(elseStmt)) { if (isIf(elseStmt)) {
stmt.setElseStmt(getIf(elseStmt)) stmt.setElseStmt(getIf(elseStmt))
@ -73,7 +73,7 @@ class IfElseTransformer : Transformer() {
* throw ...; * throw ...;
* } * }
*/ */
NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, IfStmt::class.java) { stmt -> unit.walk(Node.TreeTraversal.POSTORDER) { stmt: IfStmt ->
stmt.elseStmt.ifPresent { elseStmt -> stmt.elseStmt.ifPresent { elseStmt ->
// match // match
if (!elseStmt.isBlockStmt) { if (!elseStmt.isBlockStmt) {

@ -4,11 +4,11 @@ import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.Node 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 import dev.openrs2.deob.ast.util.walk
class NegativeLiteralTransformer : Transformer() { class NegativeLiteralTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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 val operand = expr.expression
if (!ExprUtils.isIntegerOrLongLiteral(operand)) { if (!ExprUtils.isIntegerOrLongLiteral(operand)) {
return@walk return@walk

@ -3,11 +3,11 @@ 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.Node
import com.github.javaparser.ast.expr.MethodCallExpr import com.github.javaparser.ast.expr.MethodCallExpr
import dev.openrs2.deob.ast.util.NodeUtils import dev.openrs2.deob.ast.util.walk
class NewInstanceTransformer : Transformer() { class NewInstanceTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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") { if (expr.nameAsString != "newInstance") {
return@walk return@walk
} }

@ -4,11 +4,11 @@ import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.Node 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 import dev.openrs2.deob.ast.util.walk
class TernaryTransformer : Transformer() { class TernaryTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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 condition = expr.condition
val notCondition = ExprUtils.not(condition) val notCondition = ExprUtils.not(condition)
if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) { if (ExprUtils.countNots(notCondition) >= ExprUtils.countNots(condition)) {

@ -3,11 +3,11 @@ 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.Node
import com.github.javaparser.ast.expr.EnclosedExpr import com.github.javaparser.ast.expr.EnclosedExpr
import dev.openrs2.deob.ast.util.NodeUtils import dev.openrs2.deob.ast.util.walk
class UnencloseTransformer : Transformer() { class UnencloseTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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()) expr.replace(expr.inner.clone())
} }
} }

@ -5,11 +5,11 @@ import com.github.javaparser.ast.Node
import com.github.javaparser.ast.expr.MethodCallExpr import com.github.javaparser.ast.expr.MethodCallExpr
import com.github.javaparser.ast.expr.ObjectCreationExpr import com.github.javaparser.ast.expr.ObjectCreationExpr
import com.github.javaparser.ast.expr.TypeExpr import com.github.javaparser.ast.expr.TypeExpr
import dev.openrs2.deob.ast.util.NodeUtils import dev.openrs2.deob.ast.util.walk
class ValueOfTransformer : Transformer() { class ValueOfTransformer : Transformer() {
override fun transform(unit: CompilationUnit) { 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) { if (expr.type.isBoxedType) {
expr.replace(MethodCallExpr(TypeExpr(expr.type), "valueOf", expr.arguments)) expr.replace(MethodCallExpr(TypeExpr(expr.type), "valueOf", expr.arguments))
} }

@ -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 <T extends Node> void walk(Node node, Node.TreeTraversal traversal, Class<T> type, Consumer<T> consumer) {
node.walk(traversal, n -> {
if (type.isAssignableFrom(n.getClass())) {
consumer.accept((T) n);
}
});
}
private NodeUtils() {
/* empty */
}
}

@ -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 <reified T : Node> Node.walk(traversal: TreeTraversal, crossinline consumer: (T) -> Unit) {
this.walk(traversal) {
if (it is T) {
consumer(it)
}
}
}
Loading…
Cancel
Save