From ce5a0464c7ba2813dbd1a4ecec210bd6c8559394 Mon Sep 17 00:00:00 2001 From: Desetude Date: Mon, 25 May 2020 17:06:02 +0100 Subject: [PATCH] Only run NewInstanceTransformer on Class references Fixes a problem where `Constructor var0 = clazz.getDeclaredConstructor(); return var0.newInstance();` was transformed to `Constructor var0 = clazz.getDeclaredConstructor(); return var0.getDeclaredConstructor().newInstance();` Signed-off-by: Desetude --- buildSrc/src/main/java/Versions.kt | 2 +- .../openrs2/deob/ast/transform/NewInstanceTransformer.kt | 7 ++----- .../src/main/java/dev/openrs2/deob/ast/util/TypeUtils.kt | 4 ++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/buildSrc/src/main/java/Versions.kt b/buildSrc/src/main/java/Versions.kt index 04e4f2dac3..819355b6c1 100644 --- a/buildSrc/src/main/java/Versions.kt +++ b/buildSrc/src/main/java/Versions.kt @@ -11,7 +11,7 @@ object Versions { const val guice = "4.2.3" const val inlineLogger = "1.0.2" const val jackson = "2.11.0" - const val javaParser = "3.15.22" + const val javaParser = "3.16.1" const val jdom = "2.0.6" const val jgrapht = "1.4.0" const val jimfs = "1.1" 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 index c71b773f24..1043669826 100644 --- 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 @@ -2,6 +2,7 @@ package dev.openrs2.deob.ast.transform import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.expr.MethodCallExpr +import dev.openrs2.deob.ast.util.isClass import dev.openrs2.deob.ast.util.walk import javax.inject.Singleton @@ -17,14 +18,10 @@ class NewInstanceTransformer : Transformer() { } expr.scope.ifPresent { scope -> - if (!scope.isMethodCallExpr || scope.asMethodCallExpr().nameAsString !in CONSTRUCTOR_METHODS) { + if (scope.calculateResolvedType().isClass()) { expr.setScope(MethodCallExpr(scope.clone(), "getDeclaredConstructor")) } } } } - - private companion object { - private val CONSTRUCTOR_METHODS = setOf("getConstructor", "getDeclaredConstructor") - } } diff --git a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.kt b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.kt index 4e8f8814b3..a0a12a39d4 100644 --- a/deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.kt +++ b/deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.kt @@ -5,3 +5,7 @@ import com.github.javaparser.resolution.types.ResolvedType fun ResolvedType.isString(): Boolean { return isReferenceType && asReferenceType().qualifiedName == "java.lang.String" } + +fun ResolvedType.isClass(): Boolean { + return isReferenceType && asReferenceType().qualifiedName == "java.lang.Class" +}