ClassNode.java 16.2 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.Consumer;
11
import java.util.stream.Collectors;
12

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

18
import jadx.api.ICodeCache;
S
Skylot 已提交
19
import jadx.api.ICodeInfo;
20 21 22
import jadx.api.plugins.input.data.IClassData;
import jadx.api.plugins.input.data.annotations.EncodedValue;
import jadx.api.plugins.input.data.annotations.IAnnotation;
S
Skylot 已提交
23
import jadx.core.Consts;
S
Skylot 已提交
24
import jadx.core.ProcessClass;
25
import jadx.core.dex.attributes.AFlag;
26 27
import jadx.core.dex.attributes.FieldInitAttr;
import jadx.core.dex.attributes.annotations.AnnotationsList;
28
import jadx.core.dex.attributes.nodes.NotificationAttrNode;
S
Skylot 已提交
29
import jadx.core.dex.attributes.nodes.SourceFileAttr;
S
Skylot 已提交
30 31 32 33 34 35
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 已提交
36
import jadx.core.dex.instructions.args.LiteralArg;
37
import jadx.core.dex.visitors.ProcessAnonymous;
38
import jadx.core.utils.Utils;
S
Skylot 已提交
39
import jadx.core.utils.exceptions.JadxRuntimeException;
S
Skylot 已提交
40

S
Skylot 已提交
41
import static jadx.core.dex.nodes.ProcessState.LOADED;
42
import static jadx.core.dex.nodes.ProcessState.NOT_LOADED;
43
import static jadx.core.dex.nodes.ProcessState.PROCESS_COMPLETE;
44

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

48
	private final RootNode root;
49
	private final IClassData clsData;
50

S
Skylot 已提交
51
	private final ClassInfo clsInfo;
52
	private AccessInfo accessFlags;
S
Skylot 已提交
53 54
	private ArgType superClass;
	private List<ArgType> interfaces;
55
	private List<ArgType> generics = Collections.emptyList();
S
Skylot 已提交
56

57 58
	private List<MethodNode> methods;
	private List<FieldNode> fields;
59
	private List<ClassNode> innerClasses = Collections.emptyList();
S
Skylot 已提交
60

61 62
	private List<ClassNode> inlinedClasses = Collections.emptyList();

63 64
	// store smali
	private String smali;
65 66
	// store parent for inner classes or 'this' otherwise
	private ClassNode parentClass;
S
Skylot 已提交
67

S
Skylot 已提交
68
	private volatile ProcessState state = ProcessState.NOT_LOADED;
69
	private LoadStage loadStage = LoadStage.NONE;
70 71

	/** Top level classes used in this class (only for top level classes, empty for inners) */
S
Skylot 已提交
72
	private List<ClassNode> dependencies = Collections.emptyList();
73 74 75 76
	/** Classes which uses this class */
	private List<ClassNode> useIn = Collections.emptyList();
	/** Methods which uses this class (by instructions only, definition is excluded) */
	private List<MethodNode> useInMth = Collections.emptyList();
77

S
Skylot 已提交
78 79 80
	// cache maps
	private Map<MethodInfo, MethodNode> mthInfoMap = Collections.emptyMap();

81 82 83
	public ClassNode(RootNode root, IClassData cls) {
		this.root = root;
		this.clsInfo = ClassInfo.fromType(root, ArgType.object(cls.getType()));
84
		this.clsData = cls.copy();
85
		initialLoad(clsData);
86 87
	}

S
Skylot 已提交
88
	private void initialLoad(IClassData cls) {
S
Skylot 已提交
89
		try {
90 91
			String superType = cls.getSuperType();
			if (superType == null) {
92 93 94 95
				// only java.lang.Object don't have super class
				if (!clsInfo.getType().getObject().equals(Consts.CLASS_OBJECT)) {
					throw new JadxRuntimeException("No super class in " + clsInfo.getType());
				}
96
				this.superClass = null;
S
Skylot 已提交
97
			} else {
98
				this.superClass = ArgType.object(superType);
S
Skylot 已提交
99
			}
100
			this.interfaces = Utils.collectionMap(cls.getInterfacesTypes(), ArgType::object);
S
Skylot 已提交
101

102 103 104 105 106
			methods = new ArrayList<>();
			fields = new ArrayList<>();
			cls.visitFieldsAndMethods(
					fld -> fields.add(FieldNode.build(this, fld)),
					mth -> methods.add(MethodNode.build(this, mth)));
S
Skylot 已提交
107

108 109
			AnnotationsList.attach(this, cls.getAnnotations());
			loadStaticValues(cls, fields);
110
			initAccessFlags(cls);
S
Skylot 已提交
111

112
			addSourceFilenameAttr(cls.getSourceFile());
S
Skylot 已提交
113
			buildCache();
S
Skylot 已提交
114
		} catch (Exception e) {
115
			throw new JadxRuntimeException("Error decode class: " + clsInfo, e);
S
Skylot 已提交
116 117 118
		}
	}

119 120 121 122 123 124
	public void updateGenericClsData(ArgType superClass, List<ArgType> interfaces, List<ArgType> generics) {
		this.superClass = superClass;
		this.interfaces = interfaces;
		this.generics = generics;
	}

125 126 127
	/**
	 * Restore original access flags from Dalvik annotation if present
	 */
128
	private void initAccessFlags(IClassData cls) {
129
		int accFlagsValue;
130
		IAnnotation a = getAnnotation(Consts.DALVIK_INNER_CLASS);
131
		if (a != null) {
132
			accFlagsValue = (Integer) a.getValues().get("accessFlags").getValue();
133 134 135 136 137 138
		} else {
			accFlagsValue = cls.getAccessFlags();
		}
		this.accessFlags = new AccessInfo(accFlagsValue, AFType.CLASS);
	}

139 140 141 142 143 144 145 146 147 148
	public static ClassNode addSyntheticClass(RootNode root, String name, int accessFlags) {
		ClassNode cls = new ClassNode(root, name, accessFlags);
		cls.add(AFlag.SYNTHETIC);
		cls.setState(ProcessState.PROCESS_COMPLETE);
		root.addClassNode(cls);
		return cls;
	}

	// Create empty class
	private ClassNode(RootNode root, String name, int accessFlags) {
149
		this.root = root;
150
		this.clsData = null;
151
		this.clsInfo = ClassInfo.fromName(root, name);
152 153 154 155
		this.interfaces = new ArrayList<>();
		this.methods = new ArrayList<>();
		this.fields = new ArrayList<>();
		this.accessFlags = new AccessInfo(accessFlags, AFType.CLASS);
156 157 158
		this.parentClass = this;
	}

159 160 161
	private void loadStaticValues(IClassData cls, List<FieldNode> fields) {
		if (fields.isEmpty()) {
			return;
S
Skylot 已提交
162
		}
163
		List<FieldNode> staticFields = fields.stream().filter(FieldNode::isStatic).collect(Collectors.toList());
S
Skylot 已提交
164 165
		for (FieldNode f : staticFields) {
			if (f.getAccessFlags().isFinal()) {
166
				// incorrect initialization will be removed if assign found in constructor
167
				f.addAttr(FieldInitAttr.NULL_VALUE);
S
Skylot 已提交
168 169
			}
		}
170 171 172
		List<EncodedValue> values = cls.getStaticFieldInitValues();
		int count = values.size();
		if (count == 0 || count > staticFields.size()) {
173 174
			return;
		}
175 176 177
		for (int i = 0; i < count; i++) {
			staticFields.get(i).addAttr(FieldInitAttr.constValue(values.get(i)));
		}
178 179
		// process const fields
		root().getConstValues().processConstFields(this, staticFields);
S
Skylot 已提交
180 181
	}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	private void addSourceFilenameAttr(String fileName) {
		if (fileName == null) {
			return;
		}
		if (fileName.endsWith(".java")) {
			fileName = fileName.substring(0, fileName.length() - 5);
		}
		if (fileName.isEmpty()
				|| fileName.equals("SourceFile")
				|| fileName.equals("\"")) {
			return;
		}
		if (clsInfo != null) {
			String name = clsInfo.getShortName();
			if (fileName.equals(name)) {
				return;
			}
			if (fileName.contains("$")
200
					&& fileName.endsWith('$' + name)) {
201 202
				return;
			}
S
Skylot 已提交
203 204
			ClassInfo parentCls = clsInfo.getTopParentClass();
			if (parentCls != null && fileName.equals(parentCls.getShortName())) {
S
Skylot 已提交
205 206
				return;
			}
207 208 209 210
		}
		this.addAttr(new SourceFileAttr(fileName));
	}

211 212
	public void ensureProcessed() {
		ClassNode topClass = getTopParentClass();
213 214
		ProcessState state = topClass.getState();
		if (state != PROCESS_COMPLETE) {
215
			throw new JadxRuntimeException("Expected class to be processed at this point,"
216
					+ " class: " + topClass + ", state: " + state);
217
		}
S
Skylot 已提交
218 219
	}

220 221 222 223 224 225 226 227
	public ICodeInfo decompile() {
		return decompile(true);
	}

	public ICodeInfo getCode() {
		return decompile(true);
	}

228 229
	public ICodeInfo reloadCode() {
		add(AFlag.CLASS_DEEP_RELOAD);
230 231 232
		return decompile(false);
	}

S
Skylot 已提交
233
	public void deepUnload() {
234
		if (clsData == null) {
S
Skylot 已提交
235 236 237
			// manually added class
			return;
		}
238
		clearAttributes();
239
		root().getConstValues().removeForClass(this);
240
		initialLoad(clsData);
241 242 243 244 245
		ProcessAnonymous.runForClass(this);

		for (ClassNode innerClass : innerClasses) {
			innerClass.deepUnload();
		}
246 247 248
	}

	private synchronized ICodeInfo decompile(boolean searchInCache) {
249 250 251
		ICodeCache codeCache = root().getCodeCache();
		ClassNode topParentClass = getTopParentClass();
		String clsRawName = topParentClass.getRawName();
252 253
		if (searchInCache) {
			ICodeInfo code = codeCache.get(clsRawName);
254
			if (code != null && code != ICodeInfo.EMPTY) {
255 256
				return code;
			}
S
Skylot 已提交
257
		}
258 259
		ICodeInfo codeInfo = ProcessClass.generateCode(topParentClass);
		codeCache.add(clsRawName, codeInfo);
S
Skylot 已提交
260 261 262
		return codeInfo;
	}

S
Skylot 已提交
263
	@Override
S
Skylot 已提交
264
	public void load() {
S
Skylot 已提交
265
		for (MethodNode mth : getMethods()) {
S
Skylot 已提交
266 267
			try {
				mth.load();
268
			} catch (Exception e) {
269
				mth.addError("Method load error", e);
S
Skylot 已提交
270
			}
S
Skylot 已提交
271 272 273 274
		}
		for (ClassNode innerCls : getInnerClasses()) {
			innerCls.load();
		}
S
Skylot 已提交
275
		setState(LOADED);
S
Skylot 已提交
276 277 278 279
	}

	@Override
	public void unload() {
280 281 282
		if (state == NOT_LOADED) {
			return;
		}
283 284 285 286
		methods.forEach(MethodNode::unload);
		innerClasses.forEach(ClassNode::unload);
		fields.forEach(FieldNode::unloadAttributes);
		unloadAttributes();
287
		setState(NOT_LOADED);
288
		this.loadStage = LoadStage.NONE;
289
		this.smali = null;
S
Skylot 已提交
290 291
	}

S
Skylot 已提交
292
	private void buildCache() {
S
Skylot 已提交
293
		mthInfoMap = new HashMap<>(methods.size());
S
Skylot 已提交
294 295 296 297 298
		for (MethodNode mth : methods) {
			mthInfoMap.put(mth.getMethodInfo(), mth);
		}
	}

S
Skylot 已提交
299 300
	@Nullable
	public ArgType getSuperClass() {
S
Skylot 已提交
301 302 303
		return superClass;
	}

S
Skylot 已提交
304
	public List<ArgType> getInterfaces() {
S
Skylot 已提交
305 306 307
		return interfaces;
	}

308
	public List<ArgType> getGenericTypeParameters() {
309
		return generics;
310 311
	}

312 313 314 315 316 317 318 319
	public ArgType getType() {
		ArgType clsType = clsInfo.getType();
		if (Utils.notEmpty(generics)) {
			return ArgType.generic(clsType, generics);
		}
		return clsType;
	}

S
Skylot 已提交
320 321 322 323 324 325 326 327
	public List<MethodNode> getMethods() {
		return methods;
	}

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

328 329 330 331
	public FieldNode getConstField(Object obj) {
		return getConstField(obj, true);
	}

332
	@Nullable
333
	public FieldNode getConstField(Object obj, boolean searchGlobal) {
334
		return root().getConstValues().getConstField(this, obj, searchGlobal);
335 336
	}

337
	@Nullable
1
13.beta2 已提交
338
	public FieldNode getConstFieldByLiteralArg(LiteralArg arg) {
339
		return root().getConstValues().getConstFieldByLiteralArg(this, arg);
1
13.beta2 已提交
340 341
	}

342
	public FieldNode searchField(FieldInfo field) {
S
Skylot 已提交
343
		for (FieldNode f : fields) {
344
			if (f.getFieldInfo().equals(field)) {
S
Skylot 已提交
345
				return f;
S
Skylot 已提交
346
			}
S
Skylot 已提交
347 348 349 350
		}
		return null;
	}

351 352 353 354 355 356 357 358 359
	public FieldNode searchFieldByNameAndType(FieldInfo field) {
		for (FieldNode f : fields) {
			if (f.getFieldInfo().equalsNameAndType(field)) {
				return f;
			}
		}
		return null;
	}

S
Skylot 已提交
360
	public FieldNode searchFieldByName(String name) {
S
Skylot 已提交
361
		for (FieldNode f : fields) {
S
Skylot 已提交
362
			if (f.getName().equals(name)) {
S
Skylot 已提交
363
				return f;
S
Skylot 已提交
364
			}
S
Skylot 已提交
365 366 367 368
		}
		return null;
	}

S
Skylot 已提交
369
	public MethodNode searchMethod(MethodInfo mth) {
S
Skylot 已提交
370
		return mthInfoMap.get(mth);
S
Skylot 已提交
371 372
	}

373
	public MethodNode searchMethodByShortId(String shortId) {
S
Skylot 已提交
374
		for (MethodNode m : methods) {
S
Skylot 已提交
375
			if (m.getMethodInfo().getShortId().equals(shortId)) {
S
Skylot 已提交
376
				return m;
S
Skylot 已提交
377
			}
S
Skylot 已提交
378 379 380 381
		}
		return null;
	}

382 383
	/**
	 * Return first method by original short name
384 385
	 * Note: methods are not unique by name (class can have several methods with same name but different
	 * signature)
386 387 388 389 390 391 392 393 394 395 396
	 */
	@Nullable
	public MethodNode searchMethodByShortName(String name) {
		for (MethodNode m : methods) {
			if (m.getMethodInfo().getName().equals(name)) {
				return m;
			}
		}
		return null;
	}

397 398 399
	public ClassNode getParentClass() {
		if (parentClass == null) {
			if (clsInfo.isInner()) {
400
				ClassNode parent = root.resolveClass(clsInfo.getParentClass());
401
				parentClass = parent == null ? this : parent;
402 403 404 405 406 407 408
			} else {
				parentClass = this;
			}
		}
		return parentClass;
	}

409 410
	public ClassNode getTopParentClass() {
		ClassNode parent = getParentClass();
411
		return parent == this ? this : parent.getTopParentClass();
412 413
	}

414 415 416 417 418 419 420 421 422 423
	public void visitParentClasses(Consumer<ClassNode> consumer) {
		ClassNode currentCls = this;
		ClassNode parentCls = currentCls.getParentClass();
		while (parentCls != currentCls) {
			consumer.accept(parentCls);
			currentCls = parentCls;
			parentCls = currentCls.getParentClass();
		}
	}

424 425 426 427 428 429 430 431 432 433 434
	public boolean hasNotGeneratedParent() {
		if (contains(AFlag.DONT_GENERATE)) {
			return true;
		}
		ClassNode parent = getParentClass();
		if (parent == this) {
			return false;
		}
		return parent.hasNotGeneratedParent();
	}

S
Skylot 已提交
435 436 437 438
	public List<ClassNode> getInnerClasses() {
		return innerClasses;
	}

439
	/**
440
	 * Get all inner and inlined classes recursively
441
	 *
442
	 * @param resultClassesSet all identified inner and inlined classes are added to this set
443
	 */
444 445 446 447 448 449 450 451 452 453
	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);
			}
454 455 456
		}
	}

S
Skylot 已提交
457
	public void addInnerClass(ClassNode cls) {
458 459 460
		if (innerClasses.isEmpty()) {
			innerClasses = new ArrayList<>(5);
		}
S
Skylot 已提交
461
		innerClasses.add(cls);
462
		cls.parentClass = this;
S
Skylot 已提交
463 464
	}

465 466 467 468 469 470 471
	public void addInlinedClass(ClassNode cls) {
		if (inlinedClasses.isEmpty()) {
			inlinedClasses = new ArrayList<>(5);
		}
		inlinedClasses.add(cls);
	}

472
	public boolean isEnum() {
S
Skylot 已提交
473 474 475
		return getAccessFlags().isEnum()
				&& getSuperClass() != null
				&& getSuperClass().getObject().equals(ArgType.ENUM.getObject());
476 477
	}

S
Skylot 已提交
478
	public boolean isAnonymous() {
479
		return contains(AFlag.ANONYMOUS_CLASS);
480 481
	}

482 483 484 485
	public boolean isInner() {
		return parentClass != null;
	}

486 487
	@Nullable
	public MethodNode getClassInitMth() {
488
		return searchMethodByShortId("<clinit>()V");
489 490 491
	}

	@Nullable
S
Skylot 已提交
492 493
	public MethodNode getDefaultConstructor() {
		for (MethodNode mth : methods) {
494
			if (mth.isDefaultConstructor()) {
S
Skylot 已提交
495
				return mth;
S
Skylot 已提交
496 497
			}
		}
S
Skylot 已提交
498
		return null;
S
Skylot 已提交
499 500
	}

501
	@Override
S
Skylot 已提交
502 503 504 505
	public AccessInfo getAccessFlags() {
		return accessFlags;
	}

506 507 508 509 510
	@Override
	public void setAccessFlags(AccessInfo accessFlags) {
		this.accessFlags = accessFlags;
	}

511 512
	@Override
	public RootNode root() {
513
		return root;
514 515
	}

516 517 518 519 520
	@Override
	public String typeName() {
		return "class";
	}

S
Skylot 已提交
521 522 523 524 525 526 527
	public String getRawName() {
		return clsInfo.getRawName();
	}

	/**
	 * Internal class info (don't use in code generation and external api).
	 */
S
Skylot 已提交
528 529 530 531 532
	public ClassInfo getClassInfo() {
		return clsInfo;
	}

	public String getShortName() {
533
		return clsInfo.getAliasShortName();
S
Skylot 已提交
534 535 536
	}

	public String getFullName() {
537
		return clsInfo.getAliasFullName();
S
Skylot 已提交
538 539 540
	}

	public String getPackage() {
541
		return clsInfo.getAliasPkg();
542 543
	}

544
	public String getSmali() {
545
		if (smali == null) {
546 547 548
			StringBuilder sb = new StringBuilder();
			getSmali(sb);
			sb.append(System.lineSeparator());
549 550 551
			Set<ClassNode> allInlinedClasses = new LinkedHashSet<>();
			getInnerAndInlinedClassesRecursive(allInlinedClasses);
			for (ClassNode innerClass : allInlinedClasses) {
552 553
				innerClass.getSmali(sb);
				sb.append(System.lineSeparator());
554
			}
555
			smali = sb.toString();
556
		}
557 558 559
		return smali;
	}

560 561 562 563
	protected void getSmali(StringBuilder sb) {
		if (this.clsData == null) {
			sb.append(String.format("###### Class %s is created by jadx", getFullName()));
			return;
564
		}
565 566 567
		sb.append(String.format("###### Class %s (%s)", getFullName(), getRawName()));
		sb.append(System.lineSeparator());
		sb.append(this.clsData.getDisassembledCode());
568 569
	}

570 571 572 573 574 575 576 577
	public ProcessState getState() {
		return state;
	}

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

578 579 580 581
	public LoadStage getLoadStage() {
		return loadStage;
	}

582 583
	public void setLoadStage(LoadStage loadStage) {
		this.loadStage = loadStage;
584 585 586 587 588 589 590 591 592 593
	}

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

S
Skylot 已提交
594
	public List<ClassNode> getDependencies() {
595 596 597
		return dependencies;
	}

S
Skylot 已提交
598 599 600 601
	public void setDependencies(List<ClassNode> dependencies) {
		this.dependencies = dependencies;
	}

602 603 604 605 606 607 608 609 610 611
	public List<ClassNode> getUseIn() {
		return useIn;
	}

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

	public List<MethodNode> getUseInMth() {
		return useInMth;
612 613
	}

614 615
	public void setUseInMth(List<MethodNode> useInMth) {
		this.useInMth = useInMth;
616 617
	}

618
	@Override
619 620
	public String getInputFileName() {
		return clsData == null ? "synthetic" : clsData.getInputFileName();
621 622
	}

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	@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;
	}

640 641 642 643 644
	@Override
	public int compareTo(@NotNull ClassNode o) {
		return this.getFullName().compareTo(o.getFullName());
	}

S
Skylot 已提交
645 646
	@Override
	public String toString() {
S
Skylot 已提交
647
		return clsInfo.getFullName();
S
Skylot 已提交
648
	}
649

S
Skylot 已提交
650
}