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 <harry@desetude.com>
Desetude 5 years ago committed by Graham
parent fc718a7672
commit ce5a0464c7
  1. 2
      buildSrc/src/main/java/Versions.kt
  2. 7
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/NewInstanceTransformer.kt
  3. 4
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/TypeUtils.kt

@ -11,7 +11,7 @@ object Versions {
const val guice = "4.2.3" const val guice = "4.2.3"
const val inlineLogger = "1.0.2" const val inlineLogger = "1.0.2"
const val jackson = "2.11.0" 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 jdom = "2.0.6"
const val jgrapht = "1.4.0" const val jgrapht = "1.4.0"
const val jimfs = "1.1" const val jimfs = "1.1"

@ -2,6 +2,7 @@ package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.expr.MethodCallExpr import com.github.javaparser.ast.expr.MethodCallExpr
import dev.openrs2.deob.ast.util.isClass
import dev.openrs2.deob.ast.util.walk import dev.openrs2.deob.ast.util.walk
import javax.inject.Singleton import javax.inject.Singleton
@ -17,14 +18,10 @@ class NewInstanceTransformer : Transformer() {
} }
expr.scope.ifPresent { scope -> expr.scope.ifPresent { scope ->
if (!scope.isMethodCallExpr || scope.asMethodCallExpr().nameAsString !in CONSTRUCTOR_METHODS) { if (scope.calculateResolvedType().isClass()) {
expr.setScope(MethodCallExpr(scope.clone(), "getDeclaredConstructor")) expr.setScope(MethodCallExpr(scope.clone(), "getDeclaredConstructor"))
} }
} }
} }
} }
private companion object {
private val CONSTRUCTOR_METHODS = setOf("getConstructor", "getDeclaredConstructor")
}
} }

@ -5,3 +5,7 @@ import com.github.javaparser.resolution.types.ResolvedType
fun ResolvedType.isString(): Boolean { fun ResolvedType.isString(): Boolean {
return isReferenceType && asReferenceType().qualifiedName == "java.lang.String" return isReferenceType && asReferenceType().qualifiedName == "java.lang.String"
} }
fun ResolvedType.isClass(): Boolean {
return isReferenceType && asReferenceType().qualifiedName == "java.lang.Class"
}

Loading…
Cancel
Save