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.
bzip2
Graham 5 years ago
parent 328b122330
commit bcbf4b6178
  1. 2
      deob-ast/src/main/java/dev/openrs2/deob/ast/AstDeobfuscator.java
  2. 25
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.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()
);

@ -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"));
});
});
}
}
Loading…
Cancel
Save