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

3 4 5 6 7 8
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

9 10 11 12
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

13 14 15 16 17 18
import com.android.dex.ClassData;
import com.android.dex.ClassData.Field;
import com.android.dex.ClassData.Method;
import com.android.dex.ClassDef;
import com.android.dex.Dex;

19
import jadx.api.ICodeCache;
S
Skylot 已提交
20
import jadx.api.ICodeInfo;
S
Skylot 已提交
21
import jadx.core.Consts;
S
Skylot 已提交
22
import jadx.core.ProcessClass;
23
import jadx.core.dex.attributes.AFlag;
S
Skylot 已提交
24
import jadx.core.dex.attributes.annotations.Annotation;
S
Skylot 已提交
25 26
import jadx.core.dex.attributes.nodes.LineAttrNode;
import jadx.core.dex.attributes.nodes.SourceFileAttr;
S
Skylot 已提交
27 28 29 30 31 32
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 已提交
33
import jadx.core.dex.instructions.args.LiteralArg;
S
Skylot 已提交
34
import jadx.core.dex.nodes.parser.AnnotationsParser;
35
import jadx.core.dex.nodes.parser.FieldInitAttr;
S
Skylot 已提交
36
import jadx.core.dex.nodes.parser.SignatureParser;
S
Skylot 已提交
37 38
import jadx.core.dex.nodes.parser.StaticValuesParser;
import jadx.core.utils.exceptions.DecodeException;
S
Skylot 已提交
39
import jadx.core.utils.exceptions.JadxRuntimeException;
S
Skylot 已提交
40

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

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

	private final DexNode dex;
	private final ClassInfo clsInfo;
49
	private AccessInfo accessFlags;
S
Skylot 已提交
50 51
	private ArgType superClass;
	private List<ArgType> interfaces;
52
	private List<GenericInfo> generics = Collections.emptyList();
S
Skylot 已提交
53

S
Skylot 已提交
54 55
	private final List<MethodNode> methods;
	private final List<FieldNode> fields;
56
	private List<ClassNode> innerClasses = Collections.emptyList();
S
Skylot 已提交
57

58 59
	// store smali
	private String smali;
60 61
	// store parent for inner classes or 'this' otherwise
	private ClassNode parentClass;
S
Skylot 已提交
62

S
Skylot 已提交
63
	private volatile ProcessState state = ProcessState.NOT_LOADED;
S
Skylot 已提交
64
	private List<ClassNode> dependencies = Collections.emptyList();
65

S
Skylot 已提交
66 67 68
	// cache maps
	private Map<MethodInfo, MethodNode> mthInfoMap = Collections.emptyMap();

69
	public ClassNode(DexNode dex, ClassDef cls) {
S
Skylot 已提交
70 71 72
		this.dex = dex;
		this.clsInfo = ClassInfo.fromDex(dex, cls.getTypeIndex());
		try {
S
Skylot 已提交
73 74 75
			if (cls.getSupertypeIndex() == DexNode.NO_INDEX) {
				this.superClass = null;
			} else {
S
Skylot 已提交
76
				this.superClass = dex.getType(cls.getSupertypeIndex());
S
Skylot 已提交
77
			}
S
Skylot 已提交
78
			this.interfaces = new ArrayList<>(cls.getInterfaces().length);
S
Skylot 已提交
79
			for (short interfaceIdx : cls.getInterfaces()) {
D
Donlon 已提交
80
				this.interfaces.add(dex.getType(interfaceIdx));
S
Skylot 已提交
81
			}
S
Skylot 已提交
82
			if (cls.getClassDataOffset() != 0) {
S
Skylot 已提交
83
				ClassData clsData = dex.readClassData(cls);
S
Skylot 已提交
84 85
				int mthsCount = clsData.getDirectMethods().length + clsData.getVirtualMethods().length;
				int fieldsCount = clsData.getStaticFields().length + clsData.getInstanceFields().length;
S
Skylot 已提交
86

S
Skylot 已提交
87 88
				methods = new ArrayList<>(mthsCount);
				fields = new ArrayList<>(fieldsCount);
S
Skylot 已提交
89

S
Skylot 已提交
90
				for (Method mth : clsData.getDirectMethods()) {
91
					methods.add(new MethodNode(this, mth, false));
S
Skylot 已提交
92 93
				}
				for (Method mth : clsData.getVirtualMethods()) {
94
					methods.add(new MethodNode(this, mth, true));
S
Skylot 已提交
95
				}
S
Skylot 已提交
96

S
Skylot 已提交
97
				for (Field f : clsData.getStaticFields()) {
S
Skylot 已提交
98
					fields.add(new FieldNode(this, f));
S
Skylot 已提交
99
				}
S
Skylot 已提交
100
				loadStaticValues(cls, fields);
S
Skylot 已提交
101
				for (Field f : clsData.getInstanceFields()) {
S
Skylot 已提交
102
					fields.add(new FieldNode(this, f));
S
Skylot 已提交
103 104 105 106
				}
			} else {
				methods = Collections.emptyList();
				fields = Collections.emptyList();
S
Skylot 已提交
107 108 109 110
			}

			loadAnnotations(cls);

111 112
			parseClassSignature();
			setFieldsTypesFromSignature();
S
Skylot 已提交
113

114
			int sfIdx = cls.getSourceFileIndex();
115
			if (sfIdx != DexNode.NO_INDEX) {
116
				String fileName = dex.getString(sfIdx);
117
				addSourceFilenameAttr(fileName);
118 119
			}

S
Skylot 已提交
120
			// restore original access flags from dalvik annotation if present
121
			int accFlagsValue;
S
Skylot 已提交
122
			Annotation a = getAnnotation(Consts.DALVIK_INNER_CLASS);
S
Skylot 已提交
123
			if (a != null) {
124
				accFlagsValue = (Integer) a.getValues().get("accessFlags");
S
Skylot 已提交
125
			} else {
126
				accFlagsValue = cls.getAccessFlags();
S
Skylot 已提交
127
			}
S
Skylot 已提交
128
			this.accessFlags = new AccessInfo(accFlagsValue, AFType.CLASS);
S
Skylot 已提交
129
			buildCache();
S
Skylot 已提交
130
		} catch (Exception e) {
131
			throw new JadxRuntimeException("Error decode class: " + clsInfo, e);
S
Skylot 已提交
132 133 134
		}
	}

135
	// empty synthetic class
136
	public ClassNode(DexNode dex, String name, int accessFlags) {
137
		this.dex = dex;
138 139 140 141 142
		this.clsInfo = ClassInfo.fromName(dex.root(), name);
		this.interfaces = new ArrayList<>();
		this.methods = new ArrayList<>();
		this.fields = new ArrayList<>();
		this.accessFlags = new AccessInfo(accessFlags, AFType.CLASS);
143
		this.parentClass = this;
144 145

		dex.addClassNode(this);
146 147
	}

S
Skylot 已提交
148 149 150 151
	private void loadAnnotations(ClassDef cls) {
		int offset = cls.getAnnotationsOffset();
		if (offset != 0) {
			try {
S
Skylot 已提交
152
				new AnnotationsParser(this).parse(offset);
153
			} catch (Exception e) {
S
Skylot 已提交
154
				LOG.error("Error parsing annotations in {}", this, e);
S
Skylot 已提交
155 156 157 158 159 160 161
			}
		}
	}

	private void loadStaticValues(ClassDef cls, List<FieldNode> staticFields) throws DecodeException {
		for (FieldNode f : staticFields) {
			if (f.getAccessFlags().isFinal()) {
162
				f.addAttr(FieldInitAttr.NULL_VALUE);
S
Skylot 已提交
163 164 165
			}
		}
		int offset = cls.getStaticValuesOffset();
166 167 168
		if (offset == 0) {
			return;
		}
169 170 171 172 173 174
		Dex.Section section = dex.openSection(offset);
		StaticValuesParser parser = new StaticValuesParser(dex, section);
		parser.processFields(staticFields);

		// process const fields
		root().getConstValues().processConstFields(this, staticFields);
S
Skylot 已提交
175 176
	}

177
	private void parseClassSignature() {
S
Skylot 已提交
178 179
		SignatureParser sp = SignatureParser.fromNode(this);
		if (sp == null) {
180
			return;
S
Skylot 已提交
181
		}
S
Skylot 已提交
182 183
		try {
			// parse class generic map
184
			generics = sp.consumeGenericMap();
S
Skylot 已提交
185
			// parse super class signature
S
Skylot 已提交
186
			superClass = sp.consumeType();
S
Skylot 已提交
187 188 189 190
			// parse interfaces signatures
			for (int i = 0; i < interfaces.size(); i++) {
				ArgType type = sp.consumeType();
				if (type != null) {
S
Skylot 已提交
191
					interfaces.set(i, type);
S
Skylot 已提交
192 193
				} else {
					break;
194 195
				}
			}
196
		} catch (Exception e) {
S
Skylot 已提交
197
			LOG.error("Class signature parse error: {}", this, e);
198 199 200 201 202
		}
	}

	private void setFieldsTypesFromSignature() {
		for (FieldNode field : fields) {
203 204 205
			try {
				SignatureParser sp = SignatureParser.fromNode(field);
				if (sp != null) {
S
Skylot 已提交
206 207 208 209
					ArgType gType = sp.consumeType();
					if (gType != null) {
						field.setType(gType);
					}
S
Skylot 已提交
210
				}
211 212
			} catch (Exception e) {
				LOG.error("Field signature parse error: {}.{}", this.getFullName(), field.getName(), e);
S
Skylot 已提交
213
			}
214 215 216
		}
	}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	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("$")
235
					&& fileName.endsWith('$' + name)) {
236 237
				return;
			}
S
Skylot 已提交
238 239
			ClassInfo parentCls = clsInfo.getTopParentClass();
			if (parentCls != null && fileName.equals(parentCls.getShortName())) {
S
Skylot 已提交
240 241
				return;
			}
242 243 244 245
		}
		this.addAttr(new SourceFileAttr(fileName));
	}

S
Skylot 已提交
246 247 248 249
	public void loadAndProcess() {
		ProcessClass.process(this);
	}

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

263 264 265 266
	public ICodeInfo getCode() {
		return decompile();
	}

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

	@Override
	public void unload() {
284 285 286 287
		methods.forEach(MethodNode::unload);
		innerClasses.forEach(ClassNode::unload);
		fields.forEach(FieldNode::unloadAttributes);
		unloadAttributes();
288
		setState(UNLOADED);
S
Skylot 已提交
289 290
	}

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

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

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

307 308
	public List<GenericInfo> getGenerics() {
		return generics;
309 310
	}

S
Skylot 已提交
311 312 313 314 315 316 317 318
	public List<MethodNode> getMethods() {
		return methods;
	}

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

319 320 321 322
	public FieldNode getConstField(Object obj) {
		return getConstField(obj, true);
	}

323
	@Nullable
324
	public FieldNode getConstField(Object obj, boolean searchGlobal) {
325
		return root().getConstValues().getConstField(this, obj, searchGlobal);
326 327
	}

328
	@Nullable
1
13.beta2 已提交
329
	public FieldNode getConstFieldByLiteralArg(LiteralArg arg) {
330
		return root().getConstValues().getConstFieldByLiteralArg(this, arg);
1
13.beta2 已提交
331 332
	}

S
Skylot 已提交
333
	public FieldNode searchFieldById(int id) {
334 335 336 337
		return searchField(FieldInfo.fromDex(dex, id));
	}

	public FieldNode searchField(FieldInfo field) {
S
Skylot 已提交
338
		for (FieldNode f : fields) {
339
			if (f.getFieldInfo().equals(field)) {
S
Skylot 已提交
340
				return f;
S
Skylot 已提交
341
			}
S
Skylot 已提交
342 343 344 345
		}
		return null;
	}

346 347 348 349 350 351 352 353 354
	public FieldNode searchFieldByNameAndType(FieldInfo field) {
		for (FieldNode f : fields) {
			if (f.getFieldInfo().equalsNameAndType(field)) {
				return f;
			}
		}
		return null;
	}

S
Skylot 已提交
355
	public FieldNode searchFieldByName(String name) {
S
Skylot 已提交
356
		for (FieldNode f : fields) {
S
Skylot 已提交
357
			if (f.getName().equals(name)) {
S
Skylot 已提交
358
				return f;
S
Skylot 已提交
359
			}
S
Skylot 已提交
360 361 362 363
		}
		return null;
	}

S
Skylot 已提交
364
	public MethodNode searchMethod(MethodInfo mth) {
S
Skylot 已提交
365
		return mthInfoMap.get(mth);
S
Skylot 已提交
366 367
	}

368
	public MethodNode searchMethodByShortId(String shortId) {
S
Skylot 已提交
369
		for (MethodNode m : methods) {
S
Skylot 已提交
370
			if (m.getMethodInfo().getShortId().equals(shortId)) {
S
Skylot 已提交
371
				return m;
S
Skylot 已提交
372
			}
S
Skylot 已提交
373 374 375 376
		}
		return null;
	}

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

S
Skylot 已提交
392
	public MethodNode searchMethodById(int id) {
393
		return searchMethodByShortId(MethodInfo.fromDex(dex, id).getShortId());
S
Skylot 已提交
394 395
	}

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

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

S
Skylot 已提交
413 414 415 416 417
	public List<ClassNode> getInnerClasses() {
		return innerClasses;
	}

	public void addInnerClass(ClassNode cls) {
418 419 420
		if (innerClasses.isEmpty()) {
			innerClasses = new ArrayList<>(5);
		}
S
Skylot 已提交
421
		innerClasses.add(cls);
422
		cls.parentClass = this;
S
Skylot 已提交
423 424
	}

425
	public boolean isEnum() {
S
Skylot 已提交
426 427 428
		return getAccessFlags().isEnum()
				&& getSuperClass() != null
				&& getSuperClass().getObject().equals(ArgType.ENUM.getObject());
429 430
	}

S
Skylot 已提交
431
	public boolean isAnonymous() {
432
		return contains(AFlag.ANONYMOUS_CLASS);
433 434
	}

435 436
	@Nullable
	public MethodNode getClassInitMth() {
437
		return searchMethodByShortId("<clinit>()V");
438 439 440
	}

	@Nullable
S
Skylot 已提交
441 442
	public MethodNode getDefaultConstructor() {
		for (MethodNode mth : methods) {
443
			if (mth.isDefaultConstructor()) {
S
Skylot 已提交
444
				return mth;
S
Skylot 已提交
445 446
			}
		}
S
Skylot 已提交
447
		return null;
S
Skylot 已提交
448 449
	}

450
	@Override
S
Skylot 已提交
451 452 453 454
	public AccessInfo getAccessFlags() {
		return accessFlags;
	}

455 456 457 458 459
	@Override
	public void setAccessFlags(AccessInfo accessFlags) {
		this.accessFlags = accessFlags;
	}

460
	@Override
S
Skylot 已提交
461 462 463 464
	public DexNode dex() {
		return dex;
	}

465 466 467 468 469
	@Override
	public RootNode root() {
		return dex.root();
	}

470 471 472 473 474
	@Override
	public String typeName() {
		return "class";
	}

S
Skylot 已提交
475 476 477 478 479 480 481
	public String getRawName() {
		return clsInfo.getRawName();
	}

	/**
	 * Internal class info (don't use in code generation and external api).
	 */
S
Skylot 已提交
482 483 484 485 486
	public ClassInfo getClassInfo() {
		return clsInfo;
	}

	public String getShortName() {
487
		return clsInfo.getAliasShortName();
S
Skylot 已提交
488 489 490
	}

	public String getFullName() {
491
		return clsInfo.getAliasFullName();
S
Skylot 已提交
492 493 494
	}

	public String getPackage() {
495
		return clsInfo.getAliasPkg();
496 497
	}

498 499 500 501 502 503 504 505
	public void setSmali(String smali) {
		this.smali = smali;
	}

	public String getSmali() {
		return smali;
	}

506 507 508 509 510 511 512 513
	public ProcessState getState() {
		return state;
	}

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

S
Skylot 已提交
514
	public List<ClassNode> getDependencies() {
515 516 517
		return dependencies;
	}

S
Skylot 已提交
518 519 520 521
	public void setDependencies(List<ClassNode> dependencies) {
		this.dependencies = dependencies;
	}

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	@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;
	}

S
Skylot 已提交
539 540
	@Override
	public String toString() {
S
Skylot 已提交
541
		return clsInfo.getFullName();
S
Skylot 已提交
542 543
	}
}