ClassNode.java 22.4 KB
Newer Older
S
Skylot 已提交
1 2
package jadx.core.dex.nodes;

3 4 5
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
6
import java.util.LinkedHashSet;
7 8
import java.util.List;
import java.util.Map;
9
import java.util.Set;
10
import java.util.function.BiConsumer;
11
import java.util.function.Consumer;
12
import java.util.stream.Collectors;
13

14
import org.jetbrains.annotations.NotNull;
15 16 17 18
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

S
Skylot 已提交
19
import jadx.api.DecompilationMode;
20
import jadx.api.ICodeCache;
S
Skylot 已提交
21
import jadx.api.ICodeInfo;
S
Skylot 已提交
22
import jadx.api.ICodeWriter;
S
Skylot 已提交
23
import jadx.api.JadxArgs;
24
import jadx.api.impl.SimpleCodeInfo;
25
import jadx.api.plugins.input.data.IClassData;
26 27
import jadx.api.plugins.input.data.IFieldData;
import jadx.api.plugins.input.data.IMethodData;
28
import jadx.api.plugins.input.data.annotations.EncodedValue;
S
Skylot 已提交
29 30 31 32 33 34
import jadx.api.plugins.input.data.attributes.JadxAttrType;
import jadx.api.plugins.input.data.attributes.types.AnnotationDefaultAttr;
import jadx.api.plugins.input.data.attributes.types.AnnotationDefaultClassAttr;
import jadx.api.plugins.input.data.attributes.types.InnerClassesAttr;
import jadx.api.plugins.input.data.attributes.types.InnerClsInfo;
import jadx.api.plugins.input.data.attributes.types.SourceFileAttr;
35
import jadx.api.plugins.input.data.impl.ListConsumer;
S
Skylot 已提交
36
import jadx.core.Consts;
S
Skylot 已提交
37
import jadx.core.ProcessClass;
38
import jadx.core.dex.attributes.AFlag;
39
import jadx.core.dex.attributes.AType;
40
import jadx.core.dex.attributes.nodes.InlinedAttr;
41
import jadx.core.dex.attributes.nodes.NotificationAttrNode;
S
Skylot 已提交
42 43 44 45 46 47
import jadx.core.dex.info.AccessInfo;
import jadx.core.dex.info.AccessInfo.AFType;
import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.args.ArgType;
1
13.beta2 已提交
48
import jadx.core.dex.instructions.args.LiteralArg;
49
import jadx.core.dex.nodes.utils.TypeUtils;
50
import jadx.core.utils.ListUtils;
51
import jadx.core.utils.Utils;
S
Skylot 已提交
52
import jadx.core.utils.exceptions.JadxRuntimeException;
S
Skylot 已提交
53

S
Skylot 已提交
54
import static jadx.core.dex.nodes.ProcessState.LOADED;
55
import static jadx.core.dex.nodes.ProcessState.NOT_LOADED;
56

57
public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeNode, Comparable<ClassNode> {
S
Skylot 已提交
58
	private static final Logger LOG = LoggerFactory.getLogger(ClassNode.class);
S
Skylot 已提交
59

60
	private final RootNode root;
61
	private final IClassData clsData;
62

S
Skylot 已提交
63
	private final ClassInfo clsInfo;
64
	private AccessInfo accessFlags;
S
Skylot 已提交
65 66
	private ArgType superClass;
	private List<ArgType> interfaces;
67
	private List<ArgType> generics = Collections.emptyList();
S
Skylot 已提交
68

69 70
	private List<MethodNode> methods;
	private List<FieldNode> fields;
71
	private List<ClassNode> innerClasses = Collections.emptyList();
S
Skylot 已提交
72

73 74
	private List<ClassNode> inlinedClasses = Collections.emptyList();

75 76
	// store smali
	private String smali;
77 78
	// store parent for inner classes or 'this' otherwise
	private ClassNode parentClass;
S
Skylot 已提交
79

S
Skylot 已提交
80
	private volatile ProcessState state = ProcessState.NOT_LOADED;
81
	private LoadStage loadStage = LoadStage.NONE;
82

83 84 85
	/**
	 * Top level classes used in this class (only for top level classes, empty for inners)
	 */
S
Skylot 已提交
86
	private List<ClassNode> dependencies = Collections.emptyList();
87 88 89 90
	/**
	 * Top level classes needed for code generation stage
	 */
	private List<ClassNode> codegenDeps = Collections.emptyList();
91 92 93
	/**
	 * Classes which uses this class
	 */
94
	private List<ClassNode> useIn = Collections.emptyList();
95 96 97
	/**
	 * Methods which uses this class (by instructions only, definition is excluded)
	 */
98
	private List<MethodNode> useInMth = Collections.emptyList();
99

S
Skylot 已提交
100 101 102
	// cache maps
	private Map<MethodInfo, MethodNode> mthInfoMap = Collections.emptyMap();

103 104 105
	public ClassNode(RootNode root, IClassData cls) {
		this.root = root;
		this.clsInfo = ClassInfo.fromType(root, ArgType.object(cls.getType()));
106
		this.clsData = cls.copy();
107
		initialLoad(clsData);
108 109
	}

S
Skylot 已提交
110
	private void initialLoad(IClassData cls) {
S
Skylot 已提交
111
		try {
112 113 114
			addAttrs(cls.getAttributes());
			this.accessFlags = new AccessInfo(getAccessFlags(cls), AFType.CLASS);
			this.superClass = checkSuperType(cls);
115
			this.interfaces = Utils.collectionMap(cls.getInterfacesTypes(), ArgType::object);
S
Skylot 已提交
116

117 118 119
			ListConsumer<IFieldData, FieldNode> fieldsConsumer = new ListConsumer<>(fld -> FieldNode.build(this, fld));
			ListConsumer<IMethodData, MethodNode> methodsConsumer = new ListConsumer<>(mth -> MethodNode.build(this, mth));
			cls.visitFieldsAndMethods(fieldsConsumer, methodsConsumer);
120 121 122 123
			if (this.fields != null && this.methods != null) {
				// TODO: temporary solution for restore usage info in reloaded methods and fields
				restoreUsageData(this.fields, this.methods, fieldsConsumer.getResult(), methodsConsumer.getResult());
			}
124 125
			this.fields = fieldsConsumer.getResult();
			this.methods = methodsConsumer.getResult();
S
Skylot 已提交
126

S
Skylot 已提交
127 128
			initStaticValues(fields);
			processAttributes(this);
S
Skylot 已提交
129
			buildCache();
130 131 132 133 134

			// TODO: implement module attribute parsing
			if (this.accessFlags.isModuleInfo()) {
				this.addWarnComment("Modules not supported yet");
			}
S
Skylot 已提交
135
		} catch (Exception e) {
136
			throw new JadxRuntimeException("Error decode class: " + clsInfo, e);
S
Skylot 已提交
137 138 139
		}
	}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	private void restoreUsageData(List<FieldNode> oldFields, List<MethodNode> oldMethods,
			List<FieldNode> newFields, List<MethodNode> newMethods) {
		Map<FieldInfo, FieldNode> oldFieldMap = Utils.groupBy(oldFields, FieldNode::getFieldInfo);
		for (FieldNode newField : newFields) {
			FieldNode oldField = oldFieldMap.get(newField.getFieldInfo());
			if (oldField != null) {
				newField.setUseIn(oldField.getUseIn());
			}
		}
		Map<MethodInfo, MethodNode> oldMethodsMap = Utils.groupBy(oldMethods, MethodNode::getMethodInfo);
		for (MethodNode newMethod : newMethods) {
			MethodNode oldMethod = oldMethodsMap.get(newMethod.getMethodInfo());
			if (oldMethod != null) {
				newMethod.setUseIn(oldMethod.getUseIn());
			}
		}
	}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	private ArgType checkSuperType(IClassData cls) {
		String superType = cls.getSuperType();
		if (superType == null) {
			if (clsInfo.getType().getObject().equals(Consts.CLASS_OBJECT)) {
				// java.lang.Object don't have super class
				return null;
			}
			if (this.accessFlags.isModuleInfo()) {
				// module-info also don't have super class
				return null;
			}
			throw new JadxRuntimeException("No super class in " + clsInfo.getType());
		}
		return ArgType.object(superType);
	}

174 175 176 177 178 179
	public void updateGenericClsData(ArgType superClass, List<ArgType> interfaces, List<ArgType> generics) {
		this.superClass = superClass;
		this.interfaces = interfaces;
		this.generics = generics;
	}

S
Skylot 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	private static void processAttributes(ClassNode cls) {
		// move AnnotationDefault from cls to methods (dex specific)
		AnnotationDefaultClassAttr defAttr = cls.get(JadxAttrType.ANNOTATION_DEFAULT_CLASS);
		if (defAttr != null) {
			cls.remove(JadxAttrType.ANNOTATION_DEFAULT_CLASS);
			for (Map.Entry<String, EncodedValue> entry : defAttr.getValues().entrySet()) {
				MethodNode mth = cls.searchMethodByShortName(entry.getKey());
				if (mth != null) {
					mth.addAttr(new AnnotationDefaultAttr(entry.getValue()));
				} else {
					cls.addWarnComment("Method from annotation default annotation not found: " + entry.getKey());
				}
			}
		}

		// check source file attribute
		if (!cls.checkSourceFilenameAttr()) {
			cls.remove(JadxAttrType.SOURCE_FILE);
		}
	}

	private int getAccessFlags(IClassData cls) {
		InnerClassesAttr innerClassesAttr = get(JadxAttrType.INNER_CLASSES);
		if (innerClassesAttr != null) {
			InnerClsInfo innerClsInfo = innerClassesAttr.getMap().get(cls.getType());
			if (innerClsInfo != null) {
				return innerClsInfo.getAccessFlags();
			}
208
		}
S
Skylot 已提交
209
		return cls.getAccessFlags();
210 211
	}

212
	public static ClassNode addSyntheticClass(RootNode root, String name, int accessFlags) {
213 214 215 216 217 218 219 220 221 222
		ClassInfo clsInfo = ClassInfo.fromName(root, name);
		ClassNode existCls = root.resolveClass(clsInfo);
		if (existCls != null) {
			throw new JadxRuntimeException("Class already exist: " + name);
		}
		return addSyntheticClass(root, clsInfo, accessFlags);
	}

	public static ClassNode addSyntheticClass(RootNode root, ClassInfo clsInfo, int accessFlags) {
		ClassNode cls = new ClassNode(root, clsInfo, accessFlags);
223 224 225 226 227 228 229
		cls.add(AFlag.SYNTHETIC);
		cls.setState(ProcessState.PROCESS_COMPLETE);
		root.addClassNode(cls);
		return cls;
	}

	// Create empty class
230
	private ClassNode(RootNode root, ClassInfo clsInfo, int accessFlags) {
231
		this.root = root;
232
		this.clsData = null;
233
		this.clsInfo = clsInfo;
234 235 236 237
		this.interfaces = new ArrayList<>();
		this.methods = new ArrayList<>();
		this.fields = new ArrayList<>();
		this.accessFlags = new AccessInfo(accessFlags, AFType.CLASS);
238 239 240
		this.parentClass = this;
	}

S
Skylot 已提交
241
	private void initStaticValues(List<FieldNode> fields) {
242 243
		if (fields.isEmpty()) {
			return;
S
Skylot 已提交
244
		}
245
		List<FieldNode> staticFields = fields.stream().filter(FieldNode::isStatic).collect(Collectors.toList());
S
Skylot 已提交
246
		for (FieldNode f : staticFields) {
S
Skylot 已提交
247
			if (f.getAccessFlags().isFinal() && f.get(JadxAttrType.CONSTANT_VALUE) == null) {
248
				// incorrect initialization will be removed if assign found in constructor
S
Skylot 已提交
249
				f.addAttr(EncodedValue.NULL);
S
Skylot 已提交
250 251
			}
		}
252 253 254 255 256
		try {
			// process const fields
			root().getConstValues().processConstFields(this, staticFields);
		} catch (Exception e) {
			this.addWarnComment("Failed to load initial values for static fields", e);
257
		}
S
Skylot 已提交
258 259
	}

S
Skylot 已提交
260 261 262 263
	private boolean checkSourceFilenameAttr() {
		SourceFileAttr sourceFileAttr = get(JadxAttrType.SOURCE_FILE);
		if (sourceFileAttr == null) {
			return true;
264
		}
S
Skylot 已提交
265
		String fileName = sourceFileAttr.getFileName();
266 267 268
		if (fileName.endsWith(".java")) {
			fileName = fileName.substring(0, fileName.length() - 5);
		}
269
		if (fileName.isEmpty() || fileName.equals("SourceFile")) {
S
Skylot 已提交
270
			return false;
271 272 273 274
		}
		if (clsInfo != null) {
			String name = clsInfo.getShortName();
			if (fileName.equals(name)) {
S
Skylot 已提交
275 276 277 278 279 280 281 282 283
				return false;
			}
			ClassInfo parentCls = clsInfo.getParentClass();
			while (parentCls != null) {
				String parentName = parentCls.getShortName();
				if (parentName.equals(fileName) || parentName.startsWith(fileName + '$')) {
					return false;
				}
				parentCls = parentCls.getParentClass();
284
			}
285
			if (fileName.contains("$") && fileName.endsWith('$' + name)) {
S
Skylot 已提交
286 287 288 289
				return false;
			}
			if (name.contains("$") && name.startsWith(fileName)) {
				return false;
S
Skylot 已提交
290
			}
291
		}
S
Skylot 已提交
292
		return true;
293 294
	}

295 296 297 298
	public boolean checkProcessed() {
		return getTopParentClass().getState().isProcessComplete();
	}

299
	public void ensureProcessed() {
300 301
		if (!checkProcessed()) {
			ClassNode topParentClass = getTopParentClass();
302
			throw new JadxRuntimeException("Expected class to be processed at this point,"
303
					+ " class: " + topParentClass + ", state: " + topParentClass.getState());
304
		}
S
Skylot 已提交
305 306
	}

307 308 309 310
	public ICodeInfo decompile() {
		return decompile(true);
	}

S
Skylot 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
	/**
	 * WARNING: Slow operation! Use with caution!
	 */
	public ICodeInfo decompileWithMode(DecompilationMode mode) {
		DecompilationMode baseMode = root.getArgs().getDecompilationMode();
		if (mode == baseMode) {
			return decompile(true);
		}
		JadxArgs args = root.getArgs();
		try {
			unload();
			args.setDecompilationMode(mode);
			ProcessClass process = new ProcessClass(args);
			process.initPasses(root);
			return process.generateCode(this);
		} finally {
			args.setDecompilationMode(baseMode);
		}
	}

331 332 333 334
	public ICodeInfo getCode() {
		return decompile(true);
	}

335 336
	public ICodeInfo reloadCode() {
		add(AFlag.CLASS_DEEP_RELOAD);
337 338 339
		return decompile(false);
	}

340 341 342 343 344 345 346 347 348
	public void unloadCode() {
		if (state == NOT_LOADED) {
			return;
		}
		add(AFlag.CLASS_UNLOADED);
		unloadFromCache();
		deepUnload();
	}

S
Skylot 已提交
349
	public void deepUnload() {
350
		if (clsData == null) {
S
Skylot 已提交
351 352 353
			// manually added class
			return;
		}
354
		unload();
355
		clearAttributes();
356
		root().getConstValues().removeForClass(this);
357
		initialLoad(clsData);
358

359
		innerClasses.forEach(ClassNode::deepUnload);
360 361
	}

362 363 364 365 366 367 368 369
	private void unloadFromCache() {
		if (isInner()) {
			return;
		}
		ICodeCache codeCache = root().getCodeCache();
		codeCache.remove(getRawName());
	}

370
	private synchronized ICodeInfo decompile(boolean searchInCache) {
371 372 373
		if (isInner()) {
			return ICodeInfo.EMPTY;
		}
374
		ICodeCache codeCache = root().getCodeCache();
375
		String clsRawName = getRawName();
376 377
		if (searchInCache) {
			ICodeInfo code = codeCache.get(clsRawName);
378
			if (code != ICodeInfo.EMPTY) {
379 380
				return code;
			}
S
Skylot 已提交
381
		}
382 383 384 385 386 387 388
		ICodeInfo codeInfo;
		try {
			codeInfo = root.getProcessClasses().generateCode(this);
		} catch (Throwable e) {
			addError("Code generation failed", e);
			codeInfo = new SimpleCodeInfo(Utils.getStackTrace(e));
		}
389 390 391
		if (codeInfo != ICodeInfo.EMPTY) {
			codeCache.add(clsRawName, codeInfo);
		}
S
Skylot 已提交
392 393 394
		return codeInfo;
	}

395 396 397 398 399 400 401 402 403 404 405
	@Nullable
	public ICodeInfo getCodeFromCache() {
		ICodeCache codeCache = root().getCodeCache();
		String clsRawName = getRawName();
		ICodeInfo codeInfo = codeCache.get(clsRawName);
		if (codeInfo == ICodeInfo.EMPTY) {
			return null;
		}
		return codeInfo;
	}

S
Skylot 已提交
406
	@Override
S
Skylot 已提交
407
	public void load() {
S
Skylot 已提交
408
		for (MethodNode mth : getMethods()) {
S
Skylot 已提交
409 410
			try {
				mth.load();
411
			} catch (Exception e) {
412
				mth.addError("Method load error", e);
S
Skylot 已提交
413
			}
S
Skylot 已提交
414 415 416 417
		}
		for (ClassNode innerCls : getInnerClasses()) {
			innerCls.load();
		}
S
Skylot 已提交
418
		setState(LOADED);
S
Skylot 已提交
419 420 421 422
	}

	@Override
	public void unload() {
423 424 425
		if (state == NOT_LOADED) {
			return;
		}
426 427 428 429
		methods.forEach(MethodNode::unload);
		innerClasses.forEach(ClassNode::unload);
		fields.forEach(FieldNode::unloadAttributes);
		unloadAttributes();
430
		setState(NOT_LOADED);
431
		this.loadStage = LoadStage.NONE;
432
		this.smali = null;
S
Skylot 已提交
433 434
	}

S
Skylot 已提交
435
	private void buildCache() {
S
Skylot 已提交
436
		mthInfoMap = new HashMap<>(methods.size());
S
Skylot 已提交
437 438 439 440 441
		for (MethodNode mth : methods) {
			mthInfoMap.put(mth.getMethodInfo(), mth);
		}
	}

S
Skylot 已提交
442 443
	@Nullable
	public ArgType getSuperClass() {
S
Skylot 已提交
444 445 446
		return superClass;
	}

S
Skylot 已提交
447
	public List<ArgType> getInterfaces() {
S
Skylot 已提交
448 449 450
		return interfaces;
	}

451
	public List<ArgType> getGenericTypeParameters() {
452
		return generics;
453 454
	}

455 456 457 458 459 460 461 462
	public ArgType getType() {
		ArgType clsType = clsInfo.getType();
		if (Utils.notEmpty(generics)) {
			return ArgType.generic(clsType, generics);
		}
		return clsType;
	}

S
Skylot 已提交
463 464 465 466 467 468 469 470
	public List<MethodNode> getMethods() {
		return methods;
	}

	public List<FieldNode> getFields() {
		return fields;
	}

471
	public void addField(FieldNode fld) {
472 473 474
		if (fields == null || fields.isEmpty()) {
			fields = new ArrayList<>(1);
		}
475 476 477
		fields.add(fld);
	}

478 479 480 481
	public FieldNode getConstField(Object obj) {
		return getConstField(obj, true);
	}

482
	@Nullable
483
	public FieldNode getConstField(Object obj, boolean searchGlobal) {
484
		return root().getConstValues().getConstField(this, obj, searchGlobal);
485 486
	}

487
	@Nullable
1
13.beta2 已提交
488
	public FieldNode getConstFieldByLiteralArg(LiteralArg arg) {
489
		return root().getConstValues().getConstFieldByLiteralArg(this, arg);
1
13.beta2 已提交
490 491
	}

492
	public FieldNode searchField(FieldInfo field) {
S
Skylot 已提交
493
		for (FieldNode f : fields) {
494
			if (f.getFieldInfo().equals(field)) {
S
Skylot 已提交
495
				return f;
S
Skylot 已提交
496
			}
S
Skylot 已提交
497 498 499 500
		}
		return null;
	}

501 502 503 504 505 506 507 508 509
	public FieldNode searchFieldByNameAndType(FieldInfo field) {
		for (FieldNode f : fields) {
			if (f.getFieldInfo().equalsNameAndType(field)) {
				return f;
			}
		}
		return null;
	}

S
Skylot 已提交
510
	public FieldNode searchFieldByName(String name) {
S
Skylot 已提交
511
		for (FieldNode f : fields) {
S
Skylot 已提交
512
			if (f.getName().equals(name)) {
S
Skylot 已提交
513
				return f;
S
Skylot 已提交
514
			}
S
Skylot 已提交
515 516 517 518
		}
		return null;
	}

519 520 521 522 523 524 525 526 527
	public FieldNode searchFieldByShortId(String shortId) {
		for (FieldNode f : fields) {
			if (f.getFieldInfo().getShortId().equals(shortId)) {
				return f;
			}
		}
		return null;
	}

S
Skylot 已提交
528
	public MethodNode searchMethod(MethodInfo mth) {
S
Skylot 已提交
529
		return mthInfoMap.get(mth);
S
Skylot 已提交
530 531
	}

532
	public MethodNode searchMethodByShortId(String shortId) {
S
Skylot 已提交
533
		for (MethodNode m : methods) {
S
Skylot 已提交
534
			if (m.getMethodInfo().getShortId().equals(shortId)) {
S
Skylot 已提交
535
				return m;
S
Skylot 已提交
536
			}
S
Skylot 已提交
537 538 539 540
		}
		return null;
	}

541 542
	/**
	 * Return first method by original short name
543 544
	 * Note: methods are not unique by name (class can have several methods with same name but different
	 * signature)
545 546 547 548 549 550 551 552 553 554 555
	 */
	@Nullable
	public MethodNode searchMethodByShortName(String name) {
		for (MethodNode m : methods) {
			if (m.getMethodInfo().getName().equals(name)) {
				return m;
			}
		}
		return null;
	}

556
	public ClassNode getParentClass() {
557 558 559 560 561 562 563 564 565
		return parentClass;
	}

	public void updateParentClass() {
		if (clsInfo.isInner()) {
			ClassNode parent = root.resolveClass(clsInfo.getParentClass());
			if (parent != null) {
				parentClass = parent;
				return;
566 567
			}
		}
568
		parentClass = this;
569 570
	}

571 572
	public ClassNode getTopParentClass() {
		ClassNode parent = getParentClass();
573
		return parent == this ? this : parent.getTopParentClass();
574 575
	}

576 577 578 579 580 581 582 583 584 585
	public void visitParentClasses(Consumer<ClassNode> consumer) {
		ClassNode currentCls = this;
		ClassNode parentCls = currentCls.getParentClass();
		while (parentCls != currentCls) {
			consumer.accept(parentCls);
			currentCls = parentCls;
			parentCls = currentCls.getParentClass();
		}
	}

586 587 588 589 590 591 592 593 594 595 596 597 598
	public void visitSuperTypes(BiConsumer<ArgType, ArgType> consumer) {
		TypeUtils typeUtils = root.getTypeUtils();
		ArgType thisType = this.getType();
		if (!superClass.equals(ArgType.OBJECT)) {
			consumer.accept(thisType, superClass);
			typeUtils.visitSuperTypes(superClass, consumer);
		}
		for (ArgType iface : interfaces) {
			consumer.accept(thisType, iface);
			typeUtils.visitSuperTypes(iface, consumer);
		}
	}

599 600 601 602 603 604 605 606 607 608 609
	public boolean hasNotGeneratedParent() {
		if (contains(AFlag.DONT_GENERATE)) {
			return true;
		}
		ClassNode parent = getParentClass();
		if (parent == this) {
			return false;
		}
		return parent.hasNotGeneratedParent();
	}

S
Skylot 已提交
610 611 612 613
	public List<ClassNode> getInnerClasses() {
		return innerClasses;
	}

614 615 616 617
	public List<ClassNode> getInlinedClasses() {
		return inlinedClasses;
	}

618
	/**
619
	 * Get all inner and inlined classes recursively
620
	 *
621 622
	 * @param resultClassesSet
	 *                         all identified inner and inlined classes are added to this set
623
	 */
624 625 626 627 628 629 630 631 632 633
	public void getInnerAndInlinedClassesRecursive(Set<ClassNode> resultClassesSet) {
		for (ClassNode innerCls : innerClasses) {
			if (resultClassesSet.add(innerCls)) {
				innerCls.getInnerAndInlinedClassesRecursive(resultClassesSet);
			}
		}
		for (ClassNode inlinedCls : inlinedClasses) {
			if (resultClassesSet.add(inlinedCls)) {
				inlinedCls.getInnerAndInlinedClassesRecursive(resultClassesSet);
			}
634 635 636
		}
	}

S
Skylot 已提交
637
	public void addInnerClass(ClassNode cls) {
638 639 640
		if (innerClasses.isEmpty()) {
			innerClasses = new ArrayList<>(5);
		}
S
Skylot 已提交
641
		innerClasses.add(cls);
642
		cls.parentClass = this;
S
Skylot 已提交
643 644
	}

645 646 647 648
	public void addInlinedClass(ClassNode cls) {
		if (inlinedClasses.isEmpty()) {
			inlinedClasses = new ArrayList<>(5);
		}
649
		cls.addAttr(new InlinedAttr(this));
650 651 652
		inlinedClasses.add(cls);
	}

653
	public boolean isEnum() {
S
Skylot 已提交
654 655 656
		return getAccessFlags().isEnum()
				&& getSuperClass() != null
				&& getSuperClass().getObject().equals(ArgType.ENUM.getObject());
657 658
	}

S
Skylot 已提交
659
	public boolean isAnonymous() {
660
		return contains(AType.ANONYMOUS_CLASS);
661 662
	}

663 664 665 666
	public boolean isSynthetic() {
		return contains(AFlag.SYNTHETIC);
	}

667
	public boolean isInner() {
668
		return parentClass != this;
669 670
	}

671 672 673 674
	public boolean isTopClass() {
		return parentClass == this;
	}

675 676
	@Nullable
	public MethodNode getClassInitMth() {
677
		return searchMethodByShortId("<clinit>()V");
678 679 680
	}

	@Nullable
S
Skylot 已提交
681 682
	public MethodNode getDefaultConstructor() {
		for (MethodNode mth : methods) {
683
			if (mth.isDefaultConstructor()) {
S
Skylot 已提交
684
				return mth;
S
Skylot 已提交
685 686
			}
		}
S
Skylot 已提交
687
		return null;
S
Skylot 已提交
688 689
	}

690
	@Override
S
Skylot 已提交
691 692 693 694
	public AccessInfo getAccessFlags() {
		return accessFlags;
	}

695 696 697 698 699
	@Override
	public void setAccessFlags(AccessInfo accessFlags) {
		this.accessFlags = accessFlags;
	}

700 701
	@Override
	public RootNode root() {
702
		return root;
703 704
	}

705 706 707 708 709
	@Override
	public String typeName() {
		return "class";
	}

S
Skylot 已提交
710 711 712 713 714 715 716
	public String getRawName() {
		return clsInfo.getRawName();
	}

	/**
	 * Internal class info (don't use in code generation and external api).
	 */
S
Skylot 已提交
717 718 719 720 721
	public ClassInfo getClassInfo() {
		return clsInfo;
	}

	public String getShortName() {
722
		return clsInfo.getAliasShortName();
S
Skylot 已提交
723 724 725
	}

	public String getFullName() {
726
		return clsInfo.getAliasFullName();
S
Skylot 已提交
727 728 729
	}

	public String getPackage() {
730
		return clsInfo.getAliasPkg();
731 732
	}

S
Skylot 已提交
733
	public String getDisassembledCode() {
734
		if (smali == null) {
735
			StringBuilder sb = new StringBuilder();
S
Skylot 已提交
736 737
			getDisassembledCode(sb);
			sb.append(ICodeWriter.NL);
738 739 740
			Set<ClassNode> allInlinedClasses = new LinkedHashSet<>();
			getInnerAndInlinedClassesRecursive(allInlinedClasses);
			for (ClassNode innerClass : allInlinedClasses) {
S
Skylot 已提交
741 742
				innerClass.getDisassembledCode(sb);
				sb.append(ICodeWriter.NL);
743
			}
744
			smali = sb.toString();
745
		}
746 747 748
		return smali;
	}

S
Skylot 已提交
749 750
	protected void getDisassembledCode(StringBuilder sb) {
		if (clsData == null) {
751 752
			sb.append(String.format("###### Class %s is created by jadx", getFullName()));
			return;
753
		}
754
		sb.append(String.format("###### Class %s (%s)", getFullName(), getRawName()));
S
Skylot 已提交
755 756
		sb.append(ICodeWriter.NL);
		sb.append(clsData.getDisassembledCode());
757 758
	}

759 760
	public IClassData getClsData() {
		return clsData;
761 762
	}

763 764 765 766 767 768 769 770
	public ProcessState getState() {
		return state;
	}

	public void setState(ProcessState state) {
		this.state = state;
	}

771 772 773 774
	public LoadStage getLoadStage() {
		return loadStage;
	}

775 776
	public void setLoadStage(LoadStage loadStage) {
		this.loadStage = loadStage;
777 778 779 780 781
	}

	public void reloadAtCodegenStage() {
		ClassNode topCls = this.getTopParentClass();
		if (topCls.getLoadStage() == LoadStage.CODEGEN_STAGE) {
782
			throw new JadxRuntimeException("Class not yet loaded at codegen stage: " + topCls);
783 784 785 786
		}
		topCls.add(AFlag.RELOAD_AT_CODEGEN_STAGE);
	}

S
Skylot 已提交
787
	public List<ClassNode> getDependencies() {
788 789 790
		return dependencies;
	}

S
Skylot 已提交
791 792 793 794
	public void setDependencies(List<ClassNode> dependencies) {
		this.dependencies = dependencies;
	}

795 796 797 798
	public void removeDependency(ClassNode dep) {
		this.dependencies = ListUtils.safeRemoveAndTrim(this.dependencies, dep);
	}

799 800 801 802 803 804 805 806
	public List<ClassNode> getCodegenDeps() {
		return codegenDeps;
	}

	public void setCodegenDeps(List<ClassNode> codegenDeps) {
		this.codegenDeps = codegenDeps;
	}

807
	public void addCodegenDep(ClassNode dep) {
808 809 810
		if (!codegenDeps.contains(dep)) {
			this.codegenDeps = ListUtils.safeAdd(this.codegenDeps, dep);
		}
811 812
	}

813 814 815 816
	public int getTotalDepsCount() {
		return dependencies.size() + codegenDeps.size();
	}

817 818 819 820 821 822 823 824 825 826
	public List<ClassNode> getUseIn() {
		return useIn;
	}

	public void setUseIn(List<ClassNode> useIn) {
		this.useIn = useIn;
	}

	public List<MethodNode> getUseInMth() {
		return useInMth;
827 828
	}

829 830
	public void setUseInMth(List<MethodNode> useInMth) {
		this.useInMth = useInMth;
831 832
	}

833
	@Override
834 835
	public String getInputFileName() {
		return clsData == null ? "synthetic" : clsData.getInputFileName();
836 837
	}

838 839 840 841 842
	@Override
	public AnnType getAnnType() {
		return AnnType.CLASS;
	}

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
	@Override
	public int hashCode() {
		return clsInfo.hashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o instanceof ClassNode) {
			ClassNode other = (ClassNode) o;
			return clsInfo.equals(other.clsInfo);
		}
		return false;
	}

860 861 862 863 864
	@Override
	public int compareTo(@NotNull ClassNode o) {
		return this.getFullName().compareTo(o.getFullName());
	}

S
Skylot 已提交
865 866
	@Override
	public String toString() {
S
Skylot 已提交
867
		return clsInfo.getFullName();
S
Skylot 已提交
868 869
	}
}