Search in sources :

Example 1 with TypeUtils

use of jadx.core.dex.nodes.utils.TypeUtils in project jadx by skylot.

the class MethodInvokeVisitor method getTypeVarsMapping.

private Map<ArgType, ArgType> getTypeVarsMapping(BaseInvokeNode invokeInsn) {
    MethodInfo callMthInfo = invokeInsn.getCallMth();
    ArgType declClsType = callMthInfo.getDeclClass().getType();
    ArgType callClsType = getClsCallType(invokeInsn, declClsType);
    TypeUtils typeUtils = root.getTypeUtils();
    Map<ArgType, ArgType> clsTypeVars = typeUtils.getTypeVariablesMapping(callClsType);
    Map<ArgType, ArgType> mthTypeVars = typeUtils.getTypeVarMappingForInvoke(invokeInsn);
    return Utils.mergeMaps(clsTypeVars, mthTypeVars);
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) MethodInfo(jadx.core.dex.info.MethodInfo) TypeUtils(jadx.core.dex.nodes.utils.TypeUtils)

Example 2 with TypeUtils

use of jadx.core.dex.nodes.utils.TypeUtils in project jadx by skylot.

the class TypeUpdate method invokeListener.

private TypeUpdateResult invokeListener(TypeUpdateInfo updateInfo, InsnNode insn, InsnArg arg, ArgType candidateType) {
    BaseInvokeNode invoke = (BaseInvokeNode) insn;
    if (isAssign(invoke, arg)) {
        // TODO: implement backward type propagation (from result to instance)
        return SAME;
    }
    if (invoke.getInstanceArg() == arg) {
        IMethodDetails methodDetails = root.getMethodUtils().getMethodDetails(invoke);
        if (methodDetails == null) {
            return SAME;
        }
        TypeUtils typeUtils = root.getTypeUtils();
        Set<ArgType> knownTypeVars = typeUtils.getKnownTypeVarsAtMethod(updateInfo.getMth());
        Map<ArgType, ArgType> typeVarsMap = typeUtils.getTypeVariablesMapping(candidateType);
        ArgType returnType = methodDetails.getReturnType();
        List<ArgType> argTypes = methodDetails.getArgTypes();
        int argsCount = argTypes.size();
        if (typeVarsMap.isEmpty()) {
            // generics can't be resolved => use as is
            return applyInvokeTypes(updateInfo, invoke, argsCount, knownTypeVars, () -> returnType, argTypes::get);
        }
        // resolve types before apply
        return applyInvokeTypes(updateInfo, invoke, argsCount, knownTypeVars, () -> typeUtils.replaceTypeVariablesUsingMap(returnType, typeVarsMap), argNum -> typeUtils.replaceClassGenerics(candidateType, argTypes.get(argNum)));
    }
    return SAME;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) BaseInvokeNode(jadx.core.dex.instructions.BaseInvokeNode) TypeUtils(jadx.core.dex.nodes.utils.TypeUtils) IMethodDetails(jadx.core.dex.nodes.IMethodDetails)

Example 3 with TypeUtils

use of jadx.core.dex.nodes.utils.TypeUtils in project jadx by skylot.

the class MethodNode method initArguments.

private void initArguments(List<ArgType> args) {
    int pos = getArgsStartPos(args);
    TypeUtils typeUtils = root().getTypeUtils();
    if (accFlags.isStatic()) {
        thisArg = null;
    } else {
        ArgType thisClsType = typeUtils.expandTypeVariables(this, parentClass.getType());
        RegisterArg arg = InsnArg.reg(pos++, thisClsType);
        arg.add(AFlag.THIS);
        arg.add(AFlag.IMMUTABLE_TYPE);
        thisArg = arg;
    }
    if (args.isEmpty()) {
        argsList = Collections.emptyList();
        return;
    }
    argsList = new ArrayList<>(args.size());
    for (ArgType argType : args) {
        ArgType expandedType = typeUtils.expandTypeVariables(this, argType);
        RegisterArg regArg = InsnArg.reg(pos, expandedType);
        regArg.add(AFlag.METHOD_ARGUMENT);
        regArg.add(AFlag.IMMUTABLE_TYPE);
        argsList.add(regArg);
        pos += argType.getRegCount();
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) TypeUtils(jadx.core.dex.nodes.utils.TypeUtils)

Example 4 with TypeUtils

use of jadx.core.dex.nodes.utils.TypeUtils in project jadx by skylot.

the class SignatureProcessor method parseMethodSignature.

private void parseMethodSignature(MethodNode mth) {
    SignatureParser sp = SignatureParser.fromNode(mth);
    if (sp == null) {
        return;
    }
    try {
        List<ArgType> typeParameters = sp.consumeGenericTypeParameters();
        List<ArgType> parsedArgTypes = sp.consumeMethodArgs(mth.getMethodInfo().getArgsCount());
        ArgType parsedRetType = sp.consumeType();
        if (!validateInnerType(parsedRetType) || !validateInnerType(parsedArgTypes)) {
            mth.addWarnComment("Incorrect inner types in method signature: " + sp.getSignature());
            return;
        }
        // apply before expand args
        mth.updateTypeParameters(typeParameters);
        TypeUtils typeUtils = root.getTypeUtils();
        ArgType retType = typeUtils.expandTypeVariables(mth, parsedRetType);
        List<ArgType> argTypes = Utils.collectionMap(parsedArgTypes, t -> typeUtils.expandTypeVariables(mth, t));
        if (!validateAndApplyTypes(mth, sp, retType, argTypes)) {
            // bad types -> reset typed parameters
            mth.updateTypeParameters(Collections.emptyList());
        }
    } catch (Exception e) {
        mth.addWarnComment("Failed to parse method signature: " + sp.getSignature(), e);
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) TypeUtils(jadx.core.dex.nodes.utils.TypeUtils) SignatureParser(jadx.core.dex.nodes.parser.SignatureParser) JadxException(jadx.core.utils.exceptions.JadxException)

Example 5 with TypeUtils

use of jadx.core.dex.nodes.utils.TypeUtils in project jadx by skylot.

the class ClassNode method visitSuperTypes.

public void visitSuperTypes(BiConsumer<ArgType, ArgType> consumer) {
    TypeUtils typeUtils = root.getTypeUtils();
    ArgType thisType = this.getType();
    if (!superClass.equals(ArgType.OBJECT)) {
        consumer.accept(thisType, superClass);
        typeUtils.visitSuperTypes(superClass, consumer);
    }
    for (ArgType iface : interfaces) {
        consumer.accept(thisType, iface);
        typeUtils.visitSuperTypes(iface, consumer);
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) TypeUtils(jadx.core.dex.nodes.utils.TypeUtils)

Aggregations

ArgType (jadx.core.dex.instructions.args.ArgType)5 TypeUtils (jadx.core.dex.nodes.utils.TypeUtils)5 MethodInfo (jadx.core.dex.info.MethodInfo)1 BaseInvokeNode (jadx.core.dex.instructions.BaseInvokeNode)1 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)1 IMethodDetails (jadx.core.dex.nodes.IMethodDetails)1 SignatureParser (jadx.core.dex.nodes.parser.SignatureParser)1 JadxException (jadx.core.utils.exceptions.JadxException)1