diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis/EntryRenamer.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/analysis/EntryRenamer.java | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/EntryRenamer.java b/src/main/java/cuchaz/enigma/analysis/EntryRenamer.java new file mode 100644 index 0000000..b99537c --- /dev/null +++ b/src/main/java/cuchaz/enigma/analysis/EntryRenamer.java | |||
| @@ -0,0 +1,184 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * <p> | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.analysis; | ||
| 12 | |||
| 13 | import com.google.common.collect.Lists; | ||
| 14 | import com.google.common.collect.Multimap; | ||
| 15 | import com.google.common.collect.Sets; | ||
| 16 | |||
| 17 | import java.util.AbstractMap; | ||
| 18 | import java.util.List; | ||
| 19 | import java.util.Map; | ||
| 20 | import java.util.Set; | ||
| 21 | |||
| 22 | import cuchaz.enigma.mapping.*; | ||
| 23 | |||
| 24 | public class EntryRenamer { | ||
| 25 | |||
| 26 | public static <T> void renameClassesInSet(Map<String, String> renames, Set<T> set) { | ||
| 27 | List<T> entries = Lists.newArrayList(); | ||
| 28 | for (T val : set) { | ||
| 29 | entries.add(renameClassesInThing(renames, val)); | ||
| 30 | } | ||
| 31 | set.clear(); | ||
| 32 | set.addAll(entries); | ||
| 33 | } | ||
| 34 | |||
| 35 | public static <Key, Val> void renameClassesInMap(Map<String, String> renames, Map<Key, Val> map) { | ||
| 36 | // for each key/value pair... | ||
| 37 | Set<Map.Entry<Key, Val>> entriesToAdd = Sets.newHashSet(); | ||
| 38 | for (Map.Entry<Key, Val> entry : map.entrySet()) { | ||
| 39 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key, Val>( | ||
| 40 | renameClassesInThing(renames, entry.getKey()), | ||
| 41 | renameClassesInThing(renames, entry.getValue()) | ||
| 42 | )); | ||
| 43 | } | ||
| 44 | map.clear(); | ||
| 45 | for (Map.Entry<Key, Val> entry : entriesToAdd) { | ||
| 46 | map.put(entry.getKey(), entry.getValue()); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | public static <Key, Val> void renameClassesInMultimap(Map<String, String> renames, Multimap<Key, Val> map) { | ||
| 51 | // for each key/value pair... | ||
| 52 | Set<Map.Entry<Key, Val>> entriesToAdd = Sets.newHashSet(); | ||
| 53 | for (Map.Entry<Key, Val> entry : map.entries()) { | ||
| 54 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key, Val>( | ||
| 55 | renameClassesInThing(renames, entry.getKey()), | ||
| 56 | renameClassesInThing(renames, entry.getValue()) | ||
| 57 | )); | ||
| 58 | } | ||
| 59 | map.clear(); | ||
| 60 | for (Map.Entry<Key, Val> entry : entriesToAdd) { | ||
| 61 | map.put(entry.getKey(), entry.getValue()); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | public static <Key, Val> void renameMethodsInMultimap(Map<MethodEntry, MethodEntry> renames, Multimap<Key, Val> map) { | ||
| 66 | // for each key/value pair... | ||
| 67 | Set<Map.Entry<Key, Val>> entriesToAdd = Sets.newHashSet(); | ||
| 68 | for (Map.Entry<Key, Val> entry : map.entries()) { | ||
| 69 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key, Val>( | ||
| 70 | renameMethodsInThing(renames, entry.getKey()), | ||
| 71 | renameMethodsInThing(renames, entry.getValue()) | ||
| 72 | )); | ||
| 73 | } | ||
| 74 | map.clear(); | ||
| 75 | for (Map.Entry<Key, Val> entry : entriesToAdd) { | ||
| 76 | map.put(entry.getKey(), entry.getValue()); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | public static <Key, Val> void renameMethodsInMap(Map<MethodEntry, MethodEntry> renames, Map<Key, Val> map) { | ||
| 81 | // for each key/value pair... | ||
| 82 | Set<Map.Entry<Key, Val>> entriesToAdd = Sets.newHashSet(); | ||
| 83 | for (Map.Entry<Key, Val> entry : map.entrySet()) { | ||
| 84 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key, Val>( | ||
| 85 | renameMethodsInThing(renames, entry.getKey()), | ||
| 86 | renameMethodsInThing(renames, entry.getValue()) | ||
| 87 | )); | ||
| 88 | } | ||
| 89 | map.clear(); | ||
| 90 | for (Map.Entry<Key, Val> entry : entriesToAdd) { | ||
| 91 | map.put(entry.getKey(), entry.getValue()); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | @SuppressWarnings("unchecked") | ||
| 96 | public static <T> T renameMethodsInThing(Map<MethodEntry, MethodEntry> renames, T thing) { | ||
| 97 | if (thing instanceof MethodEntry) { | ||
| 98 | MethodEntry methodEntry = (MethodEntry) thing; | ||
| 99 | MethodEntry newMethodEntry = renames.get(methodEntry); | ||
| 100 | if (newMethodEntry != null) { | ||
| 101 | return (T) new MethodEntry( | ||
| 102 | methodEntry.getClassEntry(), | ||
| 103 | newMethodEntry.getName(), | ||
| 104 | methodEntry.getSignature() | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | return thing; | ||
| 108 | } else if (thing instanceof ArgumentEntry) { | ||
| 109 | ArgumentEntry argumentEntry = (ArgumentEntry) thing; | ||
| 110 | return (T) new ArgumentEntry( | ||
| 111 | renameMethodsInThing(renames, argumentEntry.getBehaviorEntry()), | ||
| 112 | argumentEntry.getIndex(), | ||
| 113 | argumentEntry.getName() | ||
| 114 | ); | ||
| 115 | } else if (thing instanceof EntryReference) { | ||
| 116 | EntryReference<Entry, Entry> reference = (EntryReference<Entry, Entry>) thing; | ||
| 117 | reference.entry = renameMethodsInThing(renames, reference.entry); | ||
| 118 | reference.context = renameMethodsInThing(renames, reference.context); | ||
| 119 | return thing; | ||
| 120 | } | ||
| 121 | return thing; | ||
| 122 | } | ||
| 123 | |||
| 124 | @SuppressWarnings("unchecked") | ||
| 125 | public static <T> T renameClassesInThing(final Map<String, String> renames, T thing) { | ||
| 126 | if (thing instanceof String) { | ||
| 127 | String stringEntry = (String) thing; | ||
| 128 | if (renames.containsKey(stringEntry)) { | ||
| 129 | return (T) renames.get(stringEntry); | ||
| 130 | } | ||
| 131 | } else if (thing instanceof ClassEntry) { | ||
| 132 | ClassEntry classEntry = (ClassEntry) thing; | ||
| 133 | return (T) new ClassEntry(renameClassesInThing(renames, classEntry.getClassName())); | ||
| 134 | } else if (thing instanceof FieldEntry) { | ||
| 135 | FieldEntry fieldEntry = (FieldEntry) thing; | ||
| 136 | return (T) new FieldEntry( | ||
| 137 | renameClassesInThing(renames, fieldEntry.getClassEntry()), | ||
| 138 | fieldEntry.getName(), | ||
| 139 | renameClassesInThing(renames, fieldEntry.getType()) | ||
| 140 | ); | ||
| 141 | } else if (thing instanceof ConstructorEntry) { | ||
| 142 | ConstructorEntry constructorEntry = (ConstructorEntry) thing; | ||
| 143 | return (T) new ConstructorEntry( | ||
| 144 | renameClassesInThing(renames, constructorEntry.getClassEntry()), | ||
| 145 | renameClassesInThing(renames, constructorEntry.getSignature()) | ||
| 146 | ); | ||
| 147 | } else if (thing instanceof MethodEntry) { | ||
| 148 | MethodEntry methodEntry = (MethodEntry) thing; | ||
| 149 | return (T) new MethodEntry( | ||
| 150 | renameClassesInThing(renames, methodEntry.getClassEntry()), | ||
| 151 | methodEntry.getName(), | ||
| 152 | renameClassesInThing(renames, methodEntry.getSignature()) | ||
| 153 | ); | ||
| 154 | } else if (thing instanceof ArgumentEntry) { | ||
| 155 | ArgumentEntry argumentEntry = (ArgumentEntry) thing; | ||
| 156 | return (T) new ArgumentEntry( | ||
| 157 | renameClassesInThing(renames, argumentEntry.getBehaviorEntry()), | ||
| 158 | argumentEntry.getIndex(), | ||
| 159 | argumentEntry.getName() | ||
| 160 | ); | ||
| 161 | } else if (thing instanceof EntryReference) { | ||
| 162 | EntryReference<Entry, Entry> reference = (EntryReference<Entry, Entry>) thing; | ||
| 163 | reference.entry = renameClassesInThing(renames, reference.entry); | ||
| 164 | reference.context = renameClassesInThing(renames, reference.context); | ||
| 165 | return thing; | ||
| 166 | } else if (thing instanceof Signature) { | ||
| 167 | return (T) new Signature((Signature) thing, new ClassNameReplacer() { | ||
| 168 | @Override | ||
| 169 | public String replace(String className) { | ||
| 170 | return renameClassesInThing(renames, className); | ||
| 171 | } | ||
| 172 | }); | ||
| 173 | } else if (thing instanceof Type) { | ||
| 174 | return (T) new Type((Type) thing, new ClassNameReplacer() { | ||
| 175 | @Override | ||
| 176 | public String replace(String className) { | ||
| 177 | return renameClassesInThing(renames, className); | ||
| 178 | } | ||
| 179 | }); | ||
| 180 | } | ||
| 181 | |||
| 182 | return thing; | ||
| 183 | } | ||
| 184 | } | ||