| 1 | + | package jadx.core.codegen.json; |
| 2 | + | |
| 3 | + | import java.util.ArrayList; |
| 4 | + | import java.util.Collections; |
| 5 | + | import java.util.List; |
| 6 | + | import java.util.Map; |
| 7 | + | import java.util.Objects; |
| 8 | + | |
| 9 | + | import org.jetbrains.annotations.Nullable; |
| 10 | + | |
| 11 | + | import com.google.gson.FieldNamingPolicy; |
| 12 | + | import com.google.gson.Gson; |
| 13 | + | import com.google.gson.GsonBuilder; |
| 14 | + | |
| 15 | + | import jadx.api.CodePosition; |
| 16 | + | import jadx.api.JadxArgs; |
| 17 | + | import jadx.core.codegen.ClassGen; |
| 18 | + | import jadx.core.codegen.CodeWriter; |
| 19 | + | import jadx.core.codegen.MethodGen; |
| 20 | + | import jadx.core.codegen.json.cls.JsonClass; |
| 21 | + | import jadx.core.codegen.json.cls.JsonCodeLine; |
| 22 | + | import jadx.core.codegen.json.cls.JsonField; |
| 23 | + | import jadx.core.codegen.json.cls.JsonMethod; |
| 24 | + | import jadx.core.dex.attributes.AFlag; |
| 25 | + | import jadx.core.dex.info.ClassInfo; |
| 26 | + | import jadx.core.dex.instructions.args.ArgType; |
| 27 | + | import jadx.core.dex.nodes.ClassNode; |
| 28 | + | import jadx.core.dex.nodes.FieldNode; |
| 29 | + | import jadx.core.dex.nodes.InsnNode; |
| 30 | + | import jadx.core.dex.nodes.MethodNode; |
| 31 | + | import jadx.core.dex.nodes.RootNode; |
| 32 | + | import jadx.core.utils.CodeGenUtils; |
| 33 | + | import jadx.core.utils.Utils; |
| 34 | + | import jadx.core.utils.exceptions.JadxRuntimeException; |
| 35 | + | |
| 36 | + | public class JsonCodeGen { |
| 37 | + | |
| 38 | + | private static final Gson GSON = new GsonBuilder() |
| 39 | + | .setPrettyPrinting() |
| 40 | + | .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) |
| 41 | + | .disableHtmlEscaping() |
| 42 | + | .create(); |
| 43 | + | |
| 44 | + | private final ClassNode cls; |
| 45 | + | private final JadxArgs args; |
| 46 | + | private final RootNode root; |
| 47 | + | |
| 48 | + | public JsonCodeGen(ClassNode cls) { |
| 49 | + | this.cls = cls; |
| 50 | + | this.root = cls.root(); |
| 51 | + | this.args = root.getArgs(); |
| 52 | + | } |
| 53 | + | |
| 54 | + | public String process() { |
| 55 | + | JsonClass jsonCls = processCls(cls, null); |
| 56 | + | return GSON.toJson(jsonCls); |
| 57 | + | } |
| 58 | + | |
| 59 | + | private JsonClass processCls(ClassNode cls, @Nullable ClassGen parentCodeGen) { |
| 60 | + | ClassGen classGen; |
| 61 | + | if (parentCodeGen == null) { |
| 62 | + | classGen = new ClassGen(cls, args); |
| 63 | + | } else { |
| 64 | + | classGen = new ClassGen(cls, parentCodeGen); |
| 65 | + | } |
| 66 | + | ClassInfo classInfo = cls.getClassInfo(); |
| 67 | + | |
| 68 | + | JsonClass jsonCls = new JsonClass(); |
| 69 | + | jsonCls.setPkg(classInfo.getAliasPkg()); |
| 70 | + | jsonCls.setDex(cls.dex().getDexFile().getName()); |
| 71 | + | jsonCls.setName(classInfo.getFullName()); |
| 72 | + | if (classInfo.hasAlias()) { |
| 73 | + | jsonCls.setAlias(classInfo.getAliasFullName()); |
| 74 | + | } |
| 75 | + | jsonCls.setType(getClassTypeStr(cls)); |
| 76 | + | jsonCls.setAccessFlags(cls.getAccessFlags().rawValue()); |
| 77 | + | if (!Objects.equals(cls.getSuperClass(), ArgType.OBJECT)) { |
| 78 | + | jsonCls.setSuperClass(getTypeAlias(cls.getSuperClass())); |
| 79 | + | } |
| 80 | + | if (!cls.getInterfaces().isEmpty()) { |
| 81 | + | jsonCls.setInterfaces(Utils.collectionMap(cls.getInterfaces(), this::getTypeAlias)); |
| 82 | + | } |
| 83 | + | |
| 84 | + | CodeWriter cw = new CodeWriter(); |
| 85 | + | CodeGenUtils.addComments(cw, cls); |
| 86 | + | classGen.insertDecompilationProblems(cw, cls); |
| 87 | + | classGen.addClassDeclaration(cw); |
| 88 | + | jsonCls.setDeclaration(cw.finish().toString()); |
| 89 | + | |
| 90 | + | addFields(cls, jsonCls, classGen); |
| 91 | + | addMethods(cls, jsonCls, classGen); |
| 92 | + | addInnerClasses(cls, jsonCls, classGen); |
| 93 | + | |
| 94 | + | if (!cls.getClassInfo().isInner()) { |
| 95 | + | List<String> imports = Utils.collectionMap(classGen.getImports(), ClassInfo::getAliasFullName); |
| 96 | + | Collections.sort(imports); |
| 97 | + | jsonCls.setImports(imports); |
| 98 | + | } |
| 99 | + | return jsonCls; |
| 100 | + | } |
| 101 | + | |
| 102 | + | private void addInnerClasses(ClassNode cls, JsonClass jsonCls, ClassGen classGen) { |
| 103 | + | List<ClassNode> innerClasses = cls.getInnerClasses(); |
| 104 | + | if (innerClasses.isEmpty()) { |
| 105 | + | return; |
| 106 | + | } |
| 107 | + | jsonCls.setInnerClasses(new ArrayList<>(innerClasses.size())); |
| 108 | + | for (ClassNode innerCls : innerClasses) { |
| 109 | + | if (innerCls.contains(AFlag.DONT_GENERATE)) { |
| 110 | + | continue; |
| 111 | + | } |
| 112 | + | JsonClass innerJsonCls = processCls(innerCls, classGen); |
| 113 | + | jsonCls.getInnerClasses().add(innerJsonCls); |
| 114 | + | } |
| 115 | + | } |
| 116 | + | |
| 117 | + | private void addFields(ClassNode cls, JsonClass jsonCls, ClassGen classGen) { |
| 118 | + | jsonCls.setFields(new ArrayList<>()); |
| 119 | + | for (FieldNode field : cls.getFields()) { |
| 120 | + | if (field.contains(AFlag.DONT_GENERATE)) { |
| 121 | + | continue; |
| 122 | + | } |
| 123 | + | JsonField jsonField = new JsonField(); |
| 124 | + | jsonField.setName(field.getName()); |
| 125 | + | if (field.getFieldInfo().hasAlias()) { |
| 126 | + | jsonField.setAlias(field.getAlias()); |
| 127 | + | } |
| 128 | + | |
| 129 | + | CodeWriter cw = new CodeWriter(); |
| 130 | + | classGen.addField(cw, field); |
| 131 | + | jsonField.setDeclaration(cw.finish().toString()); |
| 132 | + | jsonField.setAccessFlags(field.getAccessFlags().rawValue()); |
| 133 | + | |
| 134 | + | jsonCls.getFields().add(jsonField); |
| 135 | + | } |
| 136 | + | } |
| 137 | + | |
| 138 | + | private void addMethods(ClassNode cls, JsonClass jsonCls, ClassGen classGen) { |
| 139 | + | jsonCls.setMethods(new ArrayList<>()); |
| 140 | + | for (MethodNode mth : cls.getMethods()) { |
| 141 | + | if (mth.contains(AFlag.DONT_GENERATE)) { |
| 142 | + | continue; |
| 143 | + | } |
| 144 | + | JsonMethod jsonMth = new JsonMethod(); |
| 145 | + | jsonMth.setName(mth.getName()); |
| 146 | + | if (mth.getMethodInfo().hasAlias()) { |
| 147 | + | jsonMth.setAlias(mth.getAlias()); |
| 148 | + | } |
| 149 | + | jsonMth.setSignature(mth.getMethodInfo().getShortId()); |
| 150 | + | jsonMth.setReturnType(getTypeAlias(mth.getReturnType())); |
| 151 | + | jsonMth.setArguments(Utils.collectionMap(mth.getMethodInfo().getArgumentsTypes(), this::getTypeAlias)); |
| 152 | + | |
| 153 | + | MethodGen mthGen = new MethodGen(classGen, mth); |
| 154 | + | CodeWriter cw = new CodeWriter(); |
| 155 | + | mthGen.addDefinition(cw); |
| 156 | + | jsonMth.setDeclaration(cw.finish().toString()); |
| 157 | + | jsonMth.setAccessFlags(mth.getAccessFlags().rawValue()); |
| 158 | + | jsonMth.setLines(fillMthCode(mth, mthGen)); |
| 159 | + | jsonMth.setOffset("0x" + Long.toHexString(mth.getMethodCodeOffset())); |
| 160 | + | jsonCls.getMethods().add(jsonMth); |
| 161 | + | } |
| 162 | + | } |
| 163 | + | |
| 164 | + | private List<JsonCodeLine> fillMthCode(MethodNode mth, MethodGen mthGen) { |
| 165 | + | if (mth.isNoCode()) { |
| 166 | + | return Collections.emptyList(); |
| 167 | + | } |
| 168 | + | |
| 169 | + | CodeWriter code = new CodeWriter(); |
| 170 | + | try { |
| 171 | + | mthGen.addInstructions(code); |
| 172 | + | } catch (Exception e) { |
| 173 | + | throw new JadxRuntimeException("Method generation error", e); |
| 174 | + | } |
| 175 | + | code.finish(); |
| 176 | + | String codeStr = code.toString(); |
| 177 | + | if (codeStr.isEmpty()) { |
| 178 | + | return Collections.emptyList(); |
| 179 | + | } |
| 180 | + | |
| 181 | + | String[] lines = codeStr.split(CodeWriter.NL); |
| 182 | + | Map<Integer, Integer> lineMapping = code.getLineMapping(); |
| 183 | + | Map<CodePosition, Object> annotations = code.getAnnotations(); |
| 184 | + | long mthCodeOffset = mth.getMethodCodeOffset() + 16; |
| 185 | + | |
| 186 | + | int linesCount = lines.length; |
| 187 | + | List<JsonCodeLine> codeLines = new ArrayList<>(linesCount); |
| 188 | + | for (int i = 0; i < linesCount; i++) { |
| 189 | + | String codeLine = lines[i]; |
| 190 | + | int line = i + 2; |
| 191 | + | JsonCodeLine jsonCodeLine = new JsonCodeLine(); |
| 192 | + | jsonCodeLine.setCode(codeLine); |
| 193 | + | jsonCodeLine.setSourceLine(lineMapping.get(line)); |
| 194 | + | Object obj = annotations.get(new CodePosition(line, 0)); |
| 195 | + | if (obj instanceof InsnNode) { |
| 196 | + | int offset = ((InsnNode) obj).getOffset(); |
| 197 | + | jsonCodeLine.setOffset("0x" + Long.toHexString(mthCodeOffset + offset * 2)); |
| 198 | + | } |
| 199 | + | codeLines.add(jsonCodeLine); |
| 200 | + | } |
| 201 | + | return codeLines; |
| 202 | + | } |
| 203 | + | |
| 204 | + | private String getTypeAlias(ArgType clsType) { |
| 205 | + | if (Objects.equals(clsType, ArgType.OBJECT)) { |
| 206 | + | return ArgType.OBJECT.getObject(); |
| 207 | + | } |
| 208 | + | if (clsType.isObject()) { |
| 209 | + | ClassInfo classInfo = ClassInfo.fromType(root, clsType); |
| 210 | + | return classInfo.getAliasFullName(); |
| 211 | + | } |
| 212 | + | return clsType.toString(); |
| 213 | + | } |
| 214 | + | |
| 215 | + | private String getClassTypeStr(ClassNode cls) { |
| 216 | + | if (cls.isEnum()) { |
| 217 | + | return "enum"; |
| 218 | + | } |
| 219 | + | if (cls.getAccessFlags().isInterface()) { |
| 220 | + | return "interface"; |
| 221 | + | } |
| 222 | + | return "class"; |
| 223 | + | } |
| 224 | + | } |
| 225 | + | |