diff options
| author | 2015-05-21 23:30:00 +0100 | |
|---|---|---|
| committer | 2015-05-21 23:30:00 +0100 | |
| commit | e3f452250e51b7271f3989c7dfd12e4422934942 (patch) | |
| tree | 5aa482f9a6e21eb318a3e23e7d8274d77c73faf6 /src/cuchaz/enigma/analysis/EntryRenamer.java | |
| download | enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.tar.gz enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.tar.xz enigma-fork-e3f452250e51b7271f3989c7dfd12e4422934942.zip | |
Support Gradle alongside SSJB
This makes builds faster, simpler and better automated but still keeps
Cuchaz happy. :)
Diffstat (limited to 'src/cuchaz/enigma/analysis/EntryRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/EntryRenamer.java | 192 |
1 files changed, 192 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..f748274 --- /dev/null +++ b/src/cuchaz/enigma/analysis/EntryRenamer.java | |||
| @@ -0,0 +1,192 @@ | |||
| 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 | * | ||
| 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.ClassNameReplacer; | ||
| 25 | import cuchaz.enigma.mapping.ConstructorEntry; | ||
| 26 | import cuchaz.enigma.mapping.Entry; | ||
| 27 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 28 | import cuchaz.enigma.mapping.MethodEntry; | ||
| 29 | import cuchaz.enigma.mapping.Signature; | ||
| 30 | import cuchaz.enigma.mapping.Type; | ||
| 31 | |||
| 32 | public class EntryRenamer { | ||
| 33 | |||
| 34 | public static <T> void renameClassesInSet(Map<String,String> renames, Set<T> set) { | ||
| 35 | List<T> entries = Lists.newArrayList(); | ||
| 36 | for (T val : set) { | ||
| 37 | entries.add(renameClassesInThing(renames, val)); | ||
| 38 | } | ||
| 39 | set.clear(); | ||
| 40 | set.addAll(entries); | ||
| 41 | } | ||
| 42 | |||
| 43 | public static <Key,Val> void renameClassesInMap(Map<String,String> renames, Map<Key,Val> map) { | ||
| 44 | // for each key/value pair... | ||
| 45 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 46 | for (Map.Entry<Key,Val> entry : map.entrySet()) { | ||
| 47 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 48 | renameClassesInThing(renames, entry.getKey()), | ||
| 49 | renameClassesInThing(renames, entry.getValue()) | ||
| 50 | )); | ||
| 51 | } | ||
| 52 | map.clear(); | ||
| 53 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 54 | map.put(entry.getKey(), entry.getValue()); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | public static <Key,Val> void renameClassesInMultimap(Map<String,String> renames, Multimap<Key,Val> map) { | ||
| 59 | // for each key/value pair... | ||
| 60 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 61 | for (Map.Entry<Key,Val> entry : map.entries()) { | ||
| 62 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 63 | renameClassesInThing(renames, entry.getKey()), | ||
| 64 | renameClassesInThing(renames, entry.getValue()) | ||
| 65 | )); | ||
| 66 | } | ||
| 67 | map.clear(); | ||
| 68 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 69 | map.put(entry.getKey(), entry.getValue()); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | public static <Key,Val> void renameMethodsInMultimap(Map<MethodEntry,MethodEntry> renames, Multimap<Key,Val> map) { | ||
| 74 | // for each key/value pair... | ||
| 75 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 76 | for (Map.Entry<Key,Val> entry : map.entries()) { | ||
| 77 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 78 | renameMethodsInThing(renames, entry.getKey()), | ||
| 79 | renameMethodsInThing(renames, entry.getValue()) | ||
| 80 | )); | ||
| 81 | } | ||
| 82 | map.clear(); | ||
| 83 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 84 | map.put(entry.getKey(), entry.getValue()); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | public static <Key,Val> void renameMethodsInMap(Map<MethodEntry,MethodEntry> renames, Map<Key,Val> map) { | ||
| 89 | // for each key/value pair... | ||
| 90 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 91 | for (Map.Entry<Key,Val> entry : map.entrySet()) { | ||
| 92 | entriesToAdd.add(new AbstractMap.SimpleEntry<Key,Val>( | ||
| 93 | renameMethodsInThing(renames, entry.getKey()), | ||
| 94 | renameMethodsInThing(renames, entry.getValue()) | ||
| 95 | )); | ||
| 96 | } | ||
| 97 | map.clear(); | ||
| 98 | for (Map.Entry<Key,Val> entry : entriesToAdd) { | ||
| 99 | map.put(entry.getKey(), entry.getValue()); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | @SuppressWarnings("unchecked") | ||
| 104 | public static <T> T renameMethodsInThing(Map<MethodEntry,MethodEntry> renames, T thing) { | ||
| 105 | if (thing instanceof MethodEntry) { | ||
| 106 | MethodEntry methodEntry = (MethodEntry)thing; | ||
| 107 | MethodEntry newMethodEntry = renames.get(methodEntry); | ||
| 108 | if (newMethodEntry != null) { | ||
| 109 | return (T)new MethodEntry( | ||
| 110 | methodEntry.getClassEntry(), | ||
| 111 | newMethodEntry.getName(), | ||
| 112 | methodEntry.getSignature() | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | return thing; | ||
| 116 | } else if (thing instanceof ArgumentEntry) { | ||
| 117 | ArgumentEntry argumentEntry = (ArgumentEntry)thing; | ||
| 118 | return (T)new ArgumentEntry( | ||
| 119 | renameMethodsInThing(renames, argumentEntry.getBehaviorEntry()), | ||
| 120 | argumentEntry.getIndex(), | ||
| 121 | argumentEntry.getName() | ||
| 122 | ); | ||
| 123 | } else if (thing instanceof EntryReference) { | ||
| 124 | EntryReference<Entry,Entry> reference = (EntryReference<Entry,Entry>)thing; | ||
| 125 | reference.entry = renameMethodsInThing(renames, reference.entry); | ||
| 126 | reference.context = renameMethodsInThing(renames, reference.context); | ||
| 127 | return thing; | ||
| 128 | } | ||
| 129 | return thing; | ||
| 130 | } | ||
| 131 | |||
| 132 | @SuppressWarnings("unchecked") | ||
| 133 | public static <T> T renameClassesInThing(final Map<String,String> renames, T thing) { | ||
| 134 | if (thing instanceof String) { | ||
| 135 | String stringEntry = (String)thing; | ||
| 136 | if (renames.containsKey(stringEntry)) { | ||
| 137 | return (T)renames.get(stringEntry); | ||
| 138 | } | ||
| 139 | } else if (thing instanceof ClassEntry) { | ||
| 140 | ClassEntry classEntry = (ClassEntry)thing; | ||
| 141 | return (T)new ClassEntry(renameClassesInThing(renames, classEntry.getClassName())); | ||
| 142 | } else if (thing instanceof FieldEntry) { | ||
| 143 | FieldEntry fieldEntry = (FieldEntry)thing; | ||
| 144 | return (T)new FieldEntry( | ||
| 145 | renameClassesInThing(renames, fieldEntry.getClassEntry()), | ||
| 146 | fieldEntry.getName(), | ||
| 147 | renameClassesInThing(renames, fieldEntry.getType()) | ||
| 148 | ); | ||
| 149 | } else if (thing instanceof ConstructorEntry) { | ||
| 150 | ConstructorEntry constructorEntry = (ConstructorEntry)thing; | ||
| 151 | return (T)new ConstructorEntry( | ||
| 152 | renameClassesInThing(renames, constructorEntry.getClassEntry()), | ||
| 153 | renameClassesInThing(renames, constructorEntry.getSignature()) | ||
| 154 | ); | ||
| 155 | } else if (thing instanceof MethodEntry) { | ||
| 156 | MethodEntry methodEntry = (MethodEntry)thing; | ||
| 157 | return (T)new MethodEntry( | ||
| 158 | renameClassesInThing(renames, methodEntry.getClassEntry()), | ||
| 159 | methodEntry.getName(), | ||
| 160 | renameClassesInThing(renames, methodEntry.getSignature()) | ||
| 161 | ); | ||
| 162 | } else if (thing instanceof ArgumentEntry) { | ||
| 163 | ArgumentEntry argumentEntry = (ArgumentEntry)thing; | ||
| 164 | return (T)new ArgumentEntry( | ||
| 165 | renameClassesInThing(renames, argumentEntry.getBehaviorEntry()), | ||
| 166 | argumentEntry.getIndex(), | ||
| 167 | argumentEntry.getName() | ||
| 168 | ); | ||
| 169 | } else if (thing instanceof EntryReference) { | ||
| 170 | EntryReference<Entry,Entry> reference = (EntryReference<Entry,Entry>)thing; | ||
| 171 | reference.entry = renameClassesInThing(renames, reference.entry); | ||
| 172 | reference.context = renameClassesInThing(renames, reference.context); | ||
| 173 | return thing; | ||
| 174 | } else if (thing instanceof Signature) { | ||
| 175 | return (T)new Signature((Signature)thing, new ClassNameReplacer() { | ||
| 176 | @Override | ||
| 177 | public String replace(String className) { | ||
| 178 | return renameClassesInThing(renames, className); | ||
| 179 | } | ||
| 180 | }); | ||
| 181 | } else if (thing instanceof Type) { | ||
| 182 | return (T)new Type((Type)thing, new ClassNameReplacer() { | ||
| 183 | @Override | ||
| 184 | public String replace(String className) { | ||
| 185 | return renameClassesInThing(renames, className); | ||
| 186 | } | ||
| 187 | }); | ||
| 188 | } | ||
| 189 | |||
| 190 | return thing; | ||
| 191 | } | ||
| 192 | } | ||