ClassGen.java 15.3 KB
Newer Older
S
Skylot 已提交
1 2
package jadx.core.codegen;

3
import jadx.api.IJadxArgs;
S
Skylot 已提交
4
import jadx.core.Consts;
5
import jadx.core.deobf.Deobfuscator;
S
Skylot 已提交
6 7
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
S
Skylot 已提交
8
import jadx.core.dex.attributes.AttrNode;
S
Skylot 已提交
9 10 11
import jadx.core.dex.attributes.nodes.EnumClassAttr;
import jadx.core.dex.attributes.nodes.EnumClassAttr.EnumField;
import jadx.core.dex.attributes.nodes.SourceFileAttr;
S
Skylot 已提交
12 13 14 15
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg;
16
import jadx.core.dex.instructions.args.PrimitiveType;
S
Skylot 已提交
17
import jadx.core.dex.nodes.ClassNode;
S
Skylot 已提交
18
import jadx.core.dex.nodes.DexNode;
S
Skylot 已提交
19 20 21 22 23 24
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.parser.FieldValueAttr;
import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.CodegenException;
S
Skylot 已提交
25 26 27

import java.util.ArrayList;
import java.util.Collections;
S
Skylot 已提交
28
import java.util.Comparator;
S
Skylot 已提交
29 30 31
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
32 33
import java.util.Map;
import java.util.Map.Entry;
S
Skylot 已提交
34 35
import java.util.Set;

36 37 38
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

S
Skylot 已提交
39 40 41
import com.android.dx.rop.code.AccessFlags;

public class ClassGen {
42 43
	private static final Logger LOG = LoggerFactory.getLogger(ClassGen.class);

S
Skylot 已提交
44 45 46 47 48 49 50
	public static final Comparator<MethodNode> METHOD_LINE_COMPARATOR = new Comparator<MethodNode>() {
		@Override
		public int compare(MethodNode a, MethodNode b) {
			return Utils.compare(a.getSourceLine(), b.getSourceLine());
		}
	};

S
Skylot 已提交
51 52 53 54 55
	private final ClassNode cls;
	private final ClassGen parentGen;
	private final AnnotationGen annotationGen;
	private final boolean fallback;

56 57
	private boolean showInconsistentCode = false;

S
Skylot 已提交
58
	private final Set<ClassInfo> imports = new HashSet<ClassInfo>();
S
Skylot 已提交
59
	private int clsDeclLine;
S
Skylot 已提交
60

61 62 63 64 65
	public ClassGen(ClassNode cls, ClassGen parentClsGen, IJadxArgs jadxArgs) {
		this(cls, parentClsGen, jadxArgs.isFallbackMode());
		this.showInconsistentCode = jadxArgs.isShowInconsistentCode();
	}

S
Skylot 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	public ClassGen(ClassNode cls, ClassGen parentClsGen, boolean fallback) {
		this.cls = cls;
		this.parentGen = parentClsGen;
		this.fallback = fallback;

		this.annotationGen = new AnnotationGen(cls, this);
	}

	public ClassNode getClassNode() {
		return cls;
	}

	public CodeWriter makeClass() throws CodegenException {
		CodeWriter clsBody = new CodeWriter();
		addClassCode(clsBody);

		CodeWriter clsCode = new CodeWriter();
		if (!"".equals(cls.getPackage())) {
84
			clsCode.add("package ").add(Deobfuscator.instance().getPackageName(cls.getPackage())).add(';');
85
			clsCode.newLine();
S
Skylot 已提交
86
		}
S
Skylot 已提交
87 88 89 90
		int importsCount = imports.size();
		if (importsCount != 0) {
			List<String> sortImports = new ArrayList<String>(importsCount);
			for (ClassInfo ic : imports) {
91
				sortImports.add(Deobfuscator.instance().getClassFullName(ic));
S
Skylot 已提交
92
			}
S
Skylot 已提交
93 94 95
			Collections.sort(sortImports);

			for (String imp : sortImports) {
96
				clsCode.startLine("import ").add(imp).add(';');
S
Skylot 已提交
97
			}
98
			clsCode.newLine();
S
Skylot 已提交
99 100 101 102 103 104 105 106 107

			sortImports.clear();
			imports.clear();
		}
		clsCode.add(clsBody);
		return clsCode;
	}

	public void addClassCode(CodeWriter code) throws CodegenException {
S
Skylot 已提交
108
		if (cls.contains(AFlag.DONT_GENERATE)) {
S
Skylot 已提交
109
			return;
S
Skylot 已提交
110
		}
S
Skylot 已提交
111
		if (cls.contains(AFlag.INCONSISTENT_CODE)) {
S
Skylot 已提交
112
			code.startLine("// jadx: inconsistent code");
S
Skylot 已提交
113
		}
S
Skylot 已提交
114 115
		addClassDeclaration(code);
		addClassBody(code);
S
Skylot 已提交
116 117
	}

S
Skylot 已提交
118
	public void addClassDeclaration(CodeWriter clsCode) {
S
Skylot 已提交
119 120 121 122
		AccessInfo af = cls.getAccessFlags();
		if (af.isInterface()) {
			af = af.remove(AccessFlags.ACC_ABSTRACT);
		} else if (af.isEnum()) {
S
Skylot 已提交
123 124 125
			af = af.remove(AccessFlags.ACC_FINAL)
					.remove(AccessFlags.ACC_ABSTRACT)
					.remove(AccessFlags.ACC_STATIC);
S
Skylot 已提交
126 127
		}

S
Skylot 已提交
128 129 130 131 132
		// 'static' modifier not allowed for top classes (not inner)
		if (!cls.getClassInfo().isInner()) {
			af = af.remove(AccessFlags.ACC_STATIC);
		}

S
Skylot 已提交
133
		annotationGen.addForClass(clsCode);
S
Skylot 已提交
134
		insertSourceFileInfo(clsCode, cls);
S
Skylot 已提交
135 136
		clsCode.startLine(af.makeString());
		if (af.isInterface()) {
S
Skylot 已提交
137
			if (af.isAnnotation()) {
S
Skylot 已提交
138
				clsCode.add('@');
S
Skylot 已提交
139
			}
S
Skylot 已提交
140 141 142 143 144 145
			clsCode.add("interface ");
		} else if (af.isEnum()) {
			clsCode.add("enum ");
		} else {
			clsCode.add("class ");
		}
146
		clsCode.add(Deobfuscator.instance().getClassShortName(cls));
S
Skylot 已提交
147

S
Skylot 已提交
148
		addGenericMap(clsCode, cls.getGenericMap());
149 150 151
		clsCode.add(' ');

		ClassInfo sup = cls.getSuperClass();
S
Skylot 已提交
152 153 154
		if (sup != null
				&& !sup.getFullName().equals(Consts.CLASS_OBJECT)
				&& !sup.getFullName().equals(Consts.CLASS_ENUM)) {
155 156 157
			clsCode.add("extends ");
			useClass(clsCode, sup);
			clsCode.add(' ');
S
Skylot 已提交
158 159
		}

S
Skylot 已提交
160
		if (!cls.getInterfaces().isEmpty() && !af.isAnnotation()) {
S
Skylot 已提交
161
			if (cls.getAccessFlags().isInterface()) {
162
				clsCode.add("extends ");
S
Skylot 已提交
163
			} else {
164
				clsCode.add("implements ");
S
Skylot 已提交
165
			}
S
Skylot 已提交
166
			for (Iterator<ClassInfo> it = cls.getInterfaces().iterator(); it.hasNext(); ) {
S
Skylot 已提交
167
				ClassInfo interf = it.next();
168
				useClass(clsCode, interf);
S
Skylot 已提交
169
				if (it.hasNext()) {
S
Skylot 已提交
170
					clsCode.add(", ");
S
Skylot 已提交
171
				}
S
Skylot 已提交
172
			}
S
Skylot 已提交
173
			if (!cls.getInterfaces().isEmpty()) {
174
				clsCode.add(' ');
S
Skylot 已提交
175
			}
176
		}
177
		clsCode.attachDefinition(cls);
178 179
	}

S
Skylot 已提交
180
	public boolean addGenericMap(CodeWriter code, Map<ArgType, List<ArgType>> gmap) {
S
Skylot 已提交
181
		if (gmap == null || gmap.isEmpty()) {
S
Skylot 已提交
182
			return false;
S
Skylot 已提交
183
		}
184 185 186 187 188 189 190 191
		code.add('<');
		int i = 0;
		for (Entry<ArgType, List<ArgType>> e : gmap.entrySet()) {
			ArgType type = e.getKey();
			List<ArgType> list = e.getValue();
			if (i != 0) {
				code.add(", ");
			}
192 193 194
			if (type.isGenericType()) {
				code.add(type.getObject());
			} else {
195
				useClass(code, ClassInfo.fromType(cls.dex(), type));
196
			}
197 198
			if (list != null && !list.isEmpty()) {
				code.add(" extends ");
S
Skylot 已提交
199
				for (Iterator<ArgType> it = list.iterator(); it.hasNext(); ) {
200
					ArgType g = it.next();
201 202 203
					if (g.isGenericType()) {
						code.add(g.getObject());
					} else {
204
						useClass(code, ClassInfo.fromType(cls.dex(), g));
205
					}
206 207 208 209 210 211
					if (it.hasNext()) {
						code.add(" & ");
					}
				}
			}
			i++;
S
Skylot 已提交
212
		}
213
		code.add('>');
S
Skylot 已提交
214
		return true;
S
Skylot 已提交
215 216
	}

S
Skylot 已提交
217
	public void addClassBody(CodeWriter clsCode) throws CodegenException {
218
		clsCode.add('{');
S
Skylot 已提交
219 220 221 222 223 224
		clsDeclLine = clsCode.getLine();
		clsCode.incIndent();
		addFields(clsCode);
		addInnerClasses(clsCode, cls);
		addMethods(clsCode);
		clsCode.decIndent();
225
		clsCode.startLine('}');
S
Skylot 已提交
226 227
	}

S
Skylot 已提交
228
	private void addInnerClasses(CodeWriter code, ClassNode cls) throws CodegenException {
S
Skylot 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241
		for (ClassNode innerCls : cls.getInnerClasses()) {
			if (innerCls.contains(AFlag.DONT_GENERATE)
					|| innerCls.isAnonymous()) {
				continue;
			}
			ClassGen inClGen = new ClassGen(innerCls, getParentGen(), fallback);
			code.newLine();
			inClGen.addClassCode(code);
			imports.addAll(inClGen.getImports());
		}
	}

	private boolean isInnerClassesPresents() {
S
Skylot 已提交
242 243
		for (ClassNode innerCls : cls.getInnerClasses()) {
			if (!innerCls.isAnonymous()) {
S
Skylot 已提交
244
				return true;
S
Skylot 已提交
245
			}
S
Skylot 已提交
246
		}
S
Skylot 已提交
247
		return false;
S
Skylot 已提交
248 249
	}

S
Skylot 已提交
250
	private void addMethods(CodeWriter code) {
S
Skylot 已提交
251 252
		List<MethodNode> methods = sortMethodsByLine(cls.getMethods());
		for (MethodNode mth : methods) {
S
Skylot 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
			if (mth.contains(AFlag.DONT_GENERATE)) {
				continue;
			}
			if (code.getLine() != clsDeclLine) {
				code.newLine();
			}
			try {
				addMethod(code, mth);
			} catch (Exception e) {
				String msg = ErrorsCounter.methodError(mth, "Method generation error", e);
				code.startLine("/* " + msg + CodeWriter.NL + Utils.getStackTrace(e) + " */");
			}
		}
	}

S
Skylot 已提交
268 269 270 271 272 273
	private static List<MethodNode> sortMethodsByLine(List<MethodNode> methods) {
		List<MethodNode> out = new ArrayList<MethodNode>(methods);
		Collections.sort(out, METHOD_LINE_COMPARATOR);
		return out;
	}

S
Skylot 已提交
274
	private boolean isMethodsPresents() {
S
Skylot 已提交
275
		for (MethodNode mth : cls.getMethods()) {
S
Skylot 已提交
276
			if (!mth.contains(AFlag.DONT_GENERATE)) {
S
Skylot 已提交
277
				return true;
278
			}
S
Skylot 已提交
279
		}
S
Skylot 已提交
280
		return false;
S
Skylot 已提交
281 282
	}

S
Skylot 已提交
283 284
	private void addMethod(CodeWriter code, MethodNode mth) throws CodegenException {
		if (mth.getAccessFlags().isAbstract() || mth.getAccessFlags().isNative()) {
S
Skylot 已提交
285
			MethodGen mthGen = new MethodGen(this, mth);
S
Skylot 已提交
286 287 288 289 290 291 292 293 294 295
			mthGen.addDefinition(code);
			if (cls.getAccessFlags().isAnnotation()) {
				Object def = annotationGen.getAnnotationDefaultValue(mth.getName());
				if (def != null) {
					code.add(" default ");
					annotationGen.encodeValue(code, def);
				}
			}
			code.add(';');
		} else {
S
Skylot 已提交
296
			boolean badCode = mth.contains(AFlag.INCONSISTENT_CODE);
S
Skylot 已提交
297 298 299
			if (badCode) {
				code.startLine("/* JADX WARNING: inconsistent code. */");
				code.startLine("/* Code decompiled incorrectly, please refer to instructions dump. */");
S
Skylot 已提交
300
				ErrorsCounter.methodError(mth, "Inconsistent code");
301 302 303
				if (showInconsistentCode) {
					mth.remove(AFlag.INCONSISTENT_CODE);
				}
S
Skylot 已提交
304
			}
S
Skylot 已提交
305
			MethodGen mthGen;
306
			if (badCode || mth.contains(AType.JADX_ERROR) || fallback) {
S
Skylot 已提交
307 308 309 310
				mthGen = MethodGen.getFallbackMethodGen(mth);
			} else {
				mthGen = new MethodGen(this, mth);
			}
S
Skylot 已提交
311 312 313 314 315 316
			if (mthGen.addDefinition(code)) {
				code.add(' ');
			}
			code.add('{');
			code.incIndent();
			insertSourceFileInfo(code, mth);
317 318 319 320 321
			if (fallback) {
				mthGen.addFallbackMethodCode(code);
			} else {
				mthGen.addInstructions(code);
			}
S
Skylot 已提交
322 323 324 325
			code.decIndent();
			code.startLine('}');
		}
	}
326

S
Skylot 已提交
327 328 329
	private void addFields(CodeWriter code) throws CodegenException {
		addEnumFields(code);
		for (FieldNode f : cls.getFields()) {
S
Skylot 已提交
330
			if (f.contains(AFlag.DONT_GENERATE)) {
331 332 333 334
				continue;
			}
			annotationGen.addForField(code, f);
			code.startLine(f.getAccessFlags().makeString());
335
			useType(code, f.getType());
336 337
			code.add(' ');
			code.add(f.getName());
S
Skylot 已提交
338
			FieldValueAttr fv = f.get(AType.FIELD_VALUE);
339 340 341 342 343
			if (fv != null) {
				code.add(" = ");
				if (fv.getValue() == null) {
					code.add(TypeGen.literalToString(0, f.getType()));
				} else {
344
					annotationGen.encodeValue(code, fv.getValue());
345 346 347
				}
			}
			code.add(';');
348
			code.attachDefinition(f);
349 350 351
		}
	}

S
Skylot 已提交
352 353 354 355 356 357 358 359 360
	private boolean isFieldsPresents() {
		for (FieldNode field : cls.getFields()) {
			if (!field.contains(AFlag.DONT_GENERATE)) {
				return true;
			}
		}
		return false;
	}

S
Skylot 已提交
361
	private void addEnumFields(CodeWriter code) throws CodegenException {
S
Skylot 已提交
362
		EnumClassAttr enumFields = cls.get(AType.ENUM_CLASS);
363 364 365 366 367 368 369
		if (enumFields == null) {
			return;
		}
		InsnGen igen = null;
		for (Iterator<EnumField> it = enumFields.getFields().iterator(); it.hasNext(); ) {
			EnumField f = it.next();
			code.startLine(f.getName());
S
Skylot 已提交
370
			if (!f.getArgs().isEmpty()) {
371 372 373 374 375 376 377 378 379 380 381
				code.add('(');
				for (Iterator<InsnArg> aIt = f.getArgs().iterator(); aIt.hasNext(); ) {
					InsnArg arg = aIt.next();
					if (igen == null) {
						// don't init mth gen if this is simple enum
						MethodGen mthGen = new MethodGen(this, enumFields.getStaticMethod());
						igen = new InsnGen(mthGen, false);
					}
					igen.addArg(code, arg);
					if (aIt.hasNext()) {
						code.add(", ");
S
Skylot 已提交
382
					}
S
Skylot 已提交
383
				}
384
				code.add(')');
S
Skylot 已提交
385
			}
386
			if (f.getCls() != null) {
S
Skylot 已提交
387
				code.add(' ');
388
				new ClassGen(f.getCls(), this, fallback).addClassBody(code);
S
Skylot 已提交
389
			}
390 391 392 393
			if (it.hasNext()) {
				code.add(',');
			}
		}
S
Skylot 已提交
394 395 396 397 398
		if (isMethodsPresents() || isFieldsPresents() || isInnerClassesPresents()) {
			if (enumFields.getFields().isEmpty()) {
				code.startLine();
			}
			code.add(';');
S
Skylot 已提交
399 400 401
		}
	}

402
	public void useType(CodeWriter code, ArgType type) {
S
Skylot 已提交
403
		PrimitiveType stype = type.getPrimitiveType();
404 405 406 407 408 409
		if (stype == null) {
			code.add(type.toString());
		} else if (stype == PrimitiveType.OBJECT) {
			if (type.isGenericType()) {
				code.add(type.getObject());
			} else {
410
				useClass(code, ClassInfo.fromType(cls.dex(), type));
411 412 413 414 415 416
			}
		} else if (stype == PrimitiveType.ARRAY) {
			useType(code, type.getArrayElement());
			code.add("[]");
		} else {
			code.add(stype.getLongName());
417 418
		}
	}
419

420 421 422 423
	public void useClass(CodeWriter code, ClassInfo classInfo) {
		ClassNode classNode = cls.dex().resolveClass(classInfo);
		if (classNode != null) {
			code.attachAnnotation(classNode);
S
Skylot 已提交
424
		}
425 426
		String baseClass = useClassInternal(cls.getClassInfo(), classInfo);
		code.add(baseClass);
S
Skylot 已提交
427
		ArgType[] generics = classInfo.getType().getGenericTypes();
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
		if (generics != null) {
			code.add('<');
			int len = generics.length;
			for (int i = 0; i < len; i++) {
				if (i != 0) {
					code.add(", ");
				}
				ArgType gt = generics[i];
				ArgType wt = gt.getWildcardType();
				if (wt != null) {
					code.add('?');
					int bounds = gt.getWildcardBounds();
					if (bounds != 0) {
						code.add(bounds == -1 ? " super " : " extends ");
						useType(code, wt);
					}
				} else {
					useType(code, gt);
S
Skylot 已提交
446
				}
447
			}
448
			code.add('>');
449
		}
S
Skylot 已提交
450 451
	}

S
Skylot 已提交
452
	private String useClassInternal(ClassInfo useCls, ClassInfo classInfo) {
453
		String fullName = classInfo.getFullName();
S
Skylot 已提交
454
		if (fallback) {
455
			return fullName;
S
Skylot 已提交
456
		}
457 458
		fullName = Deobfuscator.instance().getClassFullName(classInfo);
		String shortName = Deobfuscator.instance().getClassShortName(classInfo);
S
Skylot 已提交
459 460 461 462
		if (classInfo.getPackage().equals("java.lang") && classInfo.getParentClass() == null) {
			return shortName;
		} else {
			// don't add import if this class inner for current class
S
Skylot 已提交
463
			if (isClassInnerFor(classInfo, useCls)) {
S
Skylot 已提交
464
				return shortName;
S
Skylot 已提交
465
			}
466
			// don't add import if this class from same package
S
Skylot 已提交
467
			if (classInfo.getPackage().equals(useCls.getPackage()) && !classInfo.isInner()) {
468
				return shortName;
S
Skylot 已提交
469
			}
S
Skylot 已提交
470 471 472 473
			// don't add import if class not public (must be accessed using inheritance)
			ClassNode classNode = cls.dex().resolveClass(classInfo);
			if (classNode != null && !classNode.getAccessFlags().isPublic()) {
				return shortName;
S
Skylot 已提交
474
			}
S
Skylot 已提交
475
			if (searchCollision(cls.dex(), useCls, classInfo)) {
476
				return fullName;
S
Skylot 已提交
477
			}
S
Skylot 已提交
478
			if (classInfo.getPackage().equals(useCls.getPackage())) {
479
				fullName = Deobfuscator.instance().getClassName(classInfo);
S
Skylot 已提交
480
			}
481
			for (ClassInfo importCls : getImports()) {
S
Skylot 已提交
482 483
				if (!importCls.equals(classInfo)
						&& importCls.getShortName().equals(shortName)) {
484 485 486 487 488 489
					if (classInfo.isInner()) {
						String parent = useClassInternal(useCls, classInfo.getParentClass());
						return parent + "." + shortName;
					} else {
						return fullName;
					}
S
Skylot 已提交
490 491
				}
			}
492
			addImport(classInfo);
S
Skylot 已提交
493 494 495 496
			return shortName;
		}
	}

497 498 499 500 501 502 503 504
	private void addImport(ClassInfo classInfo) {
		if (parentGen != null) {
			parentGen.addImport(classInfo);
		} else {
			imports.add(classInfo);
		}
	}

505 506 507 508 509 510 511 512
	private Set<ClassInfo> getImports() {
		if (parentGen != null) {
			return parentGen.getImports();
		} else {
			return imports;
		}
	}

S
Skylot 已提交
513
	private static boolean isClassInnerFor(ClassInfo inner, ClassInfo parent) {
S
Skylot 已提交
514 515
		if (inner.isInner()) {
			ClassInfo p = inner.getParentClass();
S
Skylot 已提交
516
			return p.equals(parent) || isClassInnerFor(p, parent);
S
Skylot 已提交
517 518 519 520
		}
		return false;
	}

S
Skylot 已提交
521
	private static boolean searchCollision(DexNode dex, ClassInfo useCls, ClassInfo searchCls) {
S
Skylot 已提交
522 523 524
		if (useCls == null) {
			return false;
		}
S
Skylot 已提交
525
		String shortName = searchCls.getShortName();
S
Skylot 已提交
526 527 528 529
		if (useCls.getShortName().equals(shortName)) {
			return true;
		}
		ClassNode classNode = dex.resolveClass(useCls);
530 531
		if (classNode != null) {
			for (ClassNode inner : classNode.getInnerClasses()) {
S
Skylot 已提交
532 533
				if (inner.getShortName().equals(shortName)
						&& !inner.getClassInfo().equals(searchCls)) {
534 535
					return true;
				}
S
Skylot 已提交
536 537
			}
		}
S
Skylot 已提交
538
		return searchCollision(dex, useCls.getParentClass(), searchCls);
S
Skylot 已提交
539 540
	}

541
	private void insertSourceFileInfo(CodeWriter code, AttrNode node) {
S
Skylot 已提交
542
		SourceFileAttr sourceFileAttr = node.get(AType.SOURCE_FILE);
543
		if (sourceFileAttr != null) {
544
			code.startLine("/* compiled from: ").add(sourceFileAttr.getFileName()).add(" */");
545 546 547
		}
	}

S
Skylot 已提交
548
	public ClassGen getParentGen() {
549
		return parentGen == null ? this : parentGen;
S
Skylot 已提交
550 551 552 553 554 555 556 557 558 559
	}

	public AnnotationGen getAnnotationGen() {
		return annotationGen;
	}

	public boolean isFallbackMode() {
		return fallback;
	}
}