summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/analysis/index/InheritanceIndex.java
blob: d165cc83088281503232318e01de672e8c07b74a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * Jeff Martin - initial API and implementation
 ******************************************************************************/

package cuchaz.enigma.analysis.index;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import cuchaz.enigma.translation.Translator;
import cuchaz.enigma.translation.representation.entry.ClassDefEntry;
import cuchaz.enigma.translation.representation.entry.ClassEntry;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Set;

public class InheritanceIndex implements JarIndexer, RemappableIndex {
	private Multimap<ClassEntry, ClassEntry> classParents = HashMultimap.create();
	private Multimap<ClassEntry, ClassEntry> classChildren = HashMultimap.create();

	@Override
	public void remap(Translator translator) {
		classChildren = translator.translate(classChildren);
		classParents = translator.translate(classParents);
	}

	@Override
	public InheritanceIndex remapped(Translator translator) {
		InheritanceIndex index = new InheritanceIndex();
		index.classParents = translator.translate(classParents);
		index.classChildren = translator.translate(classChildren);

		return index;
	}

	@Override
	public void indexClass(ClassDefEntry classEntry) {
		ClassEntry superClass = classEntry.getSuperClass();
		if (superClass != null) {
			indexParent(classEntry, superClass);
		}

		for (ClassEntry interfaceEntry : classEntry.getInterfaces()) {
			indexParent(classEntry, interfaceEntry);
		}
	}

	private void indexParent(ClassEntry childEntry, ClassEntry parentEntry) {
		if (childEntry.isJre() || parentEntry.isJre()) {
			return;
		}
		classParents.put(childEntry, parentEntry);
		classChildren.put(parentEntry, childEntry);
	}

	public Collection<ClassEntry> getParents(ClassEntry classEntry) {
		return classParents.get(classEntry);
	}

	public Collection<ClassEntry> getChildren(ClassEntry classEntry) {
		return classChildren.get(classEntry);
	}

	public Set<ClassEntry> getAncestors(ClassEntry classEntry) {
		Set<ClassEntry> ancestors = Sets.newHashSet();

		LinkedList<ClassEntry> ancestorQueue = new LinkedList<>();
		ancestorQueue.push(classEntry);

		while (!ancestorQueue.isEmpty()) {
			ClassEntry ancestor = ancestorQueue.pop();
			Collection<ClassEntry> parents = getParents(ancestor);

			parents.forEach(ancestorQueue::push);
			ancestors.addAll(parents);
		}

		return ancestors;
	}

	public boolean isParent(ClassEntry classEntry) {
		return classChildren.containsKey(classEntry);
	}

	public boolean hasParents(ClassEntry classEntry) {
		Collection<ClassEntry> parents = classParents.get(classEntry);
		return parents != null && !parents.isEmpty();
	}
}