■ ■ ■ ■ ■ ■
jadx-cli/src/main/java/jadx/cli/SingleClassMode.java
| 1 | + | package jadx.cli; |
| 2 | + | |
| 3 | + | import java.io.File; |
| 4 | + | import java.util.List; |
| 5 | + | import java.util.stream.Collectors; |
| 6 | + | |
| 7 | + | import org.slf4j.Logger; |
| 8 | + | import org.slf4j.LoggerFactory; |
| 9 | + | |
| 10 | + | import jadx.api.ICodeInfo; |
| 11 | + | import jadx.api.JadxDecompiler; |
| 12 | + | import jadx.core.dex.attributes.AFlag; |
| 13 | + | import jadx.core.dex.nodes.ClassNode; |
| 14 | + | import jadx.core.dex.visitors.SaveCode; |
| 15 | + | import jadx.core.utils.exceptions.JadxRuntimeException; |
| 16 | + | import jadx.core.utils.files.FileUtils; |
| 17 | + | |
| 18 | + | public class SingleClassMode { |
| 19 | + | private static final Logger LOG = LoggerFactory.getLogger(SingleClassMode.class); |
| 20 | + | |
| 21 | + | public static boolean process(JadxDecompiler jadx, JadxCLIArgs cliArgs) { |
| 22 | + | String singleClass = cliArgs.getSingleClass(); |
| 23 | + | String singleClassOutput = cliArgs.getSingleClassOutput(); |
| 24 | + | if (singleClass == null && singleClassOutput == null) { |
| 25 | + | return false; |
| 26 | + | } |
| 27 | + | ClassNode clsForProcess; |
| 28 | + | if (singleClass != null) { |
| 29 | + | clsForProcess = jadx.getRoot().resolveClass(singleClass); |
| 30 | + | if (clsForProcess == null) { |
| 31 | + | clsForProcess = jadx.getRoot().getClasses().stream() |
| 32 | + | .filter(cls -> cls.getClassInfo().getAliasFullName().equals(singleClass)) |
| 33 | + | .findFirst().orElse(null); |
| 34 | + | } |
| 35 | + | if (clsForProcess == null) { |
| 36 | + | throw new JadxRuntimeException("Input class not found: " + singleClass); |
| 37 | + | } |
| 38 | + | if (clsForProcess.contains(AFlag.DONT_GENERATE)) { |
| 39 | + | throw new JadxRuntimeException("Input class can't be saved by currect jadx settings (marked as DONT_GENERATE)"); |
| 40 | + | } |
| 41 | + | if (clsForProcess.isInner()) { |
| 42 | + | clsForProcess = clsForProcess.getTopParentClass(); |
| 43 | + | LOG.warn("Input class is inner, parent class will be saved: {}", clsForProcess.getFullName()); |
| 44 | + | } |
| 45 | + | } else { |
| 46 | + | // singleClassOutput is set |
| 47 | + | // expect only one class to be loaded |
| 48 | + | List<ClassNode> classes = jadx.getRoot().getClasses().stream() |
| 49 | + | .filter(c -> !c.isInner() && !c.contains(AFlag.DONT_GENERATE)) |
| 50 | + | .collect(Collectors.toList()); |
| 51 | + | int size = classes.size(); |
| 52 | + | if (size == 1) { |
| 53 | + | clsForProcess = classes.get(0); |
| 54 | + | } else { |
| 55 | + | throw new JadxRuntimeException("Found " + size + " classes, single class output can't be used"); |
| 56 | + | } |
| 57 | + | } |
| 58 | + | ICodeInfo codeInfo; |
| 59 | + | try { |
| 60 | + | codeInfo = clsForProcess.decompile(); |
| 61 | + | } catch (Exception e) { |
| 62 | + | throw new JadxRuntimeException("Class decompilation failed", e); |
| 63 | + | } |
| 64 | + | String fileExt = SaveCode.getFileExtension(jadx.getRoot()); |
| 65 | + | File out; |
| 66 | + | if (singleClassOutput == null) { |
| 67 | + | out = new File(jadx.getArgs().getOutDirSrc(), clsForProcess.getClassInfo().getAliasFullPath() + fileExt); |
| 68 | + | } else { |
| 69 | + | if (singleClassOutput.endsWith(fileExt)) { |
| 70 | + | // treat as file name |
| 71 | + | out = new File(singleClassOutput); |
| 72 | + | } else { |
| 73 | + | // treat as directory |
| 74 | + | out = new File(singleClassOutput, clsForProcess.getShortName() + fileExt); |
| 75 | + | } |
| 76 | + | } |
| 77 | + | File resultOut = FileUtils.prepareFile(out); |
| 78 | + | if (clsForProcess.getClassInfo().hasAlias()) { |
| 79 | + | LOG.info("Saving class '{}' (alias: '{}') to file '{}'", |
| 80 | + | clsForProcess.getClassInfo().getFullName(), clsForProcess.getFullName(), resultOut.getAbsolutePath()); |
| 81 | + | } else { |
| 82 | + | LOG.info("Saving class '{}' to file '{}'", clsForProcess.getFullName(), resultOut.getAbsolutePath()); |
| 83 | + | } |
| 84 | + | SaveCode.save(codeInfo.getCodeStr(), resultOut); |
| 85 | + | return true; |
| 86 | + | } |
| 87 | + | } |
| 88 | + | |