Search in sources :

Example 76 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class InsnGen method makeConstructor.

private void makeConstructor(ConstructorInsn insn, ICodeWriter code) throws CodegenException {
    ClassNode cls = mth.root().resolveClass(insn.getClassType());
    if (cls != null && cls.isAnonymous() && !fallback) {
        cls.ensureProcessed();
        inlineAnonymousConstructor(code, cls, insn);
        mth.getParentClass().addInlinedClass(cls);
        return;
    }
    if (insn.isSelf()) {
        throw new JadxRuntimeException("Constructor 'self' invoke must be removed!");
    }
    MethodNode callMth = mth.root().resolveMethod(insn.getCallMth());
    if (insn.isSuper()) {
        code.attachAnnotation(callMth);
        code.add("super");
    } else if (insn.isThis()) {
        code.attachAnnotation(callMth);
        code.add("this");
    } else {
        code.add("new ");
        if (callMth == null || callMth.contains(AFlag.DONT_GENERATE)) {
            // use class reference if constructor method is missing (default constructor)
            code.attachAnnotation(mth.root().resolveClass(insn.getCallMth().getDeclClass()));
        } else {
            code.attachAnnotation(callMth);
        }
        mgen.getClassGen().addClsName(code, insn.getClassType());
        GenericInfoAttr genericInfoAttr = insn.get(AType.GENERIC_INFO);
        if (genericInfoAttr != null) {
            code.add('<');
            if (genericInfoAttr.isExplicit()) {
                boolean first = true;
                for (ArgType type : genericInfoAttr.getGenericTypes()) {
                    if (!first) {
                        code.add(',');
                    } else {
                        first = false;
                    }
                    mgen.getClassGen().useType(code, type);
                }
            }
            code.add('>');
        }
    }
    generateMethodArguments(code, insn, 0, callMth);
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) ClassNode(jadx.core.dex.nodes.ClassNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) MethodNode(jadx.core.dex.nodes.MethodNode) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) GenericInfoAttr(jadx.core.dex.attributes.nodes.GenericInfoAttr)

Example 77 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class InsnGen method makeSimpleLambda.

private void makeSimpleLambda(ICodeWriter code, InvokeCustomNode customNode) {
    try {
        InsnNode callInsn = customNode.getCallInsn();
        MethodInfo implMthInfo = customNode.getImplMthInfo();
        int implArgsCount = implMthInfo.getArgsCount();
        if (implArgsCount == 0) {
            code.add("()");
        } else {
            code.add('(');
            int callArgsCount = callInsn.getArgsCount();
            int startArg = callArgsCount - implArgsCount;
            if (customNode.getHandleType() != MethodHandleType.INVOKE_STATIC && customNode.getArgsCount() > 0 && customNode.getArg(0).isThis()) {
                callInsn.getArg(0).add(AFlag.THIS);
            }
            if (startArg >= 0) {
                for (int i = startArg; i < callArgsCount; i++) {
                    if (i != startArg) {
                        code.add(", ");
                    }
                    addArg(code, callInsn.getArg(i));
                }
            } else {
                code.add("/* ERROR: " + startArg + " */");
            }
            code.add(')');
        }
        code.add(" -> {");
        if (fallback) {
            code.add(" // ").add(implMthInfo.toString());
        }
        code.incIndent();
        code.startLine();
        if (!implMthInfo.getReturnType().isVoid()) {
            code.add("return ");
        }
        makeInsn(callInsn, code, Flags.INLINE);
        code.add(";");
        code.decIndent();
        code.startLine('}');
    } catch (Exception e) {
        throw new JadxRuntimeException("Failed to generate 'invoke-custom' instruction: " + e.getMessage(), e);
    }
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) MethodInfo(jadx.core.dex.info.MethodInfo) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) CodegenException(jadx.core.utils.exceptions.CodegenException) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 78 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class ClsSet method save.

public void save(Path path) throws IOException {
    FileUtils.makeDirsForFile(path);
    String outputName = path.getFileName().toString();
    if (outputName.endsWith(CLST_EXTENSION)) {
        try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(path))) {
            save(outputStream);
        }
    } else if (outputName.endsWith(".jar")) {
        Path temp = FileUtils.createTempFile(".zip");
        Files.copy(path, temp, StandardCopyOption.REPLACE_EXISTING);
        try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(path));
            ZipInputStream in = new ZipInputStream(Files.newInputStream(temp))) {
            String clst = CLST_PATH;
            boolean clstReplaced = false;
            ZipEntry entry = in.getNextEntry();
            while (entry != null) {
                String entryName = entry.getName();
                ZipEntry copyEntry = new ZipEntry(entryName);
                // preserve modified time
                copyEntry.setLastModifiedTime(entry.getLastModifiedTime());
                out.putNextEntry(copyEntry);
                if (entryName.equals(clst)) {
                    save(out);
                    clstReplaced = true;
                } else {
                    FileUtils.copyStream(in, out);
                }
                entry = in.getNextEntry();
            }
            if (!clstReplaced) {
                out.putNextEntry(new ZipEntry(clst));
                save(out);
            }
        }
    } else {
        throw new JadxRuntimeException("Unknown file format: " + outputName);
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 79 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class JsonMappingGen method dump.

public static void dump(RootNode root) {
    JsonMapping mapping = new JsonMapping();
    fillMapping(mapping, root);
    JadxArgs args = root.getArgs();
    File outDirSrc = args.getOutDirSrc().getAbsoluteFile();
    File mappingFile = new File(outDirSrc, "mapping.json");
    FileUtils.makeDirsForFile(mappingFile);
    try (Writer writer = new FileWriter(mappingFile)) {
        GSON.toJson(mapping, writer);
        LOG.info("Save mappings to {}", mappingFile.getAbsolutePath());
    } catch (Exception e) {
        throw new JadxRuntimeException("Failed to save mapping json", e);
    }
}
Also used : FileWriter(java.io.FileWriter) JadxArgs(jadx.api.JadxArgs) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) JsonMapping(jadx.core.codegen.json.mapping.JsonMapping) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 80 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class InlineMethods method processInvokeInsn.

private void processInvokeInsn(MethodNode mth, BlockNode block, InvokeNode insn) {
    IMethodDetails callMthDetails = insn.get(AType.METHOD_DETAILS);
    if (!(callMthDetails instanceof MethodNode)) {
        return;
    }
    MethodNode callMth = (MethodNode) callMthDetails;
    try {
        // TODO: sort inner classes process order by dependencies!
        MethodInlineAttr mia = MarkMethodsForInline.process(callMth);
        if (mia == null) {
            // method not yet loaded => will retry at codegen stage
            callMth.getParentClass().reloadAtCodegenStage();
            return;
        }
        if (mia.notNeeded()) {
            return;
        }
        inlineMethod(mth, callMth, mia, block, insn);
    } catch (Exception e) {
        throw new JadxRuntimeException("Failed to process method for inline: " + callMth, e);
    }
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode) MethodInlineAttr(jadx.core.dex.attributes.nodes.MethodInlineAttr) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) JadxException(jadx.core.utils.exceptions.JadxException) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IMethodDetails(jadx.core.dex.nodes.IMethodDetails)

Aggregations

JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)115 BlockNode (jadx.core.dex.nodes.BlockNode)25 ArrayList (java.util.ArrayList)25 InsnNode (jadx.core.dex.nodes.InsnNode)24 ArgType (jadx.core.dex.instructions.args.ArgType)20 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)17 BitSet (java.util.BitSet)11 ClassNode (jadx.core.dex.nodes.ClassNode)10 MethodNode (jadx.core.dex.nodes.MethodNode)9 InsnArg (jadx.core.dex.instructions.args.InsnArg)8 SSAVar (jadx.core.dex.instructions.args.SSAVar)8 IOException (java.io.IOException)8 List (java.util.List)8 File (java.io.File)7 IRegion (jadx.core.dex.nodes.IRegion)6 Path (java.nio.file.Path)6 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)5 PhiInsn (jadx.core.dex.instructions.PhiInsn)5 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)5 FieldNode (jadx.core.dex.nodes.FieldNode)5