summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/analysis/Ancestries.java
blob: 972410845cff8f3249c3909ce1705b79ff358d57 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (c) 2014 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.analysis;

import java.io.Serializable;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javassist.bytecode.Descriptor;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

public class Ancestries implements Serializable
{
	private static final long serialVersionUID = 738687982126844179L;
	
	private Map<String,String> m_superclasses;
	private Multimap<String,String> m_interfaces;
	
	public Ancestries( )
	{
		m_superclasses = Maps.newHashMap();
		m_interfaces = HashMultimap.create();
	}
	
	public void addSuperclass( String className, String superclassName )
	{
		className = Descriptor.toJvmName( className );
		superclassName = Descriptor.toJvmName( superclassName );
		
		if( className.equals( superclassName ) )
		{
			throw new IllegalArgumentException( "Class cannot be its own superclass! " + className );
		}
		
		if( !isJre( className ) && !isJre( superclassName ) )
		{
			m_superclasses.put( className, superclassName );
		}
	}
	
	public void addInterface( String className, String interfaceName )
	{
		className = Descriptor.toJvmName( className );
		interfaceName = Descriptor.toJvmName( interfaceName );
		
		if( className.equals( interfaceName ) )
		{
			throw new IllegalArgumentException( "Class cannot be its own interface! " + className );
		}
		
		if( !isJre( className ) && !isJre( interfaceName ) )
		{
			m_interfaces.put( className, interfaceName );
		}
	}
	
	public void renameClasses( Map<String,String> renames )
	{
		// rename superclasses
		Map<String,String> newSuperclasses = Maps.newHashMap();
		for( Map.Entry<String,String> entry : m_superclasses.entrySet() )
		{
			String subclass = renames.get( entry.getKey() );
			if( subclass == null )
			{
				subclass = entry.getKey();
			}
			String superclass = renames.get( entry.getValue() );
			if( superclass == null )
			{
				superclass = entry.getValue();
			}
			newSuperclasses.put( subclass, superclass );
		}
		m_superclasses = newSuperclasses;
		
		// rename interfaces
		Set<Map.Entry<String,String>> entriesToAdd = Sets.newHashSet();
		for( Map.Entry<String,String> entry : m_interfaces.entries() )
		{
			String className = renames.get( entry.getKey() );
			if( className == null )
			{
				className = entry.getKey();
			}
			String interfaceName = renames.get( entry.getValue() );
			if( interfaceName == null )
			{
				interfaceName = entry.getValue();
			}
			entriesToAdd.add( new AbstractMap.SimpleEntry<String,String>( className, interfaceName ) );
		}
		m_interfaces.clear();
		for( Map.Entry<String,String> entry : entriesToAdd )
		{
			m_interfaces.put( entry.getKey(), entry.getValue() );
		}
	}
	
	public String getSuperclassName( String className )
	{
		return m_superclasses.get( className );
	}
	
	public List<String> getAncestry( String className )
	{
		List<String> ancestors = new ArrayList<String>();
		while( className != null )
		{
			className = getSuperclassName( className );
			if( className != null )
			{
				ancestors.add( className );
			}
		}
		return ancestors;
	}
	
	public Set<String> getInterfaces( String className )
	{
		Set<String> interfaceNames = new HashSet<String>();
		interfaceNames.addAll( m_interfaces.get( className ) );
		for( String ancestor : getAncestry( className ) )
		{
			interfaceNames.addAll( m_interfaces.get( ancestor ) );
		}
		return interfaceNames;
	}
	
	public List<String> getSubclasses( String className )
	{
		// linear search is fast enough for now
		List<String> subclasses = Lists.newArrayList();
		for( Map.Entry<String,String> entry : m_superclasses.entrySet() )
		{
			String subclass = entry.getKey();
			String superclass = entry.getValue();
			if( className.equals( superclass ) )
			{
				subclasses.add( subclass );
			}
		}
		return subclasses;
	}
	
	public Set<String> getImplementingClasses( String targetInterfaceName )
	{
		// linear search is fast enough for now
		Set<String> classNames = Sets.newHashSet();
		for( Map.Entry<String,String> entry : m_interfaces.entries() )
		{
			String className = entry.getKey();
			String interfaceName = entry.getValue();
			if( interfaceName.equals( targetInterfaceName ) )
			{
				classNames.add( className );
				collectSubclasses( classNames, className );
			}
		}
		return classNames;
	}
	
	public boolean isInterface( String className )
	{
		return m_interfaces.containsValue( className );
	}
	
	private void collectSubclasses( Set<String> classNames, String className )
	{
		for( String subclassName : getSubclasses( className ) )
		{
			classNames.add( subclassName );
			collectSubclasses( classNames, subclassName );
		}
	}

	private boolean isJre( String className )
	{
		return className.startsWith( "java/" )
			|| className.startsWith( "javax/" );
	}
}