diff options
Diffstat (limited to 'src/cuchaz/enigma/analysis/EntryRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/EntryRenamer.java | 199 |
1 files changed, 199 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..4b2c0b7 --- /dev/null +++ b/src/cuchaz/enigma/analysis/EntryRenamer.java | |||
| @@ -0,0 +1,199 @@ | |||
| 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.Iterator; | ||
| 15 | import java.util.List; | ||
| 16 | import java.util.Map; | ||
| 17 | import java.util.Set; | ||
| 18 | |||
| 19 | import com.beust.jcommander.internal.Lists; | ||
| 20 | import com.google.common.collect.Multimap; | ||
| 21 | import com.google.common.collect.Sets; | ||
| 22 | |||
| 23 | import cuchaz.enigma.mapping.ArgumentEntry; | ||
| 24 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 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 | |||
| 30 | public class EntryRenamer | ||
| 31 | { | ||
| 32 | public static <T> void renameClassesInSet( Map<String,String> renames, Set<T> set ) | ||
| 33 | { | ||
| 34 | List<T> entries = Lists.newArrayList(); | ||
| 35 | for( T val : set ) | ||
| 36 | { | ||
| 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 | { | ||
| 45 | // for each key/value pair... | ||
| 46 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 47 | for( Map.Entry<Key,Val> entry : map.entrySet() ) | ||
| 48 | { | ||
| 49 | entriesToAdd.add( new AbstractMap.SimpleEntry<Key,Val>( | ||
| 50 | renameClassesInThing( renames, entry.getKey() ), | ||
| 51 | renameClassesInThing( renames, entry.getValue() ) | ||
| 52 | ) ); | ||
| 53 | } | ||
| 54 | map.clear(); | ||
| 55 | for( Map.Entry<Key,Val> entry : entriesToAdd ) | ||
| 56 | { | ||
| 57 | map.put( entry.getKey(), entry.getValue() ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | public static <Key,Val> void renameClassesInMultimap( Map<String,String> renames, Multimap<Key,Val> map ) | ||
| 62 | { | ||
| 63 | // for each key/value pair... | ||
| 64 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 65 | for( Map.Entry<Key,Val> entry : map.entries() ) | ||
| 66 | { | ||
| 67 | entriesToAdd.add( new AbstractMap.SimpleEntry<Key,Val>( | ||
| 68 | renameClassesInThing( renames, entry.getKey() ), | ||
| 69 | renameClassesInThing( renames, entry.getValue() ) | ||
| 70 | ) ); | ||
| 71 | } | ||
| 72 | map.clear(); | ||
| 73 | for( Map.Entry<Key,Val> entry : entriesToAdd ) | ||
| 74 | { | ||
| 75 | map.put( entry.getKey(), entry.getValue() ); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | public static <Key,Val> void renameMethodsInMultimap( Map<MethodEntry,MethodEntry> renames, Multimap<Key,Val> map ) | ||
| 80 | { | ||
| 81 | // for each key/value pair... | ||
| 82 | Set<Map.Entry<Key,Val>> entriesToAdd = Sets.newHashSet(); | ||
| 83 | Iterator<Map.Entry<Key,Val>> iter = map.entries().iterator(); | ||
| 84 | while( iter.hasNext() ) | ||
| 85 | { | ||
| 86 | Map.Entry<Key,Val> entry = iter.next(); | ||
| 87 | iter.remove(); | ||
| 88 | entriesToAdd.add( new AbstractMap.SimpleEntry<Key,Val>( | ||
| 89 | renameMethodsInThing( renames, entry.getKey() ), | ||
| 90 | renameMethodsInThing( renames, entry.getValue() ) | ||
| 91 | ) ); | ||
| 92 | } | ||
| 93 | for( Map.Entry<Key,Val> entry : entriesToAdd ) | ||
| 94 | { | ||
| 95 | map.put( entry.getKey(), entry.getValue() ); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | @SuppressWarnings( "unchecked" ) | ||
| 100 | public static <T> T renameMethodsInThing( Map<MethodEntry,MethodEntry> renames, T thing ) | ||
| 101 | { | ||
| 102 | if( thing instanceof MethodEntry ) | ||
| 103 | { | ||
| 104 | MethodEntry methodEntry = (MethodEntry)thing; | ||
| 105 | MethodEntry newMethodEntry = renames.get( methodEntry ); | ||
| 106 | if( newMethodEntry != null ) | ||
| 107 | { | ||
| 108 | return (T)new MethodEntry( | ||
| 109 | methodEntry.getClassEntry(), | ||
| 110 | newMethodEntry.getName(), | ||
| 111 | methodEntry.getSignature() | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | return thing; | ||
| 115 | } | ||
| 116 | else if( thing instanceof ArgumentEntry ) | ||
| 117 | { | ||
| 118 | ArgumentEntry argumentEntry = (ArgumentEntry)thing; | ||
| 119 | return (T)new ArgumentEntry( | ||
| 120 | renameMethodsInThing( renames, argumentEntry.getMethodEntry() ), | ||
| 121 | argumentEntry.getIndex(), | ||
| 122 | argumentEntry.getName() | ||
| 123 | ); | ||
| 124 | } | ||
| 125 | else if( thing instanceof EntryReference ) | ||
| 126 | { | ||
| 127 | EntryReference<Entry,Entry> reference = (EntryReference<Entry,Entry>)thing; | ||
| 128 | reference.entry = renameMethodsInThing( renames, reference.entry ); | ||
| 129 | reference.context = renameMethodsInThing( renames, reference.context ); | ||
| 130 | return thing; | ||
| 131 | } | ||
| 132 | return thing; | ||
| 133 | } | ||
| 134 | |||
| 135 | @SuppressWarnings( "unchecked" ) | ||
| 136 | public static <T> T renameClassesInThing( Map<String,String> renames, T thing ) | ||
| 137 | { | ||
| 138 | if( thing instanceof String ) | ||
| 139 | { | ||
| 140 | String stringEntry = (String)thing; | ||
| 141 | if( renames.containsKey( stringEntry ) ) | ||
| 142 | { | ||
| 143 | return (T)renames.get( stringEntry ); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | else if( thing instanceof ClassEntry ) | ||
| 147 | { | ||
| 148 | ClassEntry classEntry = (ClassEntry)thing; | ||
| 149 | return (T)new ClassEntry( renameClassesInThing( renames, classEntry.getClassName() ) ); | ||
| 150 | } | ||
| 151 | else if( thing instanceof FieldEntry ) | ||
| 152 | { | ||
| 153 | FieldEntry fieldEntry = (FieldEntry)thing; | ||
| 154 | return (T)new FieldEntry( | ||
| 155 | renameClassesInThing( renames, fieldEntry.getClassEntry() ), | ||
| 156 | fieldEntry.getName() | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | else if( thing instanceof ConstructorEntry ) | ||
| 160 | { | ||
| 161 | ConstructorEntry constructorEntry = (ConstructorEntry)thing; | ||
| 162 | return (T)new ConstructorEntry( | ||
| 163 | renameClassesInThing( renames, constructorEntry.getClassEntry() ), | ||
| 164 | constructorEntry.getSignature() | ||
| 165 | ); | ||
| 166 | } | ||
| 167 | else if( thing instanceof MethodEntry ) | ||
| 168 | { | ||
| 169 | MethodEntry methodEntry = (MethodEntry)thing; | ||
| 170 | return (T)new MethodEntry( | ||
| 171 | renameClassesInThing( renames, methodEntry.getClassEntry() ), | ||
| 172 | methodEntry.getName(), | ||
| 173 | methodEntry.getSignature() | ||
| 174 | ); | ||
| 175 | } | ||
| 176 | else if( thing instanceof ArgumentEntry ) | ||
| 177 | { | ||
| 178 | ArgumentEntry argumentEntry = (ArgumentEntry)thing; | ||
| 179 | return (T)new ArgumentEntry( | ||
| 180 | renameClassesInThing( renames, argumentEntry.getMethodEntry() ), | ||
| 181 | argumentEntry.getIndex(), | ||
| 182 | argumentEntry.getName() | ||
| 183 | ); | ||
| 184 | } | ||
| 185 | else if( thing instanceof EntryReference ) | ||
| 186 | { | ||
| 187 | EntryReference<Entry,Entry> reference = (EntryReference<Entry,Entry>)thing; | ||
| 188 | reference.entry = renameClassesInThing( renames, reference.entry ); | ||
| 189 | reference.context = renameClassesInThing( renames, reference.context ); | ||
| 190 | return thing; | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | throw new Error( "Not an entry: " + thing ); | ||
| 195 | } | ||
| 196 | |||
| 197 | return thing; | ||
| 198 | } | ||
| 199 | } | ||