diff options
| author | 2015-02-08 21:29:25 -0500 | |
|---|---|---|
| committer | 2015-02-08 21:29:25 -0500 | |
| commit | ed9b5cdfc648e86fd463bfa8d86b94c41671e14c (patch) | |
| tree | 2619bbc7e04dfa3b82f8dfd3b1d31f529766cd4b /src/cuchaz/enigma/analysis/TranslationIndex.java | |
| download | enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.tar.gz enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.tar.xz enigma-fork-ed9b5cdfc648e86fd463bfa8d86b94c41671e14c.zip | |
switch all classes to new signature/type system
Diffstat (limited to 'src/cuchaz/enigma/analysis/TranslationIndex.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/TranslationIndex.java | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/TranslationIndex.java b/src/cuchaz/enigma/analysis/TranslationIndex.java new file mode 100644 index 0000000..7597c3a --- /dev/null +++ b/src/cuchaz/enigma/analysis/TranslationIndex.java | |||
| @@ -0,0 +1,227 @@ | |||
| 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.IOException; | ||
| 14 | import java.io.InputStream; | ||
| 15 | import java.io.ObjectInputStream; | ||
| 16 | import java.io.ObjectOutputStream; | ||
| 17 | import java.io.OutputStream; | ||
| 18 | import java.io.Serializable; | ||
| 19 | import java.util.HashMap; | ||
| 20 | import java.util.List; | ||
| 21 | import java.util.Map; | ||
| 22 | import java.util.Set; | ||
| 23 | import java.util.zip.GZIPInputStream; | ||
| 24 | import java.util.zip.GZIPOutputStream; | ||
| 25 | |||
| 26 | import javassist.CtBehavior; | ||
| 27 | import javassist.CtClass; | ||
| 28 | import javassist.CtField; | ||
| 29 | |||
| 30 | import com.google.common.collect.HashMultimap; | ||
| 31 | import com.google.common.collect.Lists; | ||
| 32 | import com.google.common.collect.Maps; | ||
| 33 | import com.google.common.collect.Multimap; | ||
| 34 | |||
| 35 | import cuchaz.enigma.mapping.ArgumentEntry; | ||
| 36 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 37 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 38 | import cuchaz.enigma.mapping.Entry; | ||
| 39 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 40 | import cuchaz.enigma.mapping.JavassistUtil; | ||
| 41 | import cuchaz.enigma.mapping.Translator; | ||
| 42 | |||
| 43 | public class TranslationIndex implements Serializable { | ||
| 44 | |||
| 45 | private static final long serialVersionUID = 738687982126844179L; | ||
| 46 | |||
| 47 | private Map<ClassEntry,ClassEntry> m_superclasses; | ||
| 48 | private Multimap<ClassEntry,FieldEntry> m_fieldEntries; | ||
| 49 | private Multimap<ClassEntry,BehaviorEntry> m_behaviorEntries; | ||
| 50 | |||
| 51 | public TranslationIndex() { | ||
| 52 | m_superclasses = Maps.newHashMap(); | ||
| 53 | m_fieldEntries = HashMultimap.create(); | ||
| 54 | m_behaviorEntries = HashMultimap.create(); | ||
| 55 | } | ||
| 56 | |||
| 57 | public TranslationIndex(TranslationIndex other, Translator translator) { | ||
| 58 | |||
| 59 | // translate the superclasses | ||
| 60 | m_superclasses = Maps.newHashMap(); | ||
| 61 | for (Map.Entry<ClassEntry,ClassEntry> mapEntry : other.m_superclasses.entrySet()) { | ||
| 62 | m_superclasses.put( | ||
| 63 | translator.translateEntry(mapEntry.getKey()), | ||
| 64 | translator.translateEntry(mapEntry.getValue()) | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | // translate the fields | ||
| 69 | m_fieldEntries = HashMultimap.create(); | ||
| 70 | for (Map.Entry<ClassEntry,FieldEntry> mapEntry : other.m_fieldEntries.entries()) { | ||
| 71 | m_fieldEntries.put( | ||
| 72 | translator.translateEntry(mapEntry.getKey()), | ||
| 73 | translator.translateEntry(mapEntry.getValue()) | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | |||
| 77 | m_behaviorEntries = HashMultimap.create(); | ||
| 78 | for (Map.Entry<ClassEntry,BehaviorEntry> mapEntry : other.m_behaviorEntries.entries()) { | ||
| 79 | m_behaviorEntries.put( | ||
| 80 | translator.translateEntry(mapEntry.getKey()), | ||
| 81 | translator.translateEntry(mapEntry.getValue()) | ||
| 82 | ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | public void indexClass(CtClass c) { | ||
| 87 | |||
| 88 | ClassEntry classEntry = JavassistUtil.getClassEntry(c); | ||
| 89 | |||
| 90 | // add the superclass | ||
| 91 | ClassEntry superclassEntry = JavassistUtil.getSuperclassEntry(c); | ||
| 92 | if (!isJre(classEntry) && superclassEntry != null && !isJre(superclassEntry)) { | ||
| 93 | m_superclasses.put(classEntry, superclassEntry); | ||
| 94 | } | ||
| 95 | |||
| 96 | // add fields | ||
| 97 | for (CtField field : c.getDeclaredFields()) { | ||
| 98 | FieldEntry fieldEntry = JavassistUtil.getFieldEntry(field); | ||
| 99 | m_fieldEntries.put(fieldEntry.getClassEntry(), fieldEntry); | ||
| 100 | } | ||
| 101 | |||
| 102 | // add behaviors | ||
| 103 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { | ||
| 104 | BehaviorEntry behaviorEntry = JavassistUtil.getBehaviorEntry(behavior); | ||
| 105 | m_behaviorEntries.put(behaviorEntry.getClassEntry(), behaviorEntry); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | public void renameClasses(Map<String,String> renames) { | ||
| 110 | EntryRenamer.renameClassesInMap(renames, m_superclasses); | ||
| 111 | EntryRenamer.renameClassesInMultimap(renames, m_fieldEntries); | ||
| 112 | EntryRenamer.renameClassesInMultimap(renames, m_behaviorEntries); | ||
| 113 | } | ||
| 114 | |||
| 115 | public ClassEntry getSuperclass(ClassEntry classEntry) { | ||
| 116 | return m_superclasses.get(classEntry); | ||
| 117 | } | ||
| 118 | |||
| 119 | public List<ClassEntry> getAncestry(ClassEntry classEntry) { | ||
| 120 | List<ClassEntry> ancestors = Lists.newArrayList(); | ||
| 121 | while (classEntry != null) { | ||
| 122 | classEntry = getSuperclass(classEntry); | ||
| 123 | if (classEntry != null) { | ||
| 124 | ancestors.add(classEntry); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | return ancestors; | ||
| 128 | } | ||
| 129 | |||
| 130 | public List<ClassEntry> getSubclass(ClassEntry classEntry) { | ||
| 131 | // linear search is fast enough for now | ||
| 132 | List<ClassEntry> subclasses = Lists.newArrayList(); | ||
| 133 | for (Map.Entry<ClassEntry,ClassEntry> entry : m_superclasses.entrySet()) { | ||
| 134 | ClassEntry subclass = entry.getKey(); | ||
| 135 | ClassEntry superclass = entry.getValue(); | ||
| 136 | if (classEntry.equals(superclass)) { | ||
| 137 | subclasses.add(subclass); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | return subclasses; | ||
| 141 | } | ||
| 142 | |||
| 143 | public void getSubclassesRecursively(Set<ClassEntry> out, ClassEntry classEntry) { | ||
| 144 | for (ClassEntry subclassEntry : getSubclass(classEntry)) { | ||
| 145 | out.add(subclassEntry); | ||
| 146 | getSubclassesRecursively(out, subclassEntry); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | public void getSubclassNamesRecursively(Set<String> out, ClassEntry classEntry) { | ||
| 151 | for (ClassEntry subclassEntry : getSubclass(classEntry)) { | ||
| 152 | out.add(subclassEntry.getName()); | ||
| 153 | getSubclassNamesRecursively(out, subclassEntry); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | public boolean entryExists(Entry entry) { | ||
| 158 | if (entry instanceof FieldEntry) { | ||
| 159 | return fieldExists((FieldEntry)entry); | ||
| 160 | } else if (entry instanceof BehaviorEntry) { | ||
| 161 | return behaviorExists((BehaviorEntry)entry); | ||
| 162 | } else if (entry instanceof ArgumentEntry) { | ||
| 163 | return behaviorExists(((ArgumentEntry)entry).getBehaviorEntry()); | ||
| 164 | } | ||
| 165 | throw new IllegalArgumentException("Cannot check existence for " + entry.getClass()); | ||
| 166 | } | ||
| 167 | |||
| 168 | public boolean fieldExists(FieldEntry fieldEntry) { | ||
| 169 | return m_fieldEntries.containsEntry(fieldEntry.getClassEntry(), fieldEntry); | ||
| 170 | } | ||
| 171 | |||
| 172 | public boolean behaviorExists(BehaviorEntry behaviorEntry) { | ||
| 173 | return m_behaviorEntries.containsEntry(behaviorEntry.getClassEntry(), behaviorEntry); | ||
| 174 | } | ||
| 175 | |||
| 176 | public ClassEntry resolveEntryClass(Entry entry) { | ||
| 177 | |||
| 178 | if (entry instanceof ClassEntry) { | ||
| 179 | return (ClassEntry)entry; | ||
| 180 | } | ||
| 181 | |||
| 182 | // this entry could refer to a method on a class where the method is not actually implemented | ||
| 183 | // travel up the inheritance tree to find the closest implementation | ||
| 184 | while (!entryExists(entry)) { | ||
| 185 | |||
| 186 | // is there a parent class? | ||
| 187 | ClassEntry superclassEntry = getSuperclass(entry.getClassEntry()); | ||
| 188 | if (superclassEntry == null) { | ||
| 189 | // this is probably a method from a class in a library | ||
| 190 | // we can't trace the implementation up any higher unless we index the library | ||
| 191 | return null; | ||
| 192 | } | ||
| 193 | |||
| 194 | // move up to the parent class | ||
| 195 | entry = entry.cloneToNewClass(superclassEntry); | ||
| 196 | } | ||
| 197 | return entry.getClassEntry(); | ||
| 198 | } | ||
| 199 | |||
| 200 | private boolean isJre(ClassEntry classEntry) { | ||
| 201 | String packageName = classEntry.getPackageName(); | ||
| 202 | return packageName != null && (packageName.startsWith("java") || packageName.startsWith("javax")); | ||
| 203 | } | ||
| 204 | |||
| 205 | public void write(OutputStream out) | ||
| 206 | throws IOException { | ||
| 207 | GZIPOutputStream gzipout = new GZIPOutputStream(out); | ||
| 208 | ObjectOutputStream oout = new ObjectOutputStream(gzipout); | ||
| 209 | oout.writeObject(m_superclasses); | ||
| 210 | oout.writeObject(m_fieldEntries); | ||
| 211 | oout.writeObject(m_behaviorEntries); | ||
| 212 | gzipout.finish(); | ||
| 213 | } | ||
| 214 | |||
| 215 | @SuppressWarnings("unchecked") | ||
| 216 | public void read(InputStream in) | ||
| 217 | throws IOException { | ||
| 218 | try { | ||
| 219 | ObjectInputStream oin = new ObjectInputStream(new GZIPInputStream(in)); | ||
| 220 | m_superclasses = (HashMap<ClassEntry,ClassEntry>)oin.readObject(); | ||
| 221 | m_fieldEntries = (HashMultimap<ClassEntry,FieldEntry>)oin.readObject(); | ||
| 222 | m_behaviorEntries = (HashMultimap<ClassEntry,BehaviorEntry>)oin.readObject(); | ||
| 223 | } catch (ClassNotFoundException ex) { | ||
| 224 | throw new Error(ex); | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||