Retain (T[]) casts on a variadic T... argument

This prevents IDEA from showing a warning about the method call being
ambiguous.

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent d1b8fbd094
commit d305e1f41c
  1. 16
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/RedundantCastTransformer.kt

@ -9,6 +9,7 @@ import com.github.javaparser.ast.expr.NullLiteralExpr
import com.github.javaparser.ast.expr.ObjectCreationExpr
import com.github.javaparser.ast.expr.VariableDeclarationExpr
import com.github.javaparser.resolution.MethodAmbiguityException
import com.github.javaparser.resolution.declarations.ResolvedMethodLikeDeclaration
import dev.openrs2.deob.ast.Library
import dev.openrs2.deob.ast.LibraryGroup
import dev.openrs2.deob.ast.util.walk
@ -29,6 +30,8 @@ class RedundantCastTransformer : Transformer() {
val arg = expr.arguments[i]
if (!isCastedNull(arg)) {
continue
} else if (resolvesVariadicAmbiguity(i, expr.resolve(), arg as CastExpr)) {
continue
}
expr.arguments[i] = NullLiteralExpr()
@ -45,6 +48,8 @@ class RedundantCastTransformer : Transformer() {
val arg = expr.arguments[i]
if (!isCastedNull(arg)) {
continue
} else if (resolvesVariadicAmbiguity(i, expr.resolve(), arg as CastExpr)) {
continue
}
expr.arguments[i] = NullLiteralExpr()
@ -80,4 +85,15 @@ class RedundantCastTransformer : Transformer() {
}
return expr.expression is NullLiteralExpr
}
private fun resolvesVariadicAmbiguity(index: Int, method: ResolvedMethodLikeDeclaration, cast: CastExpr): Boolean {
if (index < method.numberOfParams) {
val param = method.getParam(index)
if (param.isVariadic && param.type == cast.type.resolve()) {
return true
}
}
return false
}
}

Loading…
Cancel
Save