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/EntryRenamer.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/EntryRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/EntryRenamer.java | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/EntryRenamer.java b/src/cuchaz/enigma/analysis/EntryRenamer.java new file mode 100644 index 0000000..b54489c --- /dev/null +++ b/src/cuchaz/enigma/analysis/EntryRenamer.java | |||
| @@ -0,0 +1,171 @@ | |||
| 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.util.AbstractMap; | ||
| 14 | import java.util.List; | ||
| 15 | import java.util.Map; | ||
| 16 | import java.util.Set; | ||
| 17 | |||
| 18 | import com.google.common.collect.Lists; | ||
| 19 | import com.google.common.collect.Multimap; | ||
| 20 | import com.google.common.collect.Sets; | ||
| 21 | |||
| 22 | import cuchaz.enigma.mapping.ArgumentEntry; | ||
| 23 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 24 | import cuchaz.enigma.mapping.ConstructorEntry; | ||
| 25 | import cuchaz.enigma.mapping.Entry; | ||
| 26 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 27 | import cuchaz.enigma.mapping.MethodEntry; | ||
| 28 | |||
| 29 | public class EntryRenamer { | ||
| 30 | |||
| 31 | public static <T> void renameClassesInSet(Map<String,String> renames, Set<T> set) { | ||
| 32 | List<T> entries = Lists.newArrayList(); | ||
| 33 | for (T val : set) { | ||
| 34 | entries.add(renameClassesInThing(renames, val)); | ||
| 35 | } | ||
| 36 | set.clear(); | ||
| 37 | set.addAll(entries); | ||
| 38 | } | ||
| 39 | |||
| 40 | public static <Key,Val> void renameClassesInMap(Map<String,String> renames, Map<Key,Val> map) { | ||
| 41 | // for each key/value pair... | ||
| 42 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 43 | for (Map.Entry<Key,Val> entry : map.entrySet()) { | ||
| 44 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 45 | renameClassesInThing(renames, entry.getKey()), | ||
| 46 | renameClassesInThing(renames, entry.getValue()) | ||
| 47 | )); | ||
| 48 | } | ||
| 49 | map.clear(); | ||
| 50 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 51 | map.put(entry.getKey(), entry.getValue()); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | public static <Key,Val> void renameClassesInMultimap(Map<String,String> renames, Multimap<Key,Val> map) { | ||
| 56 | // for each key/value pair... | ||
| 57 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 58 | for (Map.Entry<Key,Val> entry : map.entries()) { | ||
| 59 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 60 | renameClassesInThing(renames, entry.getKey()), | ||
| 61 | renameClassesInThing(renames, entry.getValue()) | ||
| 62 | )); | ||
| 63 | } | ||
| 64 | map.clear(); | ||
| 65 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 66 | map.put(entry.getKey(), entry.getValue()); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | public static <Key,Val> void renameMethodsInMultimap(Map<MethodEntry,MethodEntry> renames, Multimap<Key,Val> map) { | ||
| 71 | // for each key/value pair... | ||
| 72 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 73 | for (Map.Entry<Key,Val> entry : map.entries()) { | ||
| 74 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 75 | renameMethodsInThing(renames, entry.getKey()), | ||
| 76 | renameMethodsInThing(renames, entry.getValue()) | ||
| 77 | )); | ||
| 78 | } | ||
| 79 | map.clear(); | ||
| 80 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 81 | map.put(entry.getKey(), entry.getValue()); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | public static <Key,Val> void renameMethodsInMap(Map<MethodEntry,MethodEntry> renames, Map<Key,Val> map) { | ||
| 86 | // for each key/value pair... | ||
| 87 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 88 | for (Map.Entry<Key,Val> entry : map.entrySet()) { | ||
| 89 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 90 | renameMethodsInThing(renames, entry.getKey()), | ||
| 91 | renameMethodsInThing(renames, entry.getValue()) | ||
| 92 | )); | ||
| 93 | } | ||
| 94 | map.clear(); | ||
| 95 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 96 | map.put(entry.getKey(), entry.getValue()); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | @SuppressWarnings("unchecked") | ||
| 101 | public static <T> T renameMethodsInThing(Map<MethodEntry,MethodEntry> renames, T thing) { | ||
| 102 | if (thing instanceof MethodEntry) { | ||
| 103 | MethodEntry methodEntry = (MethodEntry)thing; | ||
| 104 | MethodEntry newMethodEntry = renames.get(methodEntry); | ||
| 105 | if (newMethodEntry != null) { | ||
| 106 | return (T)new MethodEntry( | ||
| 107 | methodEntry.getClassEntry(), | ||
| 108 | newMethodEntry.getName(), | ||
| 109 | methodEntry.getSignature() | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | return thing; | ||
| 113 | } else if (thing instanceof ArgumentEntry) { | ||
| 114 | ArgumentEntry argumentEntry = (ArgumentEntry)thing; | ||
| 115 | return (T)new ArgumentEntry( | ||
| 116 | renameMethodsInThing(renames, argumentEntry.getBehaviorEntry()), | ||
| 117 | argumentEntry.getIndex(), | ||
| 118 | argumentEntry.getName() | ||
| 119 | ); | ||
| 120 | } else if (thing instanceof EntryReference) { | ||
| 121 | EntryReference<Entry,Entry> reference = (EntryReference<Entry,Entry>)thing; | ||
| 122 | reference.entry = renameMethodsInThing(renames, reference.entry); | ||
| 123 | reference.context = renameMethodsInThing(renames, reference.context); | ||
| 124 | return thing; | ||
| 125 | } | ||
| 126 | return thing; | ||
| 127 | } | ||
| 128 | |||
| 129 | @SuppressWarnings("unchecked") | ||
| 130 | public static <T> T renameClassesInThing(Map<String,String> renames, T thing) { | ||
| 131 | if (thing instanceof String) { | ||
| 132 | String stringEntry = (String)thing; | ||
| 133 | if (renames.containsKey(stringEntry)) { | ||
| 134 | return (T)renames.get(stringEntry); | ||
| 135 | } | ||
| 136 | } else if (thing instanceof ClassEntry) { | ||
| 137 | ClassEntry classEntry = (ClassEntry)thing; | ||
| 138 | return (T)new ClassEntry(renameClassesInThing(renames, classEntry.getClassName())); | ||
| 139 | } else if (thing instanceof FieldEntry) { | ||
| 140 | FieldEntry fieldEntry = (FieldEntry)thing; | ||
| 141 | return (T)new FieldEntry(renameClassesInThing(renames, fieldEntry.getClassEntry()), fieldEntry.getName()); | ||
| 142 | } else if (thing instanceof ConstructorEntry) { | ||
| 143 | ConstructorEntry constructorEntry = (ConstructorEntry)thing; | ||
| 144 | return (T)new ConstructorEntry( | ||
| 145 | renameClassesInThing(renames, constructorEntry.getClassEntry()), | ||
| 146 | constructorEntry.getSignature() | ||
| 147 | ); | ||
| 148 | } else if (thing instanceof MethodEntry) { | ||
| 149 | MethodEntry methodEntry = (MethodEntry)thing; | ||
| 150 | return (T)new MethodEntry( | ||
| 151 | renameClassesInThing(renames, methodEntry.getClassEntry()), | ||
| 152 | methodEntry.getName(), | ||
| 153 | methodEntry.getSignature() | ||
| 154 | ); | ||
| 155 | } else if (thing instanceof ArgumentEntry) { | ||
| 156 | ArgumentEntry argumentEntry = (ArgumentEntry)thing; | ||
| 157 | return (T)new ArgumentEntry( | ||
| 158 | renameClassesInThing(renames, argumentEntry.getBehaviorEntry()), | ||
| 159 | argumentEntry.getIndex(), | ||
| 160 | argumentEntry.getName() | ||
| 161 | ); | ||
| 162 | } else if (thing instanceof EntryReference) { | ||
| 163 | EntryReference<Entry,Entry> reference = (EntryReference<Entry,Entry>)thing; | ||
| 164 | reference.entry = renameClassesInThing(renames, reference.entry); | ||
| 165 | reference.context = renameClassesInThing(renames, reference.context); | ||
| 166 | return thing; | ||
| 167 | } | ||
| 168 | |||
| 169 | return thing; | ||
| 170 | } | ||
| 171 | } | ||