Search in sources :

Example 1 with TypeCompare

use of jadx.core.dex.visitors.typeinference.TypeCompare in project jadx by skylot.

the class MethodInvokeVisitor method isMethodAcceptable.

private boolean isMethodAcceptable(IMethodDetails methodDetails, List<ArgType> types, Function<TypeCompareEnum, Boolean> acceptFunction) {
    List<ArgType> mthTypes = methodDetails.getArgTypes();
    int argCount = mthTypes.size();
    if (argCount != types.size()) {
        return false;
    }
    TypeCompare typeCompare = root.getTypeUpdate().getTypeCompare();
    for (int i = 0; i < argCount; i++) {
        ArgType mthType = mthTypes.get(i);
        ArgType argType = types.get(i);
        TypeCompareEnum result = typeCompare.compareTypes(argType, mthType);
        if (!acceptFunction.apply(result)) {
            return false;
        }
    }
    return true;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) TypeCompare(jadx.core.dex.visitors.typeinference.TypeCompare) TypeCompareEnum(jadx.core.dex.visitors.typeinference.TypeCompareEnum)

Example 2 with TypeCompare

use of jadx.core.dex.visitors.typeinference.TypeCompare in project jadx by skylot.

the class OverrideMethodVisitor method updateReturnType.

private boolean updateReturnType(MethodNode mth, IMethodDetails baseMth, SuperTypesData superData) {
    ArgType baseReturnType = baseMth.getReturnType();
    if (mth.getReturnType().equals(baseReturnType)) {
        return false;
    }
    if (!baseReturnType.containsTypeVariable()) {
        return false;
    }
    TypeCompare typeCompare = mth.root().getTypeUpdate().getTypeCompare();
    ArgType baseCls = baseMth.getMethodInfo().getDeclClass().getType();
    for (ArgType superType : superData.getSuperTypes()) {
        TypeCompareEnum compareResult = typeCompare.compareTypes(superType, baseCls);
        if (compareResult == TypeCompareEnum.NARROW_BY_GENERIC) {
            ArgType targetRetType = mth.root().getTypeUtils().replaceClassGenerics(superType, baseReturnType);
            if (targetRetType != null && !targetRetType.containsTypeVariable() && !targetRetType.equals(mth.getReturnType())) {
                mth.updateReturnType(targetRetType);
                return true;
            }
        }
    }
    return false;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) TypeCompare(jadx.core.dex.visitors.typeinference.TypeCompare) TypeCompareEnum(jadx.core.dex.visitors.typeinference.TypeCompareEnum)

Example 3 with TypeCompare

use of jadx.core.dex.visitors.typeinference.TypeCompare in project jadx by skylot.

the class OverrideMethodVisitor method updateArgType.

private ArgType updateArgType(MethodNode mth, IMethodDetails baseMth, SuperTypesData superData, int argNum) {
    ArgType arg = mth.getArgTypes().get(argNum);
    ArgType baseArg = baseMth.getArgTypes().get(argNum);
    if (arg.equals(baseArg)) {
        return null;
    }
    if (!baseArg.containsTypeVariable()) {
        return null;
    }
    TypeCompare typeCompare = mth.root().getTypeUpdate().getTypeCompare();
    ArgType baseCls = baseMth.getMethodInfo().getDeclClass().getType();
    for (ArgType superType : superData.getSuperTypes()) {
        TypeCompareEnum compareResult = typeCompare.compareTypes(superType, baseCls);
        if (compareResult == TypeCompareEnum.NARROW_BY_GENERIC) {
            ArgType targetArgType = mth.root().getTypeUtils().replaceClassGenerics(superType, baseArg);
            if (targetArgType != null && !targetArgType.containsTypeVariable() && !targetArgType.equals(arg)) {
                return targetArgType;
            }
        }
    }
    return null;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) TypeCompare(jadx.core.dex.visitors.typeinference.TypeCompare) TypeCompareEnum(jadx.core.dex.visitors.typeinference.TypeCompareEnum)

Example 4 with TypeCompare

use of jadx.core.dex.visitors.typeinference.TypeCompare in project jadx by skylot.

the class BlockExceptionHandler method sortHandlers.

private static void sortHandlers(MethodNode mth, List<TryCatchBlockAttr> tryBlocks) {
    TypeCompare typeCompare = mth.root().getTypeCompare();
    Comparator<ArgType> comparator = typeCompare.getReversedComparator();
    for (TryCatchBlockAttr tryBlock : tryBlocks) {
        for (ExceptionHandler handler : tryBlock.getHandlers()) {
            handler.getCatchTypes().sort((first, second) -> compareByTypeAndName(comparator, first, second));
        }
        tryBlock.getHandlers().sort((first, second) -> {
            if (first.equals(second)) {
                throw new JadxRuntimeException("Same handlers in try block: " + tryBlock);
            }
            if (first.isCatchAll()) {
                return 1;
            }
            if (second.isCatchAll()) {
                return -1;
            }
            return compareByTypeAndName(comparator, ListUtils.first(first.getCatchTypes()), ListUtils.first(second.getCatchTypes()));
        });
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) TypeCompare(jadx.core.dex.visitors.typeinference.TypeCompare) TryCatchBlockAttr(jadx.core.dex.trycatch.TryCatchBlockAttr) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 5 with TypeCompare

use of jadx.core.dex.visitors.typeinference.TypeCompare in project jadx by skylot.

the class AttachTryCatchVisitor method checkAndFilterHandlers.

private static void checkAndFilterHandlers(MethodNode mth, List<ExceptionHandler> list) {
    if (list.size() <= 1) {
        return;
    }
    // Remove shadowed handlers (with same or narrow type compared to previous)
    TypeCompare typeCompare = mth.root().getTypeCompare();
    Iterator<ExceptionHandler> it = list.iterator();
    ArgType maxType = null;
    while (it.hasNext()) {
        ExceptionHandler handler = it.next();
        ArgType maxCatch = maxCatchFromHandler(handler, typeCompare);
        if (maxType == null) {
            maxType = maxCatch;
        } else {
            TypeCompareEnum result = typeCompare.compareObjects(maxType, maxCatch);
            if (result.isWiderOrEqual()) {
                if (Consts.DEBUG_EXC_HANDLERS) {
                    LOG.debug("Removed shadowed catch handler: {}, from list: {}", handler, list);
                }
                it.remove();
            }
        }
    }
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) ArgType(jadx.core.dex.instructions.args.ArgType) TypeCompare(jadx.core.dex.visitors.typeinference.TypeCompare) TypeCompareEnum(jadx.core.dex.visitors.typeinference.TypeCompareEnum)

Aggregations

ArgType (jadx.core.dex.instructions.args.ArgType)6 TypeCompare (jadx.core.dex.visitors.typeinference.TypeCompare)6 TypeCompareEnum (jadx.core.dex.visitors.typeinference.TypeCompareEnum)5 ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)2 CodeVar (jadx.core.dex.instructions.args.CodeVar)1 TryCatchBlockAttr (jadx.core.dex.trycatch.TryCatchBlockAttr)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1