From 320a8a7f8a67956700e43402b2872869be019605 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 19:19:00 +0000 Subject: [PATCH] Convert NewInstanceTransformer to Kotlin --- .../ast/transform/NewInstanceTransformer.java | 25 ------------------- .../ast/transform/NewInstanceTransformer.kt | 22 ++++++++++++++++ 2 files changed, 22 insertions(+), 25 deletions(-) delete mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.java create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.java deleted file mode 100644 index ba68aab343..0000000000 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.java +++ /dev/null @@ -1,25 +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.MethodCallExpr; -import dev.openrs2.deob.ast.util.NodeUtils; - -public final class NewInstanceTransformer extends Transformer { - @Override - public void transform(CompilationUnit unit) { - NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, MethodCallExpr.class, expr -> { - if (!expr.getNameAsString().equals("newInstance")) { - return; - } - - expr.getScope().ifPresent(scope -> { - if (scope.isMethodCallExpr() && scope.asMethodCallExpr().getNameAsString().equals("getConstructor")) { - return; - } - - expr.setScope(new MethodCallExpr(scope.clone(), "getDeclaredConstructor")); - }); - }); - } -} diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt new file mode 100644 index 0000000000..392c269284 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt @@ -0,0 +1,22 @@ +package dev.openrs2.deob.ast.transform + +import com.github.javaparser.ast.CompilationUnit +import com.github.javaparser.ast.Node +import com.github.javaparser.ast.expr.MethodCallExpr +import dev.openrs2.deob.ast.util.NodeUtils + +class NewInstanceTransformer : Transformer() { + override fun transform(unit: CompilationUnit) { + NodeUtils.walk(unit, Node.TreeTraversal.POSTORDER, MethodCallExpr::class.java) { expr -> + if (expr.nameAsString != "newInstance") { + return@walk + } + + expr.scope.ifPresent { scope -> + if (!scope.isMethodCallExpr || scope.asMethodCallExpr().nameAsString != "getConstructor") { + expr.setScope(MethodCallExpr(scope.clone(), "getDeclaredConstructor")) + } + } + } + } +}