From bcbf4b6178a9f2050107f52637e05f03a969d5dc Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 19 Oct 2019 10:56:51 +0100 Subject: [PATCH] Replace newInstance() with getDeclaredConstructor().newInstance() Again, if these are removed from a future JDK we'll need to move this transform to the bytecode deobfuscator. --- .../dev/openrs2/deob/ast/AstDeobfuscator.java | 2 ++ .../ast/transform/NewInstanceTransformer.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.java diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java index b664859657..e75e10126d 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java @@ -19,6 +19,7 @@ import dev.openrs2.deob.ast.transform.ComplementTransformer; import dev.openrs2.deob.ast.transform.EncloseTransformer; import dev.openrs2.deob.ast.transform.IfElseTransformer; import dev.openrs2.deob.ast.transform.NegativeLiteralTransformer; +import dev.openrs2.deob.ast.transform.NewInstanceTransformer; import dev.openrs2.deob.ast.transform.TernaryTransformer; import dev.openrs2.deob.ast.transform.Transformer; import dev.openrs2.deob.ast.transform.UnencloseTransformer; @@ -35,6 +36,7 @@ public final class AstDeobfuscator { new AddSubTransformer(), new BitMaskTransformer(), new ValueOfTransformer(), + new NewInstanceTransformer(), new EncloseTransformer() ); 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 new file mode 100644 index 0000000000..ba68aab343 --- /dev/null +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.java @@ -0,0 +1,25 @@ +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")); + }); + }); + } +}