Fix use of deprecated JavaParser methods

bzip2
Graham 5 years ago
parent 0a988584b7
commit 25a6953644
  1. 14
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/AddSubTransformer.kt
  2. 10
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/BitMaskTransformer.kt
  3. 6
      deob-ast/src/main/java/dev/openrs2/deob/ast/transform/ComplementTransformer.kt
  4. 28
      deob-ast/src/main/java/dev/openrs2/deob/ast/util/ExprUtils.kt

@ -3,6 +3,8 @@ package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.Expression
import com.github.javaparser.ast.expr.IntegerLiteralExpr
import com.github.javaparser.ast.expr.LongLiteralExpr
import com.github.javaparser.ast.expr.UnaryExpr import com.github.javaparser.ast.expr.UnaryExpr
import dev.openrs2.deob.ast.util.hasSideEffects import dev.openrs2.deob.ast.util.hasSideEffects
import dev.openrs2.deob.ast.util.isString import dev.openrs2.deob.ast.util.isString
@ -45,8 +47,16 @@ class AddSubTransformer : Transformer() {
private fun Expression.isNegative(): Boolean { private fun Expression.isNegative(): Boolean {
return when { return when {
isUnaryExpr -> asUnaryExpr().operator == UnaryExpr.Operator.MINUS isUnaryExpr -> asUnaryExpr().operator == UnaryExpr.Operator.MINUS
isIntegerLiteralExpr -> asIntegerLiteralExpr().asInt() < 0 isIntegerLiteralExpr -> when (val n = asIntegerLiteralExpr().asNumber()) {
isLongLiteralExpr -> asLongLiteralExpr().asLong() < 0 IntegerLiteralExpr.MAX_31_BIT_UNSIGNED_VALUE_AS_LONG -> false
is Int -> n < 0
else -> error("Invalid IntegerLiteralExpr type")
}
isLongLiteralExpr -> when (val n = asLongLiteralExpr().asNumber()) {
LongLiteralExpr.MAX_63_BIT_UNSIGNED_VALUE_AS_BIG_INTEGER -> false
is Long -> n < 0
else -> error("Invalid LongLiteralExpr type")
}
else -> false else -> false
} }
} }

@ -3,6 +3,8 @@ package dev.openrs2.deob.ast.transform
import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.CompilationUnit
import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.expr.IntegerLiteralExpr import com.github.javaparser.ast.expr.IntegerLiteralExpr
import dev.openrs2.deob.ast.util.checkedAsInt
import dev.openrs2.deob.ast.util.checkedAsLong
import dev.openrs2.deob.ast.util.createLong import dev.openrs2.deob.ast.util.createLong
import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral
import dev.openrs2.deob.ast.util.walk import dev.openrs2.deob.ast.util.walk
@ -27,9 +29,9 @@ class BitMaskTransformer : Transformer() {
return@walk return@walk
} }
val shamt = shamtExpr.asIntegerLiteralExpr().asInt() val shamt = shamtExpr.asIntegerLiteralExpr().checkedAsInt()
if (maskExpr.isIntegerLiteralExpr) { if (maskExpr.isIntegerLiteralExpr) {
var mask = maskExpr.asIntegerLiteralExpr().asInt() var mask = maskExpr.asIntegerLiteralExpr().checkedAsInt()
mask = when (shiftOp) { mask = when (shiftOp) {
BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt
@ -37,9 +39,9 @@ class BitMaskTransformer : Transformer() {
else -> error("Invalid shiftOp") else -> error("Invalid shiftOp")
} }
maskExpr = IntegerLiteralExpr(mask) maskExpr = IntegerLiteralExpr(mask.toString())
} else { } else {
var mask = maskExpr.asLongLiteralExpr().asLong() var mask = maskExpr.asLongLiteralExpr().checkedAsLong()
mask = when (shiftOp) { mask = when (shiftOp) {
BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt BinaryExpr.Operator.SIGNED_RIGHT_SHIFT -> mask shr shamt

@ -5,6 +5,8 @@ import com.github.javaparser.ast.expr.BinaryExpr
import com.github.javaparser.ast.expr.Expression import com.github.javaparser.ast.expr.Expression
import com.github.javaparser.ast.expr.IntegerLiteralExpr import com.github.javaparser.ast.expr.IntegerLiteralExpr
import com.github.javaparser.ast.expr.UnaryExpr import com.github.javaparser.ast.expr.UnaryExpr
import dev.openrs2.deob.ast.util.checkedAsInt
import dev.openrs2.deob.ast.util.checkedAsLong
import dev.openrs2.deob.ast.util.createLong import dev.openrs2.deob.ast.util.createLong
import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral import dev.openrs2.deob.ast.util.isIntegerOrLongLiteral
import dev.openrs2.deob.ast.util.walk import dev.openrs2.deob.ast.util.walk
@ -49,8 +51,8 @@ class ComplementTransformer : Transformer() {
private fun Expression.complement(): Expression { private fun Expression.complement(): Expression {
return when { return when {
isUnaryExpr -> asUnaryExpr().expression isUnaryExpr -> asUnaryExpr().expression
isIntegerLiteralExpr -> IntegerLiteralExpr(asIntegerLiteralExpr().asInt().inv()) isIntegerLiteralExpr -> IntegerLiteralExpr(asIntegerLiteralExpr().checkedAsInt().inv().toString())
isLongLiteralExpr -> createLong(asLongLiteralExpr().asLong().inv()) isLongLiteralExpr -> createLong(asLongLiteralExpr().checkedAsLong().inv())
else -> throw IllegalArgumentException() else -> throw IllegalArgumentException()
} }
} }

@ -7,6 +7,22 @@ import com.github.javaparser.ast.expr.IntegerLiteralExpr
import com.github.javaparser.ast.expr.LongLiteralExpr import com.github.javaparser.ast.expr.LongLiteralExpr
import com.github.javaparser.ast.expr.UnaryExpr import com.github.javaparser.ast.expr.UnaryExpr
fun IntegerLiteralExpr.checkedAsInt(): Int {
val n = asNumber()
if (n !is Int) {
error("Invalid IntegerLiteralExpr type")
}
return n
}
fun LongLiteralExpr.checkedAsLong(): Long {
val n = asNumber()
if (n !is Long) {
error("Invalid LongLiteralExpr type")
}
return n
}
fun Expression.isIntegerOrLongLiteral(): Boolean { fun Expression.isIntegerOrLongLiteral(): Boolean {
return isIntegerLiteralExpr || isLongLiteralExpr return isIntegerLiteralExpr || isLongLiteralExpr
} }
@ -19,9 +35,17 @@ fun Expression.negate(): Expression {
return if (isUnaryExpr && asUnaryExpr().operator == UnaryExpr.Operator.MINUS) { return if (isUnaryExpr && asUnaryExpr().operator == UnaryExpr.Operator.MINUS) {
asUnaryExpr().expression.clone() asUnaryExpr().expression.clone()
} else if (isIntegerLiteralExpr) { } else if (isIntegerLiteralExpr) {
IntegerLiteralExpr(-asIntegerLiteralExpr().asInt()) when (val n = asIntegerLiteralExpr().asNumber()) {
IntegerLiteralExpr.MAX_31_BIT_UNSIGNED_VALUE_AS_LONG -> IntegerLiteralExpr(Integer.MIN_VALUE.toString())
is Int -> IntegerLiteralExpr((-n.toInt()).toString())
else -> error("Invalid IntegerLiteralExpr type")
}
} else if (isLongLiteralExpr) { } else if (isLongLiteralExpr) {
createLong(-asLongLiteralExpr().asLong()) when (val n = asLongLiteralExpr().asNumber()) {
LongLiteralExpr.MAX_63_BIT_UNSIGNED_VALUE_AS_BIG_INTEGER -> createLong(Long.MIN_VALUE)
is Long -> createLong(-n)
else -> error("Invalid LongLiteralExpr type")
}
} else { } else {
throw IllegalArgumentException() throw IllegalArgumentException()
} }

Loading…
Cancel
Save