Search in sources :

Example 1 with CodeVar

use of jadx.core.dex.instructions.args.CodeVar in project jadx by skylot.

the class InitCodeVariables method initCodeVar.

public static void initCodeVar(SSAVar ssaVar) {
    if (ssaVar.isCodeVarSet()) {
        return;
    }
    CodeVar codeVar = new CodeVar();
    RegisterArg assignArg = ssaVar.getAssign();
    if (assignArg.contains(AFlag.THIS)) {
        codeVar.setName(RegisterArg.THIS_ARG_NAME);
        codeVar.setThis(true);
    }
    if (assignArg.contains(AFlag.METHOD_ARGUMENT) || assignArg.contains(AFlag.CUSTOM_DECLARE)) {
        codeVar.setDeclared(true);
    }
    setCodeVar(ssaVar, codeVar);
}
Also used : CodeVar(jadx.core.dex.instructions.args.CodeVar) RegisterArg(jadx.core.dex.instructions.args.RegisterArg)

Example 2 with CodeVar

use of jadx.core.dex.instructions.args.CodeVar in project jadx by skylot.

the class VarRef method get.

@Nullable
public static VarRef get(MethodNode mth, RegisterArg reg) {
    SSAVar ssaVar = reg.getSVar();
    if (ssaVar == null) {
        return null;
    }
    CodeVar codeVar = ssaVar.getCodeVar();
    VarRef cachedVarRef = codeVar.getCachedVarRef();
    if (cachedVarRef != null) {
        if (cachedVarRef.getName() == null) {
            cachedVarRef.setName(codeVar.getName());
        }
        return cachedVarRef;
    }
    VarRef newVarRef = new VarRef(mth, ssaVar);
    codeVar.setCachedVarRef(newVarRef);
    return newVarRef;
}
Also used : CodeVar(jadx.core.dex.instructions.args.CodeVar) SSAVar(jadx.core.dex.instructions.args.SSAVar) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with CodeVar

use of jadx.core.dex.instructions.args.CodeVar in project jadx by skylot.

the class ProcessVariables method collectCodeVars.

private List<CodeVar> collectCodeVars(MethodNode mth) {
    Map<CodeVar, List<SSAVar>> codeVars = new LinkedHashMap<>();
    for (SSAVar ssaVar : mth.getSVars()) {
        if (ssaVar.getCodeVar().isThis()) {
            continue;
        }
        CodeVar codeVar = ssaVar.getCodeVar();
        List<SSAVar> list = codeVars.computeIfAbsent(codeVar, k -> new ArrayList<>());
        list.add(ssaVar);
    }
    for (Entry<CodeVar, List<SSAVar>> entry : codeVars.entrySet()) {
        CodeVar codeVar = entry.getKey();
        List<SSAVar> list = entry.getValue();
        for (SSAVar ssaVar : list) {
            CodeVar localCodeVar = ssaVar.getCodeVar();
            codeVar.mergeFlagsFrom(localCodeVar);
        }
        if (list.size() > 1) {
            for (SSAVar ssaVar : list) {
                ssaVar.setCodeVar(codeVar);
            }
        }
        codeVar.setSsaVars(list);
    }
    return new ArrayList<>(codeVars.keySet());
}
Also used : CodeVar(jadx.core.dex.instructions.args.CodeVar) SSAVar(jadx.core.dex.instructions.args.SSAVar) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with CodeVar

use of jadx.core.dex.instructions.args.CodeVar in project jadx by skylot.

the class MethodGen method addMethodArguments.

private void addMethodArguments(ICodeWriter code, List<RegisterArg> args) {
    AnnotationMethodParamsAttr paramsAnnotation = mth.get(JadxAttrType.ANNOTATION_MTH_PARAMETERS);
    int i = 0;
    Iterator<RegisterArg> it = args.iterator();
    while (it.hasNext()) {
        RegisterArg mthArg = it.next();
        SSAVar ssaVar = mthArg.getSVar();
        CodeVar var;
        if (ssaVar == null) {
            // abstract or interface methods
            var = CodeVar.fromMthArg(mthArg, classGen.isFallbackMode());
        } else {
            var = ssaVar.getCodeVar();
        }
        // add argument annotation
        if (paramsAnnotation != null) {
            annotationGen.addForParameter(code, paramsAnnotation, i);
        }
        if (var.isFinal()) {
            code.add("final ");
        }
        ArgType argType;
        ArgType varType = var.getType();
        if (varType == null || varType == ArgType.UNKNOWN) {
            // occur on decompilation errors
            argType = mthArg.getInitType();
        } else {
            argType = varType;
        }
        if (!it.hasNext() && mth.getAccessFlags().isVarArgs()) {
            // change last array argument to varargs
            if (argType.isArray()) {
                ArgType elType = argType.getArrayElement();
                classGen.useType(code, elType);
                code.add("...");
            } else {
                mth.addWarnComment("Last argument in varargs method is not array: " + var);
                classGen.useType(code, argType);
            }
        } else {
            classGen.useType(code, argType);
        }
        code.add(' ');
        String varName = nameGen.assignArg(var);
        if (code.isMetadataSupported() && ssaVar != null) /* for fallback mode */
        {
            code.attachDefinition(VarDeclareRef.get(mth, var));
        }
        code.add(varName);
        i++;
        if (it.hasNext()) {
            code.add(", ");
        }
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) CodeVar(jadx.core.dex.instructions.args.CodeVar) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) SSAVar(jadx.core.dex.instructions.args.SSAVar) AnnotationMethodParamsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr)

Example 5 with CodeVar

use of jadx.core.dex.instructions.args.CodeVar in project jadx by skylot.

the class RegionGen method makeCatchBlock.

private void makeCatchBlock(ICodeWriter code, ExceptionHandler handler) throws CodegenException {
    IContainer region = handler.getHandlerRegion();
    if (region == null) {
        return;
    }
    code.startLine("} catch (");
    if (handler.isCatchAll()) {
        useClass(code, ArgType.THROWABLE);
    } else {
        Iterator<ClassInfo> it = handler.getCatchTypes().iterator();
        if (it.hasNext()) {
            useClass(code, it.next());
        }
        while (it.hasNext()) {
            code.add(" | ");
            useClass(code, it.next());
        }
    }
    code.add(' ');
    InsnArg arg = handler.getArg();
    if (arg == null) {
        // throwing exception is too late at this point
        code.add("unknown");
    } else if (arg instanceof RegisterArg) {
        CodeVar codeVar = ((RegisterArg) arg).getSVar().getCodeVar();
        if (code.isMetadataSupported()) {
            code.attachDefinition(VarDeclareRef.get(mth, codeVar));
        }
        code.add(mgen.getNameGen().assignArg(codeVar));
    } else if (arg instanceof NamedArg) {
        code.add(mgen.getNameGen().assignNamedArg((NamedArg) arg));
    } else {
        throw new JadxRuntimeException("Unexpected arg type in catch block: " + arg + ", class: " + arg.getClass().getSimpleName());
    }
    code.add(") {");
    InsnCodeOffset.attach(code, handler.getHandlerOffset());
    CodeGenUtils.addCodeComments(code, mth, handler.getHandlerBlock());
    makeRegionIndent(code, region);
}
Also used : CodeVar(jadx.core.dex.instructions.args.CodeVar) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) InsnArg(jadx.core.dex.instructions.args.InsnArg) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) NamedArg(jadx.core.dex.instructions.args.NamedArg) IContainer(jadx.core.dex.nodes.IContainer) ClassInfo(jadx.core.dex.info.ClassInfo)

Aggregations

CodeVar (jadx.core.dex.instructions.args.CodeVar)10 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)5 SSAVar (jadx.core.dex.instructions.args.SSAVar)4 ArgType (jadx.core.dex.instructions.args.ArgType)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 AnnotationMethodParamsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr)1 MethodParametersAttr (jadx.api.plugins.input.data.attributes.types.MethodParametersAttr)1 DeclareVariablesAttr (jadx.core.dex.attributes.nodes.DeclareVariablesAttr)1 ClassInfo (jadx.core.dex.info.ClassInfo)1 InsnArg (jadx.core.dex.instructions.args.InsnArg)1 NamedArg (jadx.core.dex.instructions.args.NamedArg)1 IContainer (jadx.core.dex.nodes.IContainer)1 TypeCompare (jadx.core.dex.visitors.typeinference.TypeCompare)1 TypeCompareEnum (jadx.core.dex.visitors.typeinference.TypeCompareEnum)1 JadxException (jadx.core.utils.exceptions.JadxException)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 Nullable (org.jetbrains.annotations.Nullable)1