diff options
Diffstat (limited to 'src/cuchaz/enigma/analysis/Ancestries.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/Ancestries.java | 199 |
1 files changed, 0 insertions, 199 deletions
diff --git a/src/cuchaz/enigma/analysis/Ancestries.java b/src/cuchaz/enigma/analysis/Ancestries.java deleted file mode 100644 index 9724108..0000000 --- a/src/cuchaz/enigma/analysis/Ancestries.java +++ /dev/null | |||
| @@ -1,199 +0,0 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.analysis; | ||
| 12 | |||
| 13 | import java.io.Serializable; | ||
| 14 | import java.util.AbstractMap; | ||
| 15 | import java.util.ArrayList; | ||
| 16 | import java.util.HashSet; | ||
| 17 | import java.util.List; | ||
| 18 | import java.util.Map; | ||
| 19 | import java.util.Set; | ||
| 20 | |||
| 21 | import javassist.bytecode.Descriptor; | ||
| 22 | |||
| 23 | import com.google.common.collect.HashMultimap; | ||
| 24 | import com.google.common.collect.Lists; | ||
| 25 | import com.google.common.collect.Maps; | ||
| 26 | import com.google.common.collect.Multimap; | ||
| 27 | import com.google.common.collect.Sets; | ||
| 28 | |||
| 29 | public class Ancestries implements Serializable | ||
| 30 | { | ||
| 31 | private static final long serialVersionUID = 738687982126844179L; | ||
| 32 | |||
| 33 | private Map<String,String> m_superclasses; | ||
| 34 | private Multimap<String,String> m_interfaces; | ||
| 35 | |||
| 36 | public Ancestries( ) | ||
| 37 | { | ||
| 38 | m_superclasses = Maps.newHashMap(); | ||
| 39 | m_interfaces = HashMultimap.create(); | ||
| 40 | } | ||
| 41 | |||
| 42 | public void addSuperclass( String className, String superclassName ) | ||
| 43 | { | ||
| 44 | className = Descriptor.toJvmName( className ); | ||
| 45 | superclassName = Descriptor.toJvmName( superclassName ); | ||
| 46 | |||
| 47 | if( className.equals( superclassName ) ) | ||
| 48 | { | ||
| 49 | throw new IllegalArgumentException( "Class cannot be its own superclass! " + className ); | ||
| 50 | } | ||
| 51 | |||
| 52 | if( !isJre( className ) && !isJre( superclassName ) ) | ||
| 53 | { | ||
| 54 | m_superclasses.put( className, superclassName ); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | public void addInterface( String className, String interfaceName ) | ||
| 59 | { | ||
| 60 | className = Descriptor.toJvmName( className ); | ||
| 61 | interfaceName = Descriptor.toJvmName( interfaceName ); | ||
| 62 | |||
| 63 | if( className.equals( interfaceName ) ) | ||
| 64 | { | ||
| 65 | throw new IllegalArgumentException( "Class cannot be its own interface! " + className ); | ||
| 66 | } | ||
| 67 | |||
| 68 | if( !isJre( className ) && !isJre( interfaceName ) ) | ||
| 69 | { | ||
| 70 | m_interfaces.put( className, interfaceName ); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | public void renameClasses( Map<String,String> renames ) | ||
| 75 | { | ||
| 76 | // rename superclasses | ||
| 77 | Map<String,String> newSuperclasses = Maps.newHashMap(); | ||
| 78 | for( Map.Entry<String,String> entry : m_superclasses.entrySet() ) | ||
| 79 | { | ||
| 80 | String subclass = renames.get( entry.getKey() ); | ||
| 81 | if( subclass == null ) | ||
| 82 | { | ||
| 83 | subclass = entry.getKey(); | ||
| 84 | } | ||
| 85 | String superclass = renames.get( entry.getValue() ); | ||
| 86 | if( superclass == null ) | ||
| 87 | { | ||
| 88 | superclass = entry.getValue(); | ||
| 89 | } | ||
| 90 | newSuperclasses.put( subclass, superclass ); | ||
| 91 | } | ||
| 92 | m_superclasses = newSuperclasses; | ||
| 93 | |||
| 94 | // rename interfaces | ||
| 95 | Set<Map.Entry<String,String>> entriesToAdd = Sets.newHashSet(); | ||
| 96 | for( Map.Entry<String,String> entry : m_interfaces.entries() ) | ||
| 97 | { | ||
| 98 | String className = renames.get( entry.getKey() ); | ||
| 99 | if( className == null ) | ||
| 100 | { | ||
| 101 | className = entry.getKey(); | ||
| 102 | } | ||
| 103 | String interfaceName = renames.get( entry.getValue() ); | ||
| 104 | if( interfaceName == null ) | ||
| 105 | { | ||
| 106 | interfaceName = entry.getValue(); | ||
| 107 | } | ||
| 108 | entriesToAdd.add( new AbstractMap.SimpleEntry<String,String>( className, interfaceName ) ); | ||
| 109 | } | ||
| 110 | m_interfaces.clear(); | ||
| 111 | for( Map.Entry<String,String> entry : entriesToAdd ) | ||
| 112 | { | ||
| 113 | m_interfaces.put( entry.getKey(), entry.getValue() ); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | public String getSuperclassName( String className ) | ||
| 118 | { | ||
| 119 | return m_superclasses.get( className ); | ||
| 120 | } | ||
| 121 | |||
| 122 | public List<String> getAncestry( String className ) | ||
| 123 | { | ||
| 124 | List<String> ancestors = new ArrayList<String>(); | ||
| 125 | while( className != null ) | ||
| 126 | { | ||
| 127 | className = getSuperclassName( className ); | ||
| 128 | if( className != null ) | ||
| 129 | { | ||
| 130 | ancestors.add( className ); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | return ancestors; | ||
| 134 | } | ||
| 135 | |||
| 136 | public Set<String> getInterfaces( String className ) | ||
| 137 | { | ||
| 138 | Set<String> interfaceNames = new HashSet<String>(); | ||
| 139 | interfaceNames.addAll( m_interfaces.get( className ) ); | ||
| 140 | for( String ancestor : getAncestry( className ) ) | ||
| 141 | { | ||
| 142 | interfaceNames.addAll( m_interfaces.get( ancestor ) ); | ||
| 143 | } | ||
| 144 | return interfaceNames; | ||
| 145 | } | ||
| 146 | |||
| 147 | public List<String> getSubclasses( String className ) | ||
| 148 | { | ||
| 149 | // linear search is fast enough for now | ||
| 150 | List<String> subclasses = Lists.newArrayList(); | ||
| 151 | for( Map.Entry<String,String> entry : m_superclasses.entrySet() ) | ||
| 152 | { | ||
| 153 | String subclass = entry.getKey(); | ||
| 154 | String superclass = entry.getValue(); | ||
| 155 | if( className.equals( superclass ) ) | ||
| 156 | { | ||
| 157 | subclasses.add( subclass ); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | return subclasses; | ||
| 161 | } | ||
| 162 | |||
| 163 | public Set<String> getImplementingClasses( String targetInterfaceName ) | ||
| 164 | { | ||
| 165 | // linear search is fast enough for now | ||
| 166 | Set<String> classNames = Sets.newHashSet(); | ||
| 167 | for( Map.Entry<String,String> entry : m_interfaces.entries() ) | ||
| 168 | { | ||
| 169 | String className = entry.getKey(); | ||
| 170 | String interfaceName = entry.getValue(); | ||
| 171 | if( interfaceName.equals( targetInterfaceName ) ) | ||
| 172 | { | ||
| 173 | classNames.add( className ); | ||
| 174 | collectSubclasses( classNames, className ); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | return classNames; | ||
| 178 | } | ||
| 179 | |||
| 180 | public boolean isInterface( String className ) | ||
| 181 | { | ||
| 182 | return m_interfaces.containsValue( className ); | ||
| 183 | } | ||
| 184 | |||
| 185 | private void collectSubclasses( Set<String> classNames, String className ) | ||
| 186 | { | ||
| 187 | for( String subclassName : getSubclasses( className ) ) | ||
| 188 | { | ||
| 189 | classNames.add( subclassName ); | ||
| 190 | collectSubclasses( classNames, subclassName ); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | private boolean isJre( String className ) | ||
| 195 | { | ||
| 196 | return className.startsWith( "java/" ) | ||
| 197 | || className.startsWith( "javax/" ); | ||
| 198 | } | ||
| 199 | } | ||