summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/analysis/index/InheritanceIndex.java
blob: 1ab2abdfe361f0f39b726aeb18bab004f7655281 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
 * 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.representation.entry.ClassDefEntry;
import cuchaz.enigma.translation.representation.entry.ClassEntry;

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

public class InheritanceIndex implements JarIndexer {
	private final EntryIndex entryIndex;

	private Multimap<ClassEntry, ClassEntry> classParents = HashMultimap.create();
	private Multimap<ClassEntry, ClassEntry> classChildren = HashMultimap.create();

	public InheritanceIndex(EntryIndex entryIndex) {
		this.entryIndex = entryIndex;
	}

	@Override
	public void indexClass(ClassDefEntry classEntry) {
		if (classEntry.isJre()) {
			return;
		}

		ClassEntry superClass = classEntry.getSuperClass();
		if (superClass != null && !superClass.getName().equals("java/lang/Object")) {
			indexParent(classEntry, superClass);
		}

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

	private void indexParent(ClassEntry childEntry, ClassEntry parentEntry) {
		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 Collection<ClassEntry> getDescendants(ClassEntry classEntry) {
		Collection<ClassEntry> descendants = new HashSet<>();

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

		while (!descendantQueue.isEmpty()) {
			ClassEntry descendant = descendantQueue.pop();
			Collection<ClassEntry> children = getChildren(descendant);

			children.forEach(descendantQueue::push);
			descendants.addAll(children);
		}

		return descendants;
	}

	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 Relation computeClassRelation(ClassEntry classEntry, ClassEntry potentialAncestor) {
		if (potentialAncestor.getName().equals("java/lang/Object")) return Relation.RELATED;
		if (!entryIndex.hasClass(classEntry)) return Relation.UNKNOWN;

		for (ClassEntry ancestor : getAncestors(classEntry)) {
			if (potentialAncestor.equals(ancestor)) {
				return Relation.RELATED;
			} else if (!entryIndex.hasClass(ancestor)) {
				return Relation.UNKNOWN;
			}
		}

		return Relation.UNRELATED;
	}

	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();
	}

	public enum Relation {
		RELATED,
		UNRELATED,
		UNKNOWN
	}
}