diff options
| author | 2016-07-04 18:14:22 +1000 | |
|---|---|---|
| committer | 2016-07-04 18:14:22 +1000 | |
| commit | 59e189bef2b5e6d129fb7c2c988ed0b2130e36ac (patch) | |
| tree | 2b638e60905251de85a4917152d6fc39a4112194 /src/main/java/cuchaz/enigma/convert | |
| parent | Fixed Obf Class list order (diff) | |
| download | enigma-fork-59e189bef2b5e6d129fb7c2c988ed0b2130e36ac.tar.gz enigma-fork-59e189bef2b5e6d129fb7c2c988ed0b2130e36ac.tar.xz enigma-fork-59e189bef2b5e6d129fb7c2c988ed0b2130e36ac.zip | |
Reformat
Diffstat (limited to 'src/main/java/cuchaz/enigma/convert')
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassForest.java | 54 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassIdentifier.java | 54 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassIdentity.java | 441 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassMatch.java | 84 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassMatches.java | 149 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassMatching.java | 146 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/ClassNamer.java | 56 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/MappingsConverter.java | 540 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/MatchesReader.java | 104 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/MatchesWriter.java | 121 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/convert/MemberMatches.java | 143 |
11 files changed, 0 insertions, 1892 deletions
diff --git a/src/main/java/cuchaz/enigma/convert/ClassForest.java b/src/main/java/cuchaz/enigma/convert/ClassForest.java deleted file mode 100644 index c9b44b3..0000000 --- a/src/main/java/cuchaz/enigma/convert/ClassForest.java +++ /dev/null | |||
| @@ -1,54 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.HashMultimap; | ||
| 14 | import com.google.common.collect.Multimap; | ||
| 15 | |||
| 16 | import java.util.Collection; | ||
| 17 | |||
| 18 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 19 | |||
| 20 | |||
| 21 | public class ClassForest { | ||
| 22 | |||
| 23 | private ClassIdentifier identifier; | ||
| 24 | private Multimap<ClassIdentity, ClassEntry> forest; | ||
| 25 | |||
| 26 | public ClassForest(ClassIdentifier identifier) { | ||
| 27 | this.identifier = identifier; | ||
| 28 | this.forest = HashMultimap.create(); | ||
| 29 | } | ||
| 30 | |||
| 31 | public void add(ClassEntry entry) { | ||
| 32 | try { | ||
| 33 | this.forest.put(this.identifier.identify(entry), entry); | ||
| 34 | } catch (ClassNotFoundException ex) { | ||
| 35 | throw new Error("Unable to find class " + entry.getName()); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | public Collection<ClassIdentity> identities() { | ||
| 40 | return this.forest.keySet(); | ||
| 41 | } | ||
| 42 | |||
| 43 | public Collection<ClassEntry> classes() { | ||
| 44 | return this.forest.values(); | ||
| 45 | } | ||
| 46 | |||
| 47 | public Collection<ClassEntry> getClasses(ClassIdentity identity) { | ||
| 48 | return this.forest.get(identity); | ||
| 49 | } | ||
| 50 | |||
| 51 | public boolean containsIdentity(ClassIdentity identity) { | ||
| 52 | return this.forest.containsKey(identity); | ||
| 53 | } | ||
| 54 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/ClassIdentifier.java b/src/main/java/cuchaz/enigma/convert/ClassIdentifier.java deleted file mode 100644 index cc7f25b..0000000 --- a/src/main/java/cuchaz/enigma/convert/ClassIdentifier.java +++ /dev/null | |||
| @@ -1,54 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.Maps; | ||
| 14 | |||
| 15 | import java.util.Map; | ||
| 16 | import java.util.jar.JarFile; | ||
| 17 | |||
| 18 | import cuchaz.enigma.TranslatingTypeLoader; | ||
| 19 | import cuchaz.enigma.analysis.JarIndex; | ||
| 20 | import cuchaz.enigma.convert.ClassNamer.SidedClassNamer; | ||
| 21 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 22 | import javassist.CtClass; | ||
| 23 | |||
| 24 | |||
| 25 | public class ClassIdentifier { | ||
| 26 | |||
| 27 | private JarIndex index; | ||
| 28 | private SidedClassNamer namer; | ||
| 29 | private boolean useReferences; | ||
| 30 | private TranslatingTypeLoader loader; | ||
| 31 | private Map<ClassEntry, ClassIdentity> cache; | ||
| 32 | |||
| 33 | public ClassIdentifier(JarFile jar, JarIndex index, SidedClassNamer namer, boolean useReferences) { | ||
| 34 | this.index = index; | ||
| 35 | this.namer = namer; | ||
| 36 | this.useReferences = useReferences; | ||
| 37 | this.loader = new TranslatingTypeLoader(jar, index); | ||
| 38 | this.cache = Maps.newHashMap(); | ||
| 39 | } | ||
| 40 | |||
| 41 | public ClassIdentity identify(ClassEntry classEntry) | ||
| 42 | throws ClassNotFoundException { | ||
| 43 | ClassIdentity identity = this.cache.get(classEntry); | ||
| 44 | if (identity == null) { | ||
| 45 | CtClass c = this.loader.loadClass(classEntry.getName()); | ||
| 46 | if (c == null) { | ||
| 47 | throw new ClassNotFoundException(classEntry.getName()); | ||
| 48 | } | ||
| 49 | identity = new ClassIdentity(c, this.namer, this.index, this.useReferences); | ||
| 50 | this.cache.put(classEntry, identity); | ||
| 51 | } | ||
| 52 | return identity; | ||
| 53 | } | ||
| 54 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/ClassIdentity.java b/src/main/java/cuchaz/enigma/convert/ClassIdentity.java deleted file mode 100644 index 606c1df..0000000 --- a/src/main/java/cuchaz/enigma/convert/ClassIdentity.java +++ /dev/null | |||
| @@ -1,441 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.*; | ||
| 14 | |||
| 15 | import java.io.UnsupportedEncodingException; | ||
| 16 | import java.security.MessageDigest; | ||
| 17 | import java.security.NoSuchAlgorithmException; | ||
| 18 | import java.util.Enumeration; | ||
| 19 | import java.util.List; | ||
| 20 | import java.util.Map; | ||
| 21 | import java.util.Set; | ||
| 22 | |||
| 23 | import cuchaz.enigma.Constants; | ||
| 24 | import cuchaz.enigma.Util; | ||
| 25 | import cuchaz.enigma.analysis.ClassImplementationsTreeNode; | ||
| 26 | import cuchaz.enigma.analysis.EntryReference; | ||
| 27 | import cuchaz.enigma.analysis.JarIndex; | ||
| 28 | import cuchaz.enigma.bytecode.ConstPoolEditor; | ||
| 29 | import cuchaz.enigma.bytecode.InfoType; | ||
| 30 | import cuchaz.enigma.bytecode.accessors.ConstInfoAccessor; | ||
| 31 | import cuchaz.enigma.convert.ClassNamer.SidedClassNamer; | ||
| 32 | import cuchaz.enigma.mapping.*; | ||
| 33 | import javassist.*; | ||
| 34 | import javassist.bytecode.*; | ||
| 35 | import javassist.expr.*; | ||
| 36 | |||
| 37 | public class ClassIdentity { | ||
| 38 | |||
| 39 | private ClassEntry classEntry; | ||
| 40 | private SidedClassNamer namer; | ||
| 41 | private Multiset<String> fields; | ||
| 42 | private Multiset<String> methods; | ||
| 43 | private Multiset<String> constructors; | ||
| 44 | private String staticInitializer; | ||
| 45 | private String extendz; | ||
| 46 | private Multiset<String> implementz; | ||
| 47 | private Set<String> stringLiterals; | ||
| 48 | private Multiset<String> implementations; | ||
| 49 | private Multiset<String> references; | ||
| 50 | private String outer; | ||
| 51 | |||
| 52 | private final ClassNameReplacer m_classNameReplacer = new ClassNameReplacer() { | ||
| 53 | |||
| 54 | private Map<String, String> m_classNames = Maps.newHashMap(); | ||
| 55 | |||
| 56 | @Override | ||
| 57 | public String replace(String className) { | ||
| 58 | |||
| 59 | // classes not in the none package can be passed through | ||
| 60 | ClassEntry classEntry = new ClassEntry(className); | ||
| 61 | if (!classEntry.getPackageName().equals(Constants.NONE_PACKAGE)) { | ||
| 62 | return className; | ||
| 63 | } | ||
| 64 | |||
| 65 | // is this class ourself? | ||
| 66 | if (className.equals(classEntry.getName())) { | ||
| 67 | return "CSelf"; | ||
| 68 | } | ||
| 69 | |||
| 70 | // try the namer | ||
| 71 | if (namer != null) { | ||
| 72 | String newName = namer.getName(className); | ||
| 73 | if (newName != null) { | ||
| 74 | return newName; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | // otherwise, use local naming | ||
| 79 | if (!m_classNames.containsKey(className)) { | ||
| 80 | m_classNames.put(className, getNewClassName()); | ||
| 81 | } | ||
| 82 | return m_classNames.get(className); | ||
| 83 | } | ||
| 84 | |||
| 85 | private String getNewClassName() { | ||
| 86 | return String.format("C%03d", m_classNames.size()); | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | public ClassIdentity(CtClass c, SidedClassNamer namer, JarIndex index, boolean useReferences) { | ||
| 91 | this.namer = namer; | ||
| 92 | |||
| 93 | // stuff from the bytecode | ||
| 94 | |||
| 95 | this.classEntry = EntryFactory.getClassEntry(c); | ||
| 96 | this.fields = HashMultiset.create(); | ||
| 97 | for (CtField field : c.getDeclaredFields()) { | ||
| 98 | this.fields.add(scrubType(field.getSignature())); | ||
| 99 | } | ||
| 100 | this.methods = HashMultiset.create(); | ||
| 101 | for (CtMethod method : c.getDeclaredMethods()) { | ||
| 102 | this.methods.add(scrubSignature(method.getSignature()) + "0x" + getBehaviorSignature(method)); | ||
| 103 | } | ||
| 104 | this.constructors = HashMultiset.create(); | ||
| 105 | for (CtConstructor constructor : c.getDeclaredConstructors()) { | ||
| 106 | this.constructors.add(scrubSignature(constructor.getSignature()) + "0x" + getBehaviorSignature(constructor)); | ||
| 107 | } | ||
| 108 | this.staticInitializer = ""; | ||
| 109 | if (c.getClassInitializer() != null) { | ||
| 110 | this.staticInitializer = getBehaviorSignature(c.getClassInitializer()); | ||
| 111 | } | ||
| 112 | this.extendz = ""; | ||
| 113 | if (c.getClassFile().getSuperclass() != null) { | ||
| 114 | this.extendz = scrubClassName(Descriptor.toJvmName(c.getClassFile().getSuperclass())); | ||
| 115 | } | ||
| 116 | this.implementz = HashMultiset.create(); | ||
| 117 | for (String interfaceName : c.getClassFile().getInterfaces()) { | ||
| 118 | this.implementz.add(scrubClassName(Descriptor.toJvmName(interfaceName))); | ||
| 119 | } | ||
| 120 | |||
| 121 | this.stringLiterals = Sets.newHashSet(); | ||
| 122 | ConstPool constants = c.getClassFile().getConstPool(); | ||
| 123 | for (int i = 1; i < constants.getSize(); i++) { | ||
| 124 | if (constants.getTag(i) == ConstPool.CONST_String) { | ||
| 125 | this.stringLiterals.add(constants.getStringInfo(i)); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | // stuff from the jar index | ||
| 130 | |||
| 131 | this.implementations = HashMultiset.create(); | ||
| 132 | ClassImplementationsTreeNode implementationsNode = index.getClassImplementations(null, this.classEntry); | ||
| 133 | if (implementationsNode != null) { | ||
| 134 | @SuppressWarnings("unchecked") | ||
| 135 | Enumeration<ClassImplementationsTreeNode> implementations = implementationsNode.children(); | ||
| 136 | while (implementations.hasMoreElements()) { | ||
| 137 | ClassImplementationsTreeNode node = implementations.nextElement(); | ||
| 138 | this.implementations.add(scrubClassName(node.getClassEntry().getName())); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | this.references = HashMultiset.create(); | ||
| 143 | if (useReferences) { | ||
| 144 | for (CtField field : c.getDeclaredFields()) { | ||
| 145 | FieldEntry fieldEntry = EntryFactory.getFieldEntry(field); | ||
| 146 | index.getFieldReferences(fieldEntry).forEach(this::addReference); | ||
| 147 | } | ||
| 148 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { | ||
| 149 | BehaviorEntry behaviorEntry = EntryFactory.getBehaviorEntry(behavior); | ||
| 150 | index.getBehaviorReferences(behaviorEntry).forEach(this::addReference); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | this.outer = null; | ||
| 155 | if (this.classEntry.isInnerClass()) { | ||
| 156 | this.outer = this.classEntry.getOuterClassName(); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | private void addReference(EntryReference<? extends Entry, BehaviorEntry> reference) { | ||
| 161 | if (reference.context.getSignature() != null) { | ||
| 162 | this.references.add(String.format("%s_%s", | ||
| 163 | scrubClassName(reference.context.getClassName()), | ||
| 164 | scrubSignature(reference.context.getSignature()) | ||
| 165 | )); | ||
| 166 | } else { | ||
| 167 | this.references.add(String.format("%s_<clinit>", | ||
| 168 | scrubClassName(reference.context.getClassName()) | ||
| 169 | )); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | public ClassEntry getClassEntry() { | ||
| 174 | return this.classEntry; | ||
| 175 | } | ||
| 176 | |||
| 177 | @Override | ||
| 178 | public String toString() { | ||
| 179 | StringBuilder buf = new StringBuilder(); | ||
| 180 | buf.append("class: "); | ||
| 181 | buf.append(this.classEntry.getName()); | ||
| 182 | buf.append(" "); | ||
| 183 | buf.append(hashCode()); | ||
| 184 | buf.append("\n"); | ||
| 185 | for (String field : this.fields) { | ||
| 186 | buf.append("\tfield "); | ||
| 187 | buf.append(field); | ||
| 188 | buf.append("\n"); | ||
| 189 | } | ||
| 190 | for (String method : this.methods) { | ||
| 191 | buf.append("\tmethod "); | ||
| 192 | buf.append(method); | ||
| 193 | buf.append("\n"); | ||
| 194 | } | ||
| 195 | for (String constructor : this.constructors) { | ||
| 196 | buf.append("\tconstructor "); | ||
| 197 | buf.append(constructor); | ||
| 198 | buf.append("\n"); | ||
| 199 | } | ||
| 200 | if (this.staticInitializer.length() > 0) { | ||
| 201 | buf.append("\tinitializer "); | ||
| 202 | buf.append(this.staticInitializer); | ||
| 203 | buf.append("\n"); | ||
| 204 | } | ||
| 205 | if (this.extendz.length() > 0) { | ||
| 206 | buf.append("\textends "); | ||
| 207 | buf.append(this.extendz); | ||
| 208 | buf.append("\n"); | ||
| 209 | } | ||
| 210 | for (String interfaceName : this.implementz) { | ||
| 211 | buf.append("\timplements "); | ||
| 212 | buf.append(interfaceName); | ||
| 213 | buf.append("\n"); | ||
| 214 | } | ||
| 215 | for (String implementation : this.implementations) { | ||
| 216 | buf.append("\timplemented by "); | ||
| 217 | buf.append(implementation); | ||
| 218 | buf.append("\n"); | ||
| 219 | } | ||
| 220 | for (String reference : this.references) { | ||
| 221 | buf.append("\treference "); | ||
| 222 | buf.append(reference); | ||
| 223 | buf.append("\n"); | ||
| 224 | } | ||
| 225 | buf.append("\touter "); | ||
| 226 | buf.append(this.outer); | ||
| 227 | buf.append("\n"); | ||
| 228 | return buf.toString(); | ||
| 229 | } | ||
| 230 | |||
| 231 | private String scrubClassName(String className) { | ||
| 232 | return m_classNameReplacer.replace(className); | ||
| 233 | } | ||
| 234 | |||
| 235 | private String scrubType(String typeName) { | ||
| 236 | return scrubType(new Type(typeName)).toString(); | ||
| 237 | } | ||
| 238 | |||
| 239 | private Type scrubType(Type type) { | ||
| 240 | if (type.hasClass()) { | ||
| 241 | return new Type(type, m_classNameReplacer); | ||
| 242 | } else { | ||
| 243 | return type; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | private String scrubSignature(String signature) { | ||
| 248 | return scrubSignature(new Signature(signature)).toString(); | ||
| 249 | } | ||
| 250 | |||
| 251 | private Signature scrubSignature(Signature signature) { | ||
| 252 | return new Signature(signature, m_classNameReplacer); | ||
| 253 | } | ||
| 254 | |||
| 255 | private boolean isClassMatchedUniquely(String className) { | ||
| 256 | return this.namer != null && this.namer.getName(Descriptor.toJvmName(className)) != null; | ||
| 257 | } | ||
| 258 | |||
| 259 | private String getBehaviorSignature(CtBehavior behavior) { | ||
| 260 | try { | ||
| 261 | // does this method have an implementation? | ||
| 262 | if (behavior.getMethodInfo().getCodeAttribute() == null) { | ||
| 263 | return "(none)"; | ||
| 264 | } | ||
| 265 | |||
| 266 | // compute the hash from the opcodes | ||
| 267 | ConstPool constants = behavior.getMethodInfo().getConstPool(); | ||
| 268 | final MessageDigest digest = MessageDigest.getInstance("MD5"); | ||
| 269 | CodeIterator iter = behavior.getMethodInfo().getCodeAttribute().iterator(); | ||
| 270 | while (iter.hasNext()) { | ||
| 271 | int pos = iter.next(); | ||
| 272 | |||
| 273 | // update the hash with the opcode | ||
| 274 | int opcode = iter.byteAt(pos); | ||
| 275 | digest.update((byte) opcode); | ||
| 276 | |||
| 277 | switch (opcode) { | ||
| 278 | case Opcode.LDC: { | ||
| 279 | int constIndex = iter.byteAt(pos + 1); | ||
| 280 | updateHashWithConstant(digest, constants, constIndex); | ||
| 281 | } | ||
| 282 | break; | ||
| 283 | |||
| 284 | case Opcode.LDC_W: | ||
| 285 | case Opcode.LDC2_W: { | ||
| 286 | int constIndex = (iter.byteAt(pos + 1) << 8) | iter.byteAt(pos + 2); | ||
| 287 | updateHashWithConstant(digest, constants, constIndex); | ||
| 288 | } | ||
| 289 | break; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | // update hash with method and field accesses | ||
| 294 | behavior.instrument(new ExprEditor() { | ||
| 295 | @Override | ||
| 296 | public void edit(MethodCall call) { | ||
| 297 | updateHashWithString(digest, scrubClassName(Descriptor.toJvmName(call.getClassName()))); | ||
| 298 | updateHashWithString(digest, scrubSignature(call.getSignature())); | ||
| 299 | if (isClassMatchedUniquely(call.getClassName())) { | ||
| 300 | updateHashWithString(digest, call.getMethodName()); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | @Override | ||
| 305 | public void edit(FieldAccess access) { | ||
| 306 | updateHashWithString(digest, scrubClassName(Descriptor.toJvmName(access.getClassName()))); | ||
| 307 | updateHashWithString(digest, scrubType(access.getSignature())); | ||
| 308 | if (isClassMatchedUniquely(access.getClassName())) { | ||
| 309 | updateHashWithString(digest, access.getFieldName()); | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | @Override | ||
| 314 | public void edit(ConstructorCall call) { | ||
| 315 | updateHashWithString(digest, scrubClassName(Descriptor.toJvmName(call.getClassName()))); | ||
| 316 | updateHashWithString(digest, scrubSignature(call.getSignature())); | ||
| 317 | } | ||
| 318 | |||
| 319 | @Override | ||
| 320 | public void edit(NewExpr expr) { | ||
| 321 | updateHashWithString(digest, scrubClassName(Descriptor.toJvmName(expr.getClassName()))); | ||
| 322 | } | ||
| 323 | }); | ||
| 324 | |||
| 325 | // convert the hash to a hex string | ||
| 326 | return toHex(digest.digest()); | ||
| 327 | } catch (BadBytecode | NoSuchAlgorithmException | CannotCompileException ex) { | ||
| 328 | throw new Error(ex); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | private void updateHashWithConstant(MessageDigest digest, ConstPool constants, int index) { | ||
| 333 | ConstPoolEditor editor = new ConstPoolEditor(constants); | ||
| 334 | ConstInfoAccessor item = editor.getItem(index); | ||
| 335 | if (item.getType() == InfoType.StringInfo) { | ||
| 336 | updateHashWithString(digest, constants.getStringInfo(index)); | ||
| 337 | } | ||
| 338 | // TODO: other constants | ||
| 339 | } | ||
| 340 | |||
| 341 | private void updateHashWithString(MessageDigest digest, String val) { | ||
| 342 | try { | ||
| 343 | digest.update(val.getBytes("UTF8")); | ||
| 344 | } catch (UnsupportedEncodingException ex) { | ||
| 345 | throw new Error(ex); | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | private String toHex(byte[] bytes) { | ||
| 350 | // function taken from: | ||
| 351 | // http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java | ||
| 352 | final char[] hexArray = "0123456789ABCDEF".toCharArray(); | ||
| 353 | char[] hexChars = new char[bytes.length * 2]; | ||
| 354 | for (int j = 0; j < bytes.length; j++) { | ||
| 355 | int v = bytes[j] & 0xFF; | ||
| 356 | hexChars[j * 2] = hexArray[v >>> 4]; | ||
| 357 | hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | ||
| 358 | } | ||
| 359 | return new String(hexChars); | ||
| 360 | } | ||
| 361 | |||
| 362 | @Override | ||
| 363 | public boolean equals(Object other) { | ||
| 364 | return other instanceof ClassIdentity && equals((ClassIdentity) other); | ||
| 365 | } | ||
| 366 | |||
| 367 | public boolean equals(ClassIdentity other) { | ||
| 368 | return this.fields.equals(other.fields) | ||
| 369 | && this.methods.equals(other.methods) | ||
| 370 | && this.constructors.equals(other.constructors) | ||
| 371 | && this.staticInitializer.equals(other.staticInitializer) | ||
| 372 | && this.extendz.equals(other.extendz) | ||
| 373 | && this.implementz.equals(other.implementz) | ||
| 374 | && this.implementations.equals(other.implementations) | ||
| 375 | && this.references.equals(other.references); | ||
| 376 | } | ||
| 377 | |||
| 378 | @Override | ||
| 379 | public int hashCode() { | ||
| 380 | List<Object> objs = Lists.newArrayList(); | ||
| 381 | objs.addAll(this.fields); | ||
| 382 | objs.addAll(this.methods); | ||
| 383 | objs.addAll(this.constructors); | ||
| 384 | objs.add(this.staticInitializer); | ||
| 385 | objs.add(this.extendz); | ||
| 386 | objs.addAll(this.implementz); | ||
| 387 | objs.addAll(this.implementations); | ||
| 388 | objs.addAll(this.references); | ||
| 389 | return Util.combineHashesOrdered(objs); | ||
| 390 | } | ||
| 391 | |||
| 392 | public int getMatchScore(ClassIdentity other) { | ||
| 393 | return 2 * getNumMatches(this.extendz, other.extendz) | ||
| 394 | + 2 * getNumMatches(this.outer, other.outer) | ||
| 395 | + 2 * getNumMatches(this.implementz, other.implementz) | ||
| 396 | + getNumMatches(this.stringLiterals, other.stringLiterals) | ||
| 397 | + getNumMatches(this.fields, other.fields) | ||
| 398 | + getNumMatches(this.methods, other.methods) | ||
| 399 | + getNumMatches(this.constructors, other.constructors); | ||
| 400 | } | ||
| 401 | |||
| 402 | public int getMaxMatchScore() { | ||
| 403 | return 2 + 2 + 2 * this.implementz.size() + this.stringLiterals.size() + this.fields.size() + this.methods.size() + this.constructors.size(); | ||
| 404 | } | ||
| 405 | |||
| 406 | public boolean matches(CtClass c) { | ||
| 407 | // just compare declaration counts | ||
| 408 | return this.fields.size() == c.getDeclaredFields().length | ||
| 409 | && this.methods.size() == c.getDeclaredMethods().length | ||
| 410 | && this.constructors.size() == c.getDeclaredConstructors().length; | ||
| 411 | } | ||
| 412 | |||
| 413 | private int getNumMatches(Set<String> a, Set<String> b) { | ||
| 414 | int numMatches = 0; | ||
| 415 | for (String val : a) { | ||
| 416 | if (b.contains(val)) { | ||
| 417 | numMatches++; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | return numMatches; | ||
| 421 | } | ||
| 422 | |||
| 423 | private int getNumMatches(Multiset<String> a, Multiset<String> b) { | ||
| 424 | int numMatches = 0; | ||
| 425 | for (String val : a) { | ||
| 426 | if (b.contains(val)) { | ||
| 427 | numMatches++; | ||
| 428 | } | ||
| 429 | } | ||
| 430 | return numMatches; | ||
| 431 | } | ||
| 432 | |||
| 433 | private int getNumMatches(String a, String b) { | ||
| 434 | if (a == null && b == null) { | ||
| 435 | return 1; | ||
| 436 | } else if (a != null && b != null && a.equals(b)) { | ||
| 437 | return 1; | ||
| 438 | } | ||
| 439 | return 0; | ||
| 440 | } | ||
| 441 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/ClassMatch.java b/src/main/java/cuchaz/enigma/convert/ClassMatch.java deleted file mode 100644 index 422529e..0000000 --- a/src/main/java/cuchaz/enigma/convert/ClassMatch.java +++ /dev/null | |||
| @@ -1,84 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.Sets; | ||
| 14 | |||
| 15 | import java.util.Collection; | ||
| 16 | import java.util.Set; | ||
| 17 | |||
| 18 | import cuchaz.enigma.Util; | ||
| 19 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 20 | |||
| 21 | |||
| 22 | public class ClassMatch { | ||
| 23 | |||
| 24 | public Set<ClassEntry> sourceClasses; | ||
| 25 | public Set<ClassEntry> destClasses; | ||
| 26 | |||
| 27 | public ClassMatch(Collection<ClassEntry> sourceClasses, Collection<ClassEntry> destClasses) { | ||
| 28 | this.sourceClasses = Sets.newHashSet(sourceClasses); | ||
| 29 | this.destClasses = Sets.newHashSet(destClasses); | ||
| 30 | } | ||
| 31 | |||
| 32 | public ClassMatch(ClassEntry sourceClass, ClassEntry destClass) { | ||
| 33 | sourceClasses = Sets.newHashSet(); | ||
| 34 | if (sourceClass != null) { | ||
| 35 | sourceClasses.add(sourceClass); | ||
| 36 | } | ||
| 37 | destClasses = Sets.newHashSet(); | ||
| 38 | if (destClass != null) { | ||
| 39 | destClasses.add(destClass); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | public boolean isMatched() { | ||
| 44 | return sourceClasses.size() > 0 && destClasses.size() > 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | public boolean isAmbiguous() { | ||
| 48 | return sourceClasses.size() > 1 || destClasses.size() > 1; | ||
| 49 | } | ||
| 50 | |||
| 51 | public ClassEntry getUniqueSource() { | ||
| 52 | if (sourceClasses.size() != 1) { | ||
| 53 | throw new IllegalStateException("Match has ambiguous source!"); | ||
| 54 | } | ||
| 55 | return sourceClasses.iterator().next(); | ||
| 56 | } | ||
| 57 | |||
| 58 | public ClassEntry getUniqueDest() { | ||
| 59 | if (destClasses.size() != 1) { | ||
| 60 | throw new IllegalStateException("Match has ambiguous source!"); | ||
| 61 | } | ||
| 62 | return destClasses.iterator().next(); | ||
| 63 | } | ||
| 64 | |||
| 65 | public Set<ClassEntry> intersectSourceClasses(Set<ClassEntry> classes) { | ||
| 66 | Set<ClassEntry> intersection = Sets.newHashSet(sourceClasses); | ||
| 67 | intersection.retainAll(classes); | ||
| 68 | return intersection; | ||
| 69 | } | ||
| 70 | |||
| 71 | @Override | ||
| 72 | public int hashCode() { | ||
| 73 | return Util.combineHashesOrdered(sourceClasses, destClasses); | ||
| 74 | } | ||
| 75 | |||
| 76 | @Override | ||
| 77 | public boolean equals(Object other) { | ||
| 78 | return other instanceof ClassMatch && equals((ClassMatch) other); | ||
| 79 | } | ||
| 80 | |||
| 81 | public boolean equals(ClassMatch other) { | ||
| 82 | return this.sourceClasses.equals(other.sourceClasses) && this.destClasses.equals(other.destClasses); | ||
| 83 | } | ||
| 84 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/ClassMatches.java b/src/main/java/cuchaz/enigma/convert/ClassMatches.java deleted file mode 100644 index 851c082..0000000 --- a/src/main/java/cuchaz/enigma/convert/ClassMatches.java +++ /dev/null | |||
| @@ -1,149 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.BiMap; | ||
| 14 | import com.google.common.collect.HashBiMap; | ||
| 15 | import com.google.common.collect.Maps; | ||
| 16 | import com.google.common.collect.Sets; | ||
| 17 | |||
| 18 | import java.util.*; | ||
| 19 | |||
| 20 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 21 | |||
| 22 | |||
| 23 | public class ClassMatches implements Iterable<ClassMatch> { | ||
| 24 | |||
| 25 | Collection<ClassMatch> m_matches; | ||
| 26 | Map<ClassEntry, ClassMatch> m_matchesBySource; | ||
| 27 | Map<ClassEntry, ClassMatch> m_matchesByDest; | ||
| 28 | BiMap<ClassEntry, ClassEntry> m_uniqueMatches; | ||
| 29 | Map<ClassEntry, ClassMatch> m_ambiguousMatchesBySource; | ||
| 30 | Map<ClassEntry, ClassMatch> m_ambiguousMatchesByDest; | ||
| 31 | Set<ClassEntry> m_unmatchedSourceClasses; | ||
| 32 | Set<ClassEntry> m_unmatchedDestClasses; | ||
| 33 | |||
| 34 | public ClassMatches() { | ||
| 35 | this(new ArrayList<>()); | ||
| 36 | } | ||
| 37 | |||
| 38 | public ClassMatches(Collection<ClassMatch> matches) { | ||
| 39 | m_matches = matches; | ||
| 40 | m_matchesBySource = Maps.newHashMap(); | ||
| 41 | m_matchesByDest = Maps.newHashMap(); | ||
| 42 | m_uniqueMatches = HashBiMap.create(); | ||
| 43 | m_ambiguousMatchesBySource = Maps.newHashMap(); | ||
| 44 | m_ambiguousMatchesByDest = Maps.newHashMap(); | ||
| 45 | m_unmatchedSourceClasses = Sets.newHashSet(); | ||
| 46 | m_unmatchedDestClasses = Sets.newHashSet(); | ||
| 47 | |||
| 48 | matches.forEach(this::indexMatch); | ||
| 49 | } | ||
| 50 | |||
| 51 | public void add(ClassMatch match) { | ||
| 52 | m_matches.add(match); | ||
| 53 | indexMatch(match); | ||
| 54 | } | ||
| 55 | |||
| 56 | public void remove(ClassMatch match) { | ||
| 57 | for (ClassEntry sourceClass : match.sourceClasses) { | ||
| 58 | m_matchesBySource.remove(sourceClass); | ||
| 59 | m_uniqueMatches.remove(sourceClass); | ||
| 60 | m_ambiguousMatchesBySource.remove(sourceClass); | ||
| 61 | m_unmatchedSourceClasses.remove(sourceClass); | ||
| 62 | } | ||
| 63 | for (ClassEntry destClass : match.destClasses) { | ||
| 64 | m_matchesByDest.remove(destClass); | ||
| 65 | m_uniqueMatches.inverse().remove(destClass); | ||
| 66 | m_ambiguousMatchesByDest.remove(destClass); | ||
| 67 | m_unmatchedDestClasses.remove(destClass); | ||
| 68 | } | ||
| 69 | m_matches.remove(match); | ||
| 70 | } | ||
| 71 | |||
| 72 | public int size() { | ||
| 73 | return m_matches.size(); | ||
| 74 | } | ||
| 75 | |||
| 76 | @Override | ||
| 77 | public Iterator<ClassMatch> iterator() { | ||
| 78 | return m_matches.iterator(); | ||
| 79 | } | ||
| 80 | |||
| 81 | private void indexMatch(ClassMatch match) { | ||
| 82 | if (!match.isMatched()) { | ||
| 83 | // unmatched | ||
| 84 | m_unmatchedSourceClasses.addAll(match.sourceClasses); | ||
| 85 | m_unmatchedDestClasses.addAll(match.destClasses); | ||
| 86 | } else { | ||
| 87 | if (match.isAmbiguous()) { | ||
| 88 | // ambiguously matched | ||
| 89 | for (ClassEntry entry : match.sourceClasses) { | ||
| 90 | m_ambiguousMatchesBySource.put(entry, match); | ||
| 91 | } | ||
| 92 | for (ClassEntry entry : match.destClasses) { | ||
| 93 | m_ambiguousMatchesByDest.put(entry, match); | ||
| 94 | } | ||
| 95 | } else { | ||
| 96 | // uniquely matched | ||
| 97 | m_uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest()); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | for (ClassEntry entry : match.sourceClasses) { | ||
| 101 | m_matchesBySource.put(entry, match); | ||
| 102 | } | ||
| 103 | for (ClassEntry entry : match.destClasses) { | ||
| 104 | m_matchesByDest.put(entry, match); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | public BiMap<ClassEntry, ClassEntry> getUniqueMatches() { | ||
| 109 | return m_uniqueMatches; | ||
| 110 | } | ||
| 111 | |||
| 112 | public Set<ClassEntry> getUnmatchedSourceClasses() { | ||
| 113 | return m_unmatchedSourceClasses; | ||
| 114 | } | ||
| 115 | |||
| 116 | public Set<ClassEntry> getUnmatchedDestClasses() { | ||
| 117 | return m_unmatchedDestClasses; | ||
| 118 | } | ||
| 119 | |||
| 120 | public Set<ClassEntry> getAmbiguouslyMatchedSourceClasses() { | ||
| 121 | return m_ambiguousMatchesBySource.keySet(); | ||
| 122 | } | ||
| 123 | |||
| 124 | public ClassMatch getMatchBySource(ClassEntry sourceClass) { | ||
| 125 | return m_matchesBySource.get(sourceClass); | ||
| 126 | } | ||
| 127 | |||
| 128 | public void removeSource(ClassEntry sourceClass) { | ||
| 129 | ClassMatch match = m_matchesBySource.get(sourceClass); | ||
| 130 | if (match != null) { | ||
| 131 | remove(match); | ||
| 132 | match.sourceClasses.remove(sourceClass); | ||
| 133 | if (!match.sourceClasses.isEmpty() || !match.destClasses.isEmpty()) { | ||
| 134 | add(match); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | public void removeDest(ClassEntry destClass) { | ||
| 140 | ClassMatch match = m_matchesByDest.get(destClass); | ||
| 141 | if (match != null) { | ||
| 142 | remove(match); | ||
| 143 | match.destClasses.remove(destClass); | ||
| 144 | if (!match.sourceClasses.isEmpty() || !match.destClasses.isEmpty()) { | ||
| 145 | add(match); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/ClassMatching.java b/src/main/java/cuchaz/enigma/convert/ClassMatching.java deleted file mode 100644 index 194b6c4..0000000 --- a/src/main/java/cuchaz/enigma/convert/ClassMatching.java +++ /dev/null | |||
| @@ -1,146 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.BiMap; | ||
| 14 | import com.google.common.collect.HashBiMap; | ||
| 15 | import com.google.common.collect.Lists; | ||
| 16 | import com.google.common.collect.Sets; | ||
| 17 | |||
| 18 | import java.util.ArrayList; | ||
| 19 | import java.util.Collection; | ||
| 20 | import java.util.List; | ||
| 21 | import java.util.Map.Entry; | ||
| 22 | import java.util.Set; | ||
| 23 | |||
| 24 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 25 | |||
| 26 | public class ClassMatching { | ||
| 27 | |||
| 28 | private ClassForest m_sourceClasses; | ||
| 29 | private ClassForest m_destClasses; | ||
| 30 | private BiMap<ClassEntry, ClassEntry> m_knownMatches; | ||
| 31 | |||
| 32 | public ClassMatching(ClassIdentifier sourceIdentifier, ClassIdentifier destIdentifier) { | ||
| 33 | m_sourceClasses = new ClassForest(sourceIdentifier); | ||
| 34 | m_destClasses = new ClassForest(destIdentifier); | ||
| 35 | m_knownMatches = HashBiMap.create(); | ||
| 36 | } | ||
| 37 | |||
| 38 | public void addKnownMatches(BiMap<ClassEntry, ClassEntry> knownMatches) { | ||
| 39 | m_knownMatches.putAll(knownMatches); | ||
| 40 | } | ||
| 41 | |||
| 42 | public void match(Iterable<ClassEntry> sourceClasses, Iterable<ClassEntry> destClasses) { | ||
| 43 | for (ClassEntry sourceClass : sourceClasses) { | ||
| 44 | if (!m_knownMatches.containsKey(sourceClass)) { | ||
| 45 | m_sourceClasses.add(sourceClass); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | for (ClassEntry destClass : destClasses) { | ||
| 49 | if (!m_knownMatches.containsValue(destClass)) { | ||
| 50 | m_destClasses.add(destClass); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | public Collection<ClassMatch> matches() { | ||
| 56 | List<ClassMatch> matches = Lists.newArrayList(); | ||
| 57 | for (Entry<ClassEntry, ClassEntry> entry : m_knownMatches.entrySet()) { | ||
| 58 | matches.add(new ClassMatch(entry.getKey(), entry.getValue())); | ||
| 59 | } | ||
| 60 | for (ClassIdentity identity : m_sourceClasses.identities()) { | ||
| 61 | matches.add(new ClassMatch(m_sourceClasses.getClasses(identity), m_destClasses.getClasses(identity))); | ||
| 62 | } | ||
| 63 | for (ClassIdentity identity : m_destClasses.identities()) { | ||
| 64 | if (!m_sourceClasses.containsIdentity(identity)) { | ||
| 65 | matches.add(new ClassMatch(new ArrayList<>(), m_destClasses.getClasses(identity))); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | return matches; | ||
| 69 | } | ||
| 70 | |||
| 71 | public Collection<ClassEntry> sourceClasses() { | ||
| 72 | Set<ClassEntry> classes = Sets.newHashSet(); | ||
| 73 | for (ClassMatch match : matches()) { | ||
| 74 | classes.addAll(match.sourceClasses); | ||
| 75 | } | ||
| 76 | return classes; | ||
| 77 | } | ||
| 78 | |||
| 79 | public Collection<ClassEntry> destClasses() { | ||
| 80 | Set<ClassEntry> classes = Sets.newHashSet(); | ||
| 81 | for (ClassMatch match : matches()) { | ||
| 82 | classes.addAll(match.destClasses); | ||
| 83 | } | ||
| 84 | return classes; | ||
| 85 | } | ||
| 86 | |||
| 87 | public BiMap<ClassEntry, ClassEntry> uniqueMatches() { | ||
| 88 | BiMap<ClassEntry, ClassEntry> uniqueMatches = HashBiMap.create(); | ||
| 89 | for (ClassMatch match : matches()) { | ||
| 90 | if (match.isMatched() && !match.isAmbiguous()) { | ||
| 91 | uniqueMatches.put(match.getUniqueSource(), match.getUniqueDest()); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | return uniqueMatches; | ||
| 95 | } | ||
| 96 | |||
| 97 | public Collection<ClassMatch> ambiguousMatches() { | ||
| 98 | List<ClassMatch> ambiguousMatches = Lists.newArrayList(); | ||
| 99 | for (ClassMatch match : matches()) { | ||
| 100 | if (match.isMatched() && match.isAmbiguous()) { | ||
| 101 | ambiguousMatches.add(match); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | return ambiguousMatches; | ||
| 105 | } | ||
| 106 | |||
| 107 | public Collection<ClassEntry> unmatchedSourceClasses() { | ||
| 108 | List<ClassEntry> classes = Lists.newArrayList(); | ||
| 109 | for (ClassMatch match : matches()) { | ||
| 110 | if (!match.isMatched() && !match.sourceClasses.isEmpty()) { | ||
| 111 | classes.addAll(match.sourceClasses); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | return classes; | ||
| 115 | } | ||
| 116 | |||
| 117 | public Collection<ClassEntry> unmatchedDestClasses() { | ||
| 118 | List<ClassEntry> classes = Lists.newArrayList(); | ||
| 119 | for (ClassMatch match : matches()) { | ||
| 120 | if (!match.isMatched() && !match.destClasses.isEmpty()) { | ||
| 121 | classes.addAll(match.destClasses); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | return classes; | ||
| 125 | } | ||
| 126 | |||
| 127 | @Override | ||
| 128 | public String toString() { | ||
| 129 | |||
| 130 | // count the ambiguous classes | ||
| 131 | int numAmbiguousSource = 0; | ||
| 132 | int numAmbiguousDest = 0; | ||
| 133 | for (ClassMatch match : ambiguousMatches()) { | ||
| 134 | numAmbiguousSource += match.sourceClasses.size(); | ||
| 135 | numAmbiguousDest += match.destClasses.size(); | ||
| 136 | } | ||
| 137 | |||
| 138 | StringBuilder buf = new StringBuilder(); | ||
| 139 | buf.append(String.format("%20s%8s%8s\n", "", "Source", "Dest")); | ||
| 140 | buf.append(String.format("%20s%8d%8d\n", "Classes", sourceClasses().size(), destClasses().size())); | ||
| 141 | buf.append(String.format("%20s%8d%8d\n", "Uniquely matched", uniqueMatches().size(), uniqueMatches().size())); | ||
| 142 | buf.append(String.format("%20s%8d%8d\n", "Ambiguously matched", numAmbiguousSource, numAmbiguousDest)); | ||
| 143 | buf.append(String.format("%20s%8d%8d\n", "Unmatched", unmatchedSourceClasses().size(), unmatchedDestClasses().size())); | ||
| 144 | return buf.toString(); | ||
| 145 | } | ||
| 146 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/ClassNamer.java b/src/main/java/cuchaz/enigma/convert/ClassNamer.java deleted file mode 100644 index e471c7d..0000000 --- a/src/main/java/cuchaz/enigma/convert/ClassNamer.java +++ /dev/null | |||
| @@ -1,56 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.BiMap; | ||
| 14 | import com.google.common.collect.Maps; | ||
| 15 | |||
| 16 | import java.util.Map; | ||
| 17 | |||
| 18 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 19 | |||
| 20 | public class ClassNamer { | ||
| 21 | |||
| 22 | public interface SidedClassNamer { | ||
| 23 | String getName(String name); | ||
| 24 | } | ||
| 25 | |||
| 26 | private Map<String, String> sourceNames; | ||
| 27 | private Map<String, String> destNames; | ||
| 28 | |||
| 29 | public ClassNamer(BiMap<ClassEntry, ClassEntry> mappings) { | ||
| 30 | // convert the identity mappings to name maps | ||
| 31 | this.sourceNames = Maps.newHashMap(); | ||
| 32 | this.destNames = Maps.newHashMap(); | ||
| 33 | int i = 0; | ||
| 34 | for (Map.Entry<ClassEntry, ClassEntry> entry : mappings.entrySet()) { | ||
| 35 | String name = String.format("M%04d", i++); | ||
| 36 | this.sourceNames.put(entry.getKey().getName(), name); | ||
| 37 | this.destNames.put(entry.getValue().getName(), name); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | public String getSourceName(String name) { | ||
| 42 | return this.sourceNames.get(name); | ||
| 43 | } | ||
| 44 | |||
| 45 | public String getDestName(String name) { | ||
| 46 | return this.destNames.get(name); | ||
| 47 | } | ||
| 48 | |||
| 49 | public SidedClassNamer getSourceNamer() { | ||
| 50 | return this::getSourceName; | ||
| 51 | } | ||
| 52 | |||
| 53 | public SidedClassNamer getDestNamer() { | ||
| 54 | return this::getDestName; | ||
| 55 | } | ||
| 56 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/MappingsConverter.java b/src/main/java/cuchaz/enigma/convert/MappingsConverter.java deleted file mode 100644 index 7739adf..0000000 --- a/src/main/java/cuchaz/enigma/convert/MappingsConverter.java +++ /dev/null | |||
| @@ -1,540 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.*; | ||
| 14 | |||
| 15 | import java.util.*; | ||
| 16 | import java.util.jar.JarFile; | ||
| 17 | |||
| 18 | import cuchaz.enigma.Constants; | ||
| 19 | import cuchaz.enigma.Deobfuscator; | ||
| 20 | import cuchaz.enigma.analysis.JarIndex; | ||
| 21 | import cuchaz.enigma.convert.ClassNamer.SidedClassNamer; | ||
| 22 | import cuchaz.enigma.mapping.*; | ||
| 23 | |||
| 24 | public class MappingsConverter { | ||
| 25 | |||
| 26 | public static ClassMatches computeClassMatches(JarFile sourceJar, JarFile destJar, Mappings mappings) { | ||
| 27 | |||
| 28 | // index jars | ||
| 29 | System.out.println("Indexing source jar..."); | ||
| 30 | JarIndex sourceIndex = new JarIndex(); | ||
| 31 | sourceIndex.indexJar(sourceJar, false); | ||
| 32 | System.out.println("Indexing dest jar..."); | ||
| 33 | JarIndex destIndex = new JarIndex(); | ||
| 34 | destIndex.indexJar(destJar, false); | ||
| 35 | |||
| 36 | // compute the matching | ||
| 37 | ClassMatching matching = computeMatching(sourceJar, sourceIndex, destJar, destIndex, null); | ||
| 38 | return new ClassMatches(matching.matches()); | ||
| 39 | } | ||
| 40 | |||
| 41 | public static ClassMatching computeMatching(JarFile sourceJar, JarIndex sourceIndex, JarFile destJar, JarIndex destIndex, BiMap<ClassEntry, ClassEntry> knownMatches) { | ||
| 42 | |||
| 43 | System.out.println("Iteratively matching classes"); | ||
| 44 | |||
| 45 | ClassMatching lastMatching = null; | ||
| 46 | int round = 0; | ||
| 47 | SidedClassNamer sourceNamer = null; | ||
| 48 | SidedClassNamer destNamer = null; | ||
| 49 | for (boolean useReferences : Arrays.asList(false, true)) { | ||
| 50 | |||
| 51 | int numUniqueMatchesLastTime = 0; | ||
| 52 | if (lastMatching != null) { | ||
| 53 | numUniqueMatchesLastTime = lastMatching.uniqueMatches().size(); | ||
| 54 | } | ||
| 55 | |||
| 56 | while (true) { | ||
| 57 | |||
| 58 | System.out.println("Round " + (++round) + "..."); | ||
| 59 | |||
| 60 | // init the matching with identity settings | ||
| 61 | ClassMatching matching = new ClassMatching( | ||
| 62 | new ClassIdentifier(sourceJar, sourceIndex, sourceNamer, useReferences), | ||
| 63 | new ClassIdentifier(destJar, destIndex, destNamer, useReferences) | ||
| 64 | ); | ||
| 65 | |||
| 66 | if (knownMatches != null) { | ||
| 67 | matching.addKnownMatches(knownMatches); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (lastMatching == null) { | ||
| 71 | // search all classes | ||
| 72 | matching.match(sourceIndex.getObfClassEntries(), destIndex.getObfClassEntries()); | ||
| 73 | } else { | ||
| 74 | // we already know about these matches from last time | ||
| 75 | matching.addKnownMatches(lastMatching.uniqueMatches()); | ||
| 76 | |||
| 77 | // search unmatched and ambiguously-matched classes | ||
| 78 | matching.match(lastMatching.unmatchedSourceClasses(), lastMatching.unmatchedDestClasses()); | ||
| 79 | for (ClassMatch match : lastMatching.ambiguousMatches()) { | ||
| 80 | matching.match(match.sourceClasses, match.destClasses); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | System.out.println(matching); | ||
| 84 | BiMap<ClassEntry, ClassEntry> uniqueMatches = matching.uniqueMatches(); | ||
| 85 | |||
| 86 | // did we match anything new this time? | ||
| 87 | if (uniqueMatches.size() > numUniqueMatchesLastTime) { | ||
| 88 | numUniqueMatchesLastTime = uniqueMatches.size(); | ||
| 89 | lastMatching = matching; | ||
| 90 | } else { | ||
| 91 | break; | ||
| 92 | } | ||
| 93 | |||
| 94 | // update the namers | ||
| 95 | ClassNamer namer = new ClassNamer(uniqueMatches); | ||
| 96 | sourceNamer = namer.getSourceNamer(); | ||
| 97 | destNamer = namer.getDestNamer(); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return lastMatching; | ||
| 102 | } | ||
| 103 | |||
| 104 | public static Mappings newMappings(ClassMatches matches, Mappings oldMappings, Deobfuscator sourceDeobfuscator, Deobfuscator destDeobfuscator) { | ||
| 105 | |||
| 106 | // sort the unique matches by size of inner class chain | ||
| 107 | Multimap<Integer, java.util.Map.Entry<ClassEntry, ClassEntry>> matchesByDestChainSize = HashMultimap.create(); | ||
| 108 | for (java.util.Map.Entry<ClassEntry, ClassEntry> match : matches.getUniqueMatches().entrySet()) { | ||
| 109 | int chainSize = destDeobfuscator.getJarIndex().getObfClassChain(match.getValue()).size(); | ||
| 110 | matchesByDestChainSize.put(chainSize, match); | ||
| 111 | } | ||
| 112 | |||
| 113 | // build the mappings (in order of small-to-large inner chains) | ||
| 114 | Mappings newMappings = new Mappings(); | ||
| 115 | List<Integer> chainSizes = Lists.newArrayList(matchesByDestChainSize.keySet()); | ||
| 116 | Collections.sort(chainSizes); | ||
| 117 | for (int chainSize : chainSizes) { | ||
| 118 | for (java.util.Map.Entry<ClassEntry, ClassEntry> match : matchesByDestChainSize.get(chainSize)) { | ||
| 119 | |||
| 120 | // get class info | ||
| 121 | ClassEntry obfSourceClassEntry = match.getKey(); | ||
| 122 | ClassEntry obfDestClassEntry = match.getValue(); | ||
| 123 | List<ClassEntry> destClassChain = destDeobfuscator.getJarIndex().getObfClassChain(obfDestClassEntry); | ||
| 124 | |||
| 125 | ClassMapping sourceMapping = sourceDeobfuscator.getMappings().getClassByObf(obfSourceClassEntry); | ||
| 126 | if (sourceMapping == null) { | ||
| 127 | // if this class was never deobfuscated, don't try to match it | ||
| 128 | continue; | ||
| 129 | } | ||
| 130 | |||
| 131 | // find out where to make the dest class mapping | ||
| 132 | if (destClassChain.size() == 1) { | ||
| 133 | // not an inner class, add directly to mappings | ||
| 134 | newMappings.addClassMapping(migrateClassMapping(obfDestClassEntry, sourceMapping, matches, false)); | ||
| 135 | } else { | ||
| 136 | // inner class, find the outer class mapping | ||
| 137 | ClassMapping destMapping = null; | ||
| 138 | for (int i = 0; i < destClassChain.size() - 1; i++) { | ||
| 139 | ClassEntry destChainClassEntry = destClassChain.get(i); | ||
| 140 | if (destMapping == null) { | ||
| 141 | destMapping = newMappings.getClassByObf(destChainClassEntry); | ||
| 142 | if (destMapping == null) { | ||
| 143 | destMapping = new ClassMapping(destChainClassEntry.getName()); | ||
| 144 | newMappings.addClassMapping(destMapping); | ||
| 145 | } | ||
| 146 | } else { | ||
| 147 | destMapping = destMapping.getInnerClassByObfSimple(destChainClassEntry.getInnermostClassName()); | ||
| 148 | if (destMapping == null) { | ||
| 149 | destMapping = new ClassMapping(destChainClassEntry.getName()); | ||
| 150 | destMapping.addInnerClassMapping(destMapping); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | if (destMapping != null) { | ||
| 155 | destMapping.addInnerClassMapping(migrateClassMapping(obfDestClassEntry, sourceMapping, matches, true)); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | return newMappings; | ||
| 161 | } | ||
| 162 | |||
| 163 | private static ClassMapping migrateClassMapping(ClassEntry newObfClass, ClassMapping oldClassMapping, final ClassMatches matches, boolean useSimpleName) { | ||
| 164 | |||
| 165 | ClassNameReplacer replacer = className -> { | ||
| 166 | ClassEntry newClassEntry = matches.getUniqueMatches().get(new ClassEntry(className)); | ||
| 167 | if (newClassEntry != null) { | ||
| 168 | return newClassEntry.getName(); | ||
| 169 | } | ||
| 170 | return null; | ||
| 171 | }; | ||
| 172 | |||
| 173 | ClassMapping newClassMapping; | ||
| 174 | String deobfName = oldClassMapping.getDeobfName(); | ||
| 175 | if (deobfName != null) { | ||
| 176 | if (useSimpleName) { | ||
| 177 | deobfName = new ClassEntry(deobfName).getSimpleName(); | ||
| 178 | } | ||
| 179 | newClassMapping = new ClassMapping(newObfClass.getName(), deobfName); | ||
| 180 | } else { | ||
| 181 | newClassMapping = new ClassMapping(newObfClass.getName()); | ||
| 182 | } | ||
| 183 | |||
| 184 | // migrate fields | ||
| 185 | for (FieldMapping oldFieldMapping : oldClassMapping.fields()) { | ||
| 186 | if (canMigrate(oldFieldMapping.getObfType(), matches)) { | ||
| 187 | newClassMapping.addFieldMapping(new FieldMapping(oldFieldMapping, replacer)); | ||
| 188 | } else { | ||
| 189 | System.out.println(String.format("Can't map field, dropping: %s.%s %s", | ||
| 190 | oldClassMapping.getDeobfName(), | ||
| 191 | oldFieldMapping.getDeobfName(), | ||
| 192 | oldFieldMapping.getObfType() | ||
| 193 | )); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | // migrate methods | ||
| 198 | for (MethodMapping oldMethodMapping : oldClassMapping.methods()) { | ||
| 199 | if (canMigrate(oldMethodMapping.getObfSignature(), matches)) { | ||
| 200 | newClassMapping.addMethodMapping(new MethodMapping(oldMethodMapping, replacer)); | ||
| 201 | } else { | ||
| 202 | System.out.println(String.format("Can't map method, dropping: %s.%s %s", | ||
| 203 | oldClassMapping.getDeobfName(), | ||
| 204 | oldMethodMapping.getDeobfName(), | ||
| 205 | oldMethodMapping.getObfSignature() | ||
| 206 | )); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | return newClassMapping; | ||
| 211 | } | ||
| 212 | |||
| 213 | private static boolean canMigrate(Signature oldObfSignature, ClassMatches classMatches) { | ||
| 214 | for (Type oldObfType : oldObfSignature.types()) { | ||
| 215 | if (!canMigrate(oldObfType, classMatches)) { | ||
| 216 | return false; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | return true; | ||
| 220 | } | ||
| 221 | |||
| 222 | private static boolean canMigrate(Type oldObfType, ClassMatches classMatches) { | ||
| 223 | |||
| 224 | // non classes can be migrated | ||
| 225 | if (!oldObfType.hasClass()) { | ||
| 226 | return true; | ||
| 227 | } | ||
| 228 | |||
| 229 | // non obfuscated classes can be migrated | ||
| 230 | ClassEntry classEntry = oldObfType.getClassEntry(); | ||
| 231 | if (!classEntry.getPackageName().equals(Constants.NONE_PACKAGE)) { | ||
| 232 | return true; | ||
| 233 | } | ||
| 234 | |||
| 235 | // obfuscated classes with mappings can be migrated | ||
| 236 | return classMatches.getUniqueMatches().containsKey(classEntry); | ||
| 237 | } | ||
| 238 | |||
| 239 | public interface Doer<T extends Entry> { | ||
| 240 | Collection<T> getDroppedEntries(MappingsChecker checker); | ||
| 241 | |||
| 242 | Collection<T> getObfEntries(JarIndex jarIndex); | ||
| 243 | |||
| 244 | Collection<? extends MemberMapping<T>> getMappings(ClassMapping destClassMapping); | ||
| 245 | |||
| 246 | Set<T> filterEntries(Collection<T> obfEntries, T obfSourceEntry, ClassMatches classMatches); | ||
| 247 | |||
| 248 | void setUpdateObfMember(ClassMapping classMapping, MemberMapping<T> memberMapping, T newEntry); | ||
| 249 | |||
| 250 | boolean hasObfMember(ClassMapping classMapping, T obfEntry); | ||
| 251 | |||
| 252 | void removeMemberByObf(ClassMapping classMapping, T obfEntry); | ||
| 253 | } | ||
| 254 | |||
| 255 | public static Doer<FieldEntry> getFieldDoer() { | ||
| 256 | return new Doer<FieldEntry>() { | ||
| 257 | |||
| 258 | @Override | ||
| 259 | public Collection<FieldEntry> getDroppedEntries(MappingsChecker checker) { | ||
| 260 | return checker.getDroppedFieldMappings().keySet(); | ||
| 261 | } | ||
| 262 | |||
| 263 | @Override | ||
| 264 | public Collection<FieldEntry> getObfEntries(JarIndex jarIndex) { | ||
| 265 | return jarIndex.getObfFieldEntries(); | ||
| 266 | } | ||
| 267 | |||
| 268 | @Override | ||
| 269 | public Collection<? extends MemberMapping<FieldEntry>> getMappings(ClassMapping destClassMapping) { | ||
| 270 | return (Collection<? extends MemberMapping<FieldEntry>>) destClassMapping.fields(); | ||
| 271 | } | ||
| 272 | |||
| 273 | @Override | ||
| 274 | public Set<FieldEntry> filterEntries(Collection<FieldEntry> obfDestFields, FieldEntry obfSourceField, ClassMatches classMatches) { | ||
| 275 | Set<FieldEntry> out = Sets.newHashSet(); | ||
| 276 | for (FieldEntry obfDestField : obfDestFields) { | ||
| 277 | Type translatedDestType = translate(obfDestField.getType(), classMatches.getUniqueMatches().inverse()); | ||
| 278 | if (translatedDestType.equals(obfSourceField.getType())) { | ||
| 279 | out.add(obfDestField); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | return out; | ||
| 283 | } | ||
| 284 | |||
| 285 | @Override | ||
| 286 | public void setUpdateObfMember(ClassMapping classMapping, MemberMapping<FieldEntry> memberMapping, FieldEntry newField) { | ||
| 287 | FieldMapping fieldMapping = (FieldMapping) memberMapping; | ||
| 288 | classMapping.setFieldObfNameAndType(fieldMapping.getObfName(), fieldMapping.getObfType(), newField.getName(), newField.getType()); | ||
| 289 | } | ||
| 290 | |||
| 291 | @Override | ||
| 292 | public boolean hasObfMember(ClassMapping classMapping, FieldEntry obfField) { | ||
| 293 | return classMapping.getFieldByObf(obfField.getName(), obfField.getType()) != null; | ||
| 294 | } | ||
| 295 | |||
| 296 | @Override | ||
| 297 | public void removeMemberByObf(ClassMapping classMapping, FieldEntry obfField) { | ||
| 298 | classMapping.removeFieldMapping(classMapping.getFieldByObf(obfField.getName(), obfField.getType())); | ||
| 299 | } | ||
| 300 | }; | ||
| 301 | } | ||
| 302 | |||
| 303 | public static Doer<BehaviorEntry> getMethodDoer() { | ||
| 304 | return new Doer<BehaviorEntry>() { | ||
| 305 | |||
| 306 | @Override | ||
| 307 | public Collection<BehaviorEntry> getDroppedEntries(MappingsChecker checker) { | ||
| 308 | return checker.getDroppedMethodMappings().keySet(); | ||
| 309 | } | ||
| 310 | |||
| 311 | @Override | ||
| 312 | public Collection<BehaviorEntry> getObfEntries(JarIndex jarIndex) { | ||
| 313 | return jarIndex.getObfBehaviorEntries(); | ||
| 314 | } | ||
| 315 | |||
| 316 | @Override | ||
| 317 | public Collection<? extends MemberMapping<BehaviorEntry>> getMappings(ClassMapping destClassMapping) { | ||
| 318 | return (Collection<? extends MemberMapping<BehaviorEntry>>) destClassMapping.methods(); | ||
| 319 | } | ||
| 320 | |||
| 321 | @Override | ||
| 322 | public Set<BehaviorEntry> filterEntries(Collection<BehaviorEntry> obfDestFields, BehaviorEntry obfSourceField, ClassMatches classMatches) { | ||
| 323 | Set<BehaviorEntry> out = Sets.newHashSet(); | ||
| 324 | for (BehaviorEntry obfDestField : obfDestFields) { | ||
| 325 | Signature translatedDestSignature = translate(obfDestField.getSignature(), classMatches.getUniqueMatches().inverse()); | ||
| 326 | if (translatedDestSignature == null && obfSourceField.getSignature() == null) { | ||
| 327 | out.add(obfDestField); | ||
| 328 | } else if (translatedDestSignature == null || obfSourceField.getSignature() == null) { | ||
| 329 | // skip it | ||
| 330 | } else if (translatedDestSignature.equals(obfSourceField.getSignature())) { | ||
| 331 | out.add(obfDestField); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | return out; | ||
| 335 | } | ||
| 336 | |||
| 337 | @Override | ||
| 338 | public void setUpdateObfMember(ClassMapping classMapping, MemberMapping<BehaviorEntry> memberMapping, BehaviorEntry newBehavior) { | ||
| 339 | MethodMapping methodMapping = (MethodMapping) memberMapping; | ||
| 340 | classMapping.setMethodObfNameAndSignature(methodMapping.getObfName(), methodMapping.getObfSignature(), newBehavior.getName(), newBehavior.getSignature()); | ||
| 341 | } | ||
| 342 | |||
| 343 | @Override | ||
| 344 | public boolean hasObfMember(ClassMapping classMapping, BehaviorEntry obfBehavior) { | ||
| 345 | return classMapping.getMethodByObf(obfBehavior.getName(), obfBehavior.getSignature()) != null; | ||
| 346 | } | ||
| 347 | |||
| 348 | @Override | ||
| 349 | public void removeMemberByObf(ClassMapping classMapping, BehaviorEntry obfBehavior) { | ||
| 350 | classMapping.removeMethodMapping(classMapping.getMethodByObf(obfBehavior.getName(), obfBehavior.getSignature())); | ||
| 351 | } | ||
| 352 | }; | ||
| 353 | } | ||
| 354 | |||
| 355 | public static <T extends Entry> MemberMatches<T> computeMemberMatches(Deobfuscator destDeobfuscator, Mappings destMappings, ClassMatches classMatches, Doer<T> doer) { | ||
| 356 | |||
| 357 | MemberMatches<T> memberMatches = new MemberMatches<>(); | ||
| 358 | |||
| 359 | // unmatched source fields are easy | ||
| 360 | MappingsChecker checker = new MappingsChecker(destDeobfuscator.getJarIndex()); | ||
| 361 | checker.dropBrokenMappings(destMappings); | ||
| 362 | for (T destObfEntry : doer.getDroppedEntries(checker)) { | ||
| 363 | T srcObfEntry = translate(destObfEntry, classMatches.getUniqueMatches().inverse()); | ||
| 364 | memberMatches.addUnmatchedSourceEntry(srcObfEntry); | ||
| 365 | } | ||
| 366 | |||
| 367 | // get matched fields (anything that's left after the checks/drops is matched( | ||
| 368 | for (ClassMapping classMapping : destMappings.classes()) { | ||
| 369 | collectMatchedFields(memberMatches, classMapping, classMatches, doer); | ||
| 370 | } | ||
| 371 | |||
| 372 | // get unmatched dest fields | ||
| 373 | for (T destEntry : doer.getObfEntries(destDeobfuscator.getJarIndex())) { | ||
| 374 | if (!memberMatches.isMatchedDestEntry(destEntry)) { | ||
| 375 | memberMatches.addUnmatchedDestEntry(destEntry); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | System.out.println("Automatching " + memberMatches.getUnmatchedSourceEntries().size() + " unmatched source entries..."); | ||
| 380 | |||
| 381 | // go through the unmatched source fields and try to pick out the easy matches | ||
| 382 | for (ClassEntry obfSourceClass : Lists.newArrayList(memberMatches.getSourceClassesWithUnmatchedEntries())) { | ||
| 383 | for (T obfSourceEntry : Lists.newArrayList(memberMatches.getUnmatchedSourceEntries(obfSourceClass))) { | ||
| 384 | |||
| 385 | // get the possible dest matches | ||
| 386 | ClassEntry obfDestClass = classMatches.getUniqueMatches().get(obfSourceClass); | ||
| 387 | |||
| 388 | // filter by type/signature | ||
| 389 | Set<T> obfDestEntries = doer.filterEntries(memberMatches.getUnmatchedDestEntries(obfDestClass), obfSourceEntry, classMatches); | ||
| 390 | |||
| 391 | if (obfDestEntries.size() == 1) { | ||
| 392 | // make the easy match | ||
| 393 | memberMatches.makeMatch(obfSourceEntry, obfDestEntries.iterator().next()); | ||
| 394 | } else if (obfDestEntries.isEmpty()) { | ||
| 395 | // no match is possible =( | ||
| 396 | memberMatches.makeSourceUnmatchable(obfSourceEntry); | ||
| 397 | } | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | System.out.println(String.format("Ended up with %d ambiguous and %d unmatchable source entries", | ||
| 402 | memberMatches.getUnmatchedSourceEntries().size(), | ||
| 403 | memberMatches.getUnmatchableSourceEntries().size() | ||
| 404 | )); | ||
| 405 | |||
| 406 | return memberMatches; | ||
| 407 | } | ||
| 408 | |||
| 409 | private static <T extends Entry> void collectMatchedFields(MemberMatches<T> memberMatches, ClassMapping destClassMapping, ClassMatches classMatches, Doer<T> doer) { | ||
| 410 | |||
| 411 | // get the fields for this class | ||
| 412 | for (MemberMapping<T> destEntryMapping : doer.getMappings(destClassMapping)) { | ||
| 413 | T destObfField = destEntryMapping.getObfEntry(destClassMapping.getObfEntry()); | ||
| 414 | T srcObfField = translate(destObfField, classMatches.getUniqueMatches().inverse()); | ||
| 415 | memberMatches.addMatch(srcObfField, destObfField); | ||
| 416 | } | ||
| 417 | |||
| 418 | // recurse | ||
| 419 | for (ClassMapping destInnerClassMapping : destClassMapping.innerClasses()) { | ||
| 420 | collectMatchedFields(memberMatches, destInnerClassMapping, classMatches, doer); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | @SuppressWarnings("unchecked") | ||
| 425 | private static <T extends Entry> T translate(T in, BiMap<ClassEntry, ClassEntry> map) { | ||
| 426 | if (in instanceof FieldEntry) { | ||
| 427 | return (T) new FieldEntry( | ||
| 428 | map.get(in.getClassEntry()), | ||
| 429 | in.getName(), | ||
| 430 | translate(((FieldEntry) in).getType(), map) | ||
| 431 | ); | ||
| 432 | } else if (in instanceof MethodEntry) { | ||
| 433 | return (T) new MethodEntry( | ||
| 434 | map.get(in.getClassEntry()), | ||
| 435 | in.getName(), | ||
| 436 | translate(((MethodEntry) in).getSignature(), map) | ||
| 437 | ); | ||
| 438 | } else if (in instanceof ConstructorEntry) { | ||
| 439 | return (T) new ConstructorEntry( | ||
| 440 | map.get(in.getClassEntry()), | ||
| 441 | translate(((ConstructorEntry) in).getSignature(), map) | ||
| 442 | ); | ||
| 443 | } | ||
| 444 | throw new Error("Unhandled entry type: " + in.getClass()); | ||
| 445 | } | ||
| 446 | |||
| 447 | private static Type translate(Type type, final BiMap<ClassEntry, ClassEntry> map) { | ||
| 448 | return new Type(type, inClassName -> { | ||
| 449 | ClassEntry outClassEntry = map.get(new ClassEntry(inClassName)); | ||
| 450 | if (outClassEntry == null) { | ||
| 451 | return null; | ||
| 452 | } | ||
| 453 | return outClassEntry.getName(); | ||
| 454 | }); | ||
| 455 | } | ||
| 456 | |||
| 457 | private static Signature translate(Signature signature, final BiMap<ClassEntry, ClassEntry> map) { | ||
| 458 | if (signature == null) { | ||
| 459 | return null; | ||
| 460 | } | ||
| 461 | return new Signature(signature, inClassName -> { | ||
| 462 | ClassEntry outClassEntry = map.get(new ClassEntry(inClassName)); | ||
| 463 | if (outClassEntry == null) { | ||
| 464 | return null; | ||
| 465 | } | ||
| 466 | return outClassEntry.getName(); | ||
| 467 | }); | ||
| 468 | } | ||
| 469 | |||
| 470 | public static <T extends Entry> void applyMemberMatches(Mappings mappings, ClassMatches classMatches, MemberMatches<T> memberMatches, Doer<T> doer) { | ||
| 471 | for (ClassMapping classMapping : mappings.classes()) { | ||
| 472 | applyMemberMatches(classMapping, classMatches, memberMatches, doer); | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | private static <T extends Entry> void applyMemberMatches(ClassMapping classMapping, ClassMatches classMatches, MemberMatches<T> memberMatches, Doer<T> doer) { | ||
| 477 | |||
| 478 | // get the classes | ||
| 479 | ClassEntry obfDestClass = classMapping.getObfEntry(); | ||
| 480 | |||
| 481 | // make a map of all the renames we need to make | ||
| 482 | Map<T, T> renames = Maps.newHashMap(); | ||
| 483 | for (MemberMapping<T> memberMapping : Lists.newArrayList(doer.getMappings(classMapping))) { | ||
| 484 | T obfOldDestEntry = memberMapping.getObfEntry(obfDestClass); | ||
| 485 | T obfSourceEntry = getSourceEntryFromDestMapping(memberMapping, obfDestClass, classMatches); | ||
| 486 | |||
| 487 | // but drop the unmatchable things | ||
| 488 | if (memberMatches.isUnmatchableSourceEntry(obfSourceEntry)) { | ||
| 489 | doer.removeMemberByObf(classMapping, obfOldDestEntry); | ||
| 490 | continue; | ||
| 491 | } | ||
| 492 | |||
| 493 | T obfNewDestEntry = memberMatches.matches().get(obfSourceEntry); | ||
| 494 | if (obfNewDestEntry != null && !obfOldDestEntry.getName().equals(obfNewDestEntry.getName())) { | ||
| 495 | renames.put(obfOldDestEntry, obfNewDestEntry); | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | if (!renames.isEmpty()) { | ||
| 500 | |||
| 501 | // apply to this class (should never need more than n passes) | ||
| 502 | int numRenamesAppliedThisRound; | ||
| 503 | do { | ||
| 504 | numRenamesAppliedThisRound = 0; | ||
| 505 | |||
| 506 | for (MemberMapping<T> memberMapping : Lists.newArrayList(doer.getMappings(classMapping))) { | ||
| 507 | T obfOldDestEntry = memberMapping.getObfEntry(obfDestClass); | ||
| 508 | T obfNewDestEntry = renames.get(obfOldDestEntry); | ||
| 509 | if (obfNewDestEntry != null) { | ||
| 510 | // make sure this rename won't cause a collision | ||
| 511 | // otherwise, save it for the next round and try again next time | ||
| 512 | if (!doer.hasObfMember(classMapping, obfNewDestEntry)) { | ||
| 513 | doer.setUpdateObfMember(classMapping, memberMapping, obfNewDestEntry); | ||
| 514 | renames.remove(obfOldDestEntry); | ||
| 515 | numRenamesAppliedThisRound++; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | } | ||
| 519 | } while (numRenamesAppliedThisRound > 0); | ||
| 520 | |||
| 521 | if (!renames.isEmpty()) { | ||
| 522 | System.err.println(String.format("WARNING: Couldn't apply all the renames for class %s. %d renames left.", | ||
| 523 | classMapping.getObfFullName(), renames.size() | ||
| 524 | )); | ||
| 525 | for (Map.Entry<T, T> entry : renames.entrySet()) { | ||
| 526 | System.err.println(String.format("\t%s -> %s", entry.getKey().getName(), entry.getValue().getName())); | ||
| 527 | } | ||
| 528 | } | ||
| 529 | } | ||
| 530 | |||
| 531 | // recurse | ||
| 532 | for (ClassMapping innerClassMapping : classMapping.innerClasses()) { | ||
| 533 | applyMemberMatches(innerClassMapping, classMatches, memberMatches, doer); | ||
| 534 | } | ||
| 535 | } | ||
| 536 | |||
| 537 | private static <T extends Entry> T getSourceEntryFromDestMapping(MemberMapping<T> destMemberMapping, ClassEntry obfDestClass, ClassMatches classMatches) { | ||
| 538 | return translate(destMemberMapping.getObfEntry(obfDestClass), classMatches.getUniqueMatches().inverse()); | ||
| 539 | } | ||
| 540 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/MatchesReader.java b/src/main/java/cuchaz/enigma/convert/MatchesReader.java deleted file mode 100644 index f7853ac..0000000 --- a/src/main/java/cuchaz/enigma/convert/MatchesReader.java +++ /dev/null | |||
| @@ -1,104 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.Lists; | ||
| 14 | |||
| 15 | import java.io.BufferedReader; | ||
| 16 | import java.io.File; | ||
| 17 | import java.io.FileReader; | ||
| 18 | import java.io.IOException; | ||
| 19 | import java.util.Collection; | ||
| 20 | import java.util.List; | ||
| 21 | |||
| 22 | import cuchaz.enigma.mapping.*; | ||
| 23 | |||
| 24 | |||
| 25 | public class MatchesReader { | ||
| 26 | |||
| 27 | public static ClassMatches readClasses(File file) | ||
| 28 | throws IOException { | ||
| 29 | try (BufferedReader in = new BufferedReader(new FileReader(file))) { | ||
| 30 | ClassMatches matches = new ClassMatches(); | ||
| 31 | String line; | ||
| 32 | while ((line = in.readLine()) != null) { | ||
| 33 | matches.add(readClassMatch(line)); | ||
| 34 | } | ||
| 35 | return matches; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | private static ClassMatch readClassMatch(String line) | ||
| 40 | throws IOException { | ||
| 41 | String[] sides = line.split(":", 2); | ||
| 42 | return new ClassMatch(readClasses(sides[0]), readClasses(sides[1])); | ||
| 43 | } | ||
| 44 | |||
| 45 | private static Collection<ClassEntry> readClasses(String in) { | ||
| 46 | List<ClassEntry> entries = Lists.newArrayList(); | ||
| 47 | for (String className : in.split(",")) { | ||
| 48 | className = className.trim(); | ||
| 49 | if (className.length() > 0) { | ||
| 50 | entries.add(new ClassEntry(className)); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | return entries; | ||
| 54 | } | ||
| 55 | |||
| 56 | public static <T extends Entry> MemberMatches<T> readMembers(File file) | ||
| 57 | throws IOException { | ||
| 58 | try (BufferedReader in = new BufferedReader(new FileReader(file))) { | ||
| 59 | MemberMatches<T> matches = new MemberMatches<>(); | ||
| 60 | String line; | ||
| 61 | while ((line = in.readLine()) != null) { | ||
| 62 | readMemberMatch(matches, line); | ||
| 63 | } | ||
| 64 | return matches; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | private static <T extends Entry> void readMemberMatch(MemberMatches<T> matches, String line) { | ||
| 69 | if (line.startsWith("!")) { | ||
| 70 | T source = readEntry(line.substring(1)); | ||
| 71 | matches.addUnmatchableSourceEntry(source); | ||
| 72 | } else { | ||
| 73 | String[] parts = line.split(":", 2); | ||
| 74 | T source = readEntry(parts[0]); | ||
| 75 | T dest = readEntry(parts[1]); | ||
| 76 | if (source != null && dest != null) { | ||
| 77 | matches.addMatch(source, dest); | ||
| 78 | } else if (source != null) { | ||
| 79 | matches.addUnmatchedSourceEntry(source); | ||
| 80 | } else if (dest != null) { | ||
| 81 | matches.addUnmatchedDestEntry(dest); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | @SuppressWarnings("unchecked") | ||
| 87 | private static <T extends Entry> T readEntry(String in) { | ||
| 88 | if (in.length() <= 0) { | ||
| 89 | return null; | ||
| 90 | } | ||
| 91 | String[] parts = in.split(" "); | ||
| 92 | if (parts.length == 3 && parts[2].indexOf('(') < 0) { | ||
| 93 | return (T) new FieldEntry(new ClassEntry(parts[0]), parts[1], new Type(parts[2])); | ||
| 94 | } else { | ||
| 95 | if (parts.length == 2) { | ||
| 96 | return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1]); | ||
| 97 | } else if (parts.length == 3) { | ||
| 98 | return (T) EntryFactory.getBehaviorEntry(parts[0], parts[1], parts[2]); | ||
| 99 | } else { | ||
| 100 | throw new Error("Malformed behavior entry: " + in); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/MatchesWriter.java b/src/main/java/cuchaz/enigma/convert/MatchesWriter.java deleted file mode 100644 index baf7929..0000000 --- a/src/main/java/cuchaz/enigma/convert/MatchesWriter.java +++ /dev/null | |||
| @@ -1,121 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import java.io.File; | ||
| 14 | import java.io.FileWriter; | ||
| 15 | import java.io.IOException; | ||
| 16 | import java.util.Map; | ||
| 17 | |||
| 18 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 19 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 20 | import cuchaz.enigma.mapping.Entry; | ||
| 21 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 22 | |||
| 23 | |||
| 24 | public class MatchesWriter { | ||
| 25 | |||
| 26 | public static void writeClasses(ClassMatches matches, File file) | ||
| 27 | throws IOException { | ||
| 28 | try (FileWriter out = new FileWriter(file)) { | ||
| 29 | for (ClassMatch match : matches) { | ||
| 30 | writeClassMatch(out, match); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | private static void writeClassMatch(FileWriter out, ClassMatch match) | ||
| 36 | throws IOException { | ||
| 37 | writeClasses(out, match.sourceClasses); | ||
| 38 | out.write(":"); | ||
| 39 | writeClasses(out, match.destClasses); | ||
| 40 | out.write("\n"); | ||
| 41 | } | ||
| 42 | |||
| 43 | private static void writeClasses(FileWriter out, Iterable<ClassEntry> classes) | ||
| 44 | throws IOException { | ||
| 45 | boolean isFirst = true; | ||
| 46 | for (ClassEntry entry : classes) { | ||
| 47 | if (isFirst) { | ||
| 48 | isFirst = false; | ||
| 49 | } else { | ||
| 50 | out.write(","); | ||
| 51 | } | ||
| 52 | out.write(entry.toString()); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | public static <T extends Entry> void writeMembers(MemberMatches<T> matches, File file) | ||
| 57 | throws IOException { | ||
| 58 | try (FileWriter out = new FileWriter(file)) { | ||
| 59 | for (Map.Entry<T, T> match : matches.matches().entrySet()) { | ||
| 60 | writeMemberMatch(out, match.getKey(), match.getValue()); | ||
| 61 | } | ||
| 62 | for (T entry : matches.getUnmatchedSourceEntries()) { | ||
| 63 | writeMemberMatch(out, entry, null); | ||
| 64 | } | ||
| 65 | for (T entry : matches.getUnmatchedDestEntries()) { | ||
| 66 | writeMemberMatch(out, null, entry); | ||
| 67 | } | ||
| 68 | for (T entry : matches.getUnmatchableSourceEntries()) { | ||
| 69 | writeUnmatchableEntry(out, entry); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | private static <T extends Entry> void writeMemberMatch(FileWriter out, T source, T dest) | ||
| 75 | throws IOException { | ||
| 76 | if (source != null) { | ||
| 77 | writeEntry(out, source); | ||
| 78 | } | ||
| 79 | out.write(":"); | ||
| 80 | if (dest != null) { | ||
| 81 | writeEntry(out, dest); | ||
| 82 | } | ||
| 83 | out.write("\n"); | ||
| 84 | } | ||
| 85 | |||
| 86 | private static <T extends Entry> void writeUnmatchableEntry(FileWriter out, T entry) | ||
| 87 | throws IOException { | ||
| 88 | out.write("!"); | ||
| 89 | writeEntry(out, entry); | ||
| 90 | out.write("\n"); | ||
| 91 | } | ||
| 92 | |||
| 93 | private static <T extends Entry> void writeEntry(FileWriter out, T entry) | ||
| 94 | throws IOException { | ||
| 95 | if (entry instanceof FieldEntry) { | ||
| 96 | writeField(out, (FieldEntry) entry); | ||
| 97 | } else if (entry instanceof BehaviorEntry) { | ||
| 98 | writeBehavior(out, (BehaviorEntry) entry); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | private static void writeField(FileWriter out, FieldEntry fieldEntry) | ||
| 103 | throws IOException { | ||
| 104 | out.write(fieldEntry.getClassName()); | ||
| 105 | out.write(" "); | ||
| 106 | out.write(fieldEntry.getName()); | ||
| 107 | out.write(" "); | ||
| 108 | out.write(fieldEntry.getType().toString()); | ||
| 109 | } | ||
| 110 | |||
| 111 | private static void writeBehavior(FileWriter out, BehaviorEntry behaviorEntry) | ||
| 112 | throws IOException { | ||
| 113 | out.write(behaviorEntry.getClassName()); | ||
| 114 | out.write(" "); | ||
| 115 | out.write(behaviorEntry.getName()); | ||
| 116 | out.write(" "); | ||
| 117 | if (behaviorEntry.getSignature() != null) { | ||
| 118 | out.write(behaviorEntry.getSignature().toString()); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
diff --git a/src/main/java/cuchaz/enigma/convert/MemberMatches.java b/src/main/java/cuchaz/enigma/convert/MemberMatches.java deleted file mode 100644 index 662c1db..0000000 --- a/src/main/java/cuchaz/enigma/convert/MemberMatches.java +++ /dev/null | |||
| @@ -1,143 +0,0 @@ | |||
| 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.convert; | ||
| 12 | |||
| 13 | import com.google.common.collect.*; | ||
| 14 | |||
| 15 | import java.util.Collection; | ||
| 16 | import java.util.Set; | ||
| 17 | |||
| 18 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 19 | import cuchaz.enigma.mapping.Entry; | ||
| 20 | |||
| 21 | |||
| 22 | public class MemberMatches<T extends Entry> { | ||
| 23 | |||
| 24 | private BiMap<T, T> m_matches; | ||
| 25 | private Multimap<ClassEntry, T> m_matchedSourceEntries; | ||
| 26 | private Multimap<ClassEntry, T> m_unmatchedSourceEntries; | ||
| 27 | private Multimap<ClassEntry, T> m_unmatchedDestEntries; | ||
| 28 | private Multimap<ClassEntry, T> m_unmatchableSourceEntries; | ||
| 29 | |||
| 30 | public MemberMatches() { | ||
| 31 | m_matches = HashBiMap.create(); | ||
| 32 | m_matchedSourceEntries = HashMultimap.create(); | ||
| 33 | m_unmatchedSourceEntries = HashMultimap.create(); | ||
| 34 | m_unmatchedDestEntries = HashMultimap.create(); | ||
| 35 | m_unmatchableSourceEntries = HashMultimap.create(); | ||
| 36 | } | ||
| 37 | |||
| 38 | public void addMatch(T srcEntry, T destEntry) { | ||
| 39 | boolean wasAdded = m_matches.put(srcEntry, destEntry) == null; | ||
| 40 | assert (wasAdded); | ||
| 41 | wasAdded = m_matchedSourceEntries.put(srcEntry.getClassEntry(), srcEntry); | ||
| 42 | assert (wasAdded); | ||
| 43 | } | ||
| 44 | |||
| 45 | public void addUnmatchedSourceEntry(T sourceEntry) { | ||
| 46 | boolean wasAdded = m_unmatchedSourceEntries.put(sourceEntry.getClassEntry(), sourceEntry); | ||
| 47 | assert (wasAdded); | ||
| 48 | } | ||
| 49 | |||
| 50 | public void addUnmatchedDestEntry(T destEntry) { | ||
| 51 | boolean wasAdded = m_unmatchedDestEntries.put(destEntry.getClassEntry(), destEntry); | ||
| 52 | assert (wasAdded); | ||
| 53 | } | ||
| 54 | |||
| 55 | public void addUnmatchableSourceEntry(T sourceEntry) { | ||
| 56 | boolean wasAdded = m_unmatchableSourceEntries.put(sourceEntry.getClassEntry(), sourceEntry); | ||
| 57 | assert (wasAdded); | ||
| 58 | } | ||
| 59 | |||
| 60 | public Set<ClassEntry> getSourceClassesWithUnmatchedEntries() { | ||
| 61 | return m_unmatchedSourceEntries.keySet(); | ||
| 62 | } | ||
| 63 | |||
| 64 | public Collection<ClassEntry> getSourceClassesWithoutUnmatchedEntries() { | ||
| 65 | Set<ClassEntry> out = Sets.newHashSet(); | ||
| 66 | out.addAll(m_matchedSourceEntries.keySet()); | ||
| 67 | out.removeAll(m_unmatchedSourceEntries.keySet()); | ||
| 68 | return out; | ||
| 69 | } | ||
| 70 | |||
| 71 | public Collection<T> getUnmatchedSourceEntries() { | ||
| 72 | return m_unmatchedSourceEntries.values(); | ||
| 73 | } | ||
| 74 | |||
| 75 | public Collection<T> getUnmatchedSourceEntries(ClassEntry sourceClass) { | ||
| 76 | return m_unmatchedSourceEntries.get(sourceClass); | ||
| 77 | } | ||
| 78 | |||
| 79 | public Collection<T> getUnmatchedDestEntries() { | ||
| 80 | return m_unmatchedDestEntries.values(); | ||
| 81 | } | ||
| 82 | |||
| 83 | public Collection<T> getUnmatchedDestEntries(ClassEntry destClass) { | ||
| 84 | return m_unmatchedDestEntries.get(destClass); | ||
| 85 | } | ||
| 86 | |||
| 87 | public Collection<T> getUnmatchableSourceEntries() { | ||
| 88 | return m_unmatchableSourceEntries.values(); | ||
| 89 | } | ||
| 90 | |||
| 91 | public boolean hasSource(T sourceEntry) { | ||
| 92 | return m_matches.containsKey(sourceEntry) || m_unmatchedSourceEntries.containsValue(sourceEntry); | ||
| 93 | } | ||
| 94 | |||
| 95 | public boolean hasDest(T destEntry) { | ||
| 96 | return m_matches.containsValue(destEntry) || m_unmatchedDestEntries.containsValue(destEntry); | ||
| 97 | } | ||
| 98 | |||
| 99 | public BiMap<T, T> matches() { | ||
| 100 | return m_matches; | ||
| 101 | } | ||
| 102 | |||
| 103 | public boolean isMatchedSourceEntry(T sourceEntry) { | ||
| 104 | return m_matches.containsKey(sourceEntry); | ||
| 105 | } | ||
| 106 | |||
| 107 | public boolean isMatchedDestEntry(T destEntry) { | ||
| 108 | return m_matches.containsValue(destEntry); | ||
| 109 | } | ||
| 110 | |||
| 111 | public boolean isUnmatchableSourceEntry(T sourceEntry) { | ||
| 112 | return m_unmatchableSourceEntries.containsEntry(sourceEntry.getClassEntry(), sourceEntry); | ||
| 113 | } | ||
| 114 | |||
| 115 | public void makeMatch(T sourceEntry, T destEntry) { | ||
| 116 | boolean wasRemoved = m_unmatchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry); | ||
| 117 | assert (wasRemoved); | ||
| 118 | wasRemoved = m_unmatchedDestEntries.remove(destEntry.getClassEntry(), destEntry); | ||
| 119 | assert (wasRemoved); | ||
| 120 | addMatch(sourceEntry, destEntry); | ||
| 121 | } | ||
| 122 | |||
| 123 | public boolean isMatched(T sourceEntry, T destEntry) { | ||
| 124 | T match = m_matches.get(sourceEntry); | ||
| 125 | return match != null && match.equals(destEntry); | ||
| 126 | } | ||
| 127 | |||
| 128 | public void unmakeMatch(T sourceEntry, T destEntry) { | ||
| 129 | boolean wasRemoved = m_matches.remove(sourceEntry) != null; | ||
| 130 | assert (wasRemoved); | ||
| 131 | wasRemoved = m_matchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry); | ||
| 132 | assert (wasRemoved); | ||
| 133 | addUnmatchedSourceEntry(sourceEntry); | ||
| 134 | addUnmatchedDestEntry(destEntry); | ||
| 135 | } | ||
| 136 | |||
| 137 | public void makeSourceUnmatchable(T sourceEntry) { | ||
| 138 | assert (!isMatchedSourceEntry(sourceEntry)); | ||
| 139 | boolean wasRemoved = m_unmatchedSourceEntries.remove(sourceEntry.getClassEntry(), sourceEntry); | ||
| 140 | assert (wasRemoved); | ||
| 141 | addUnmatchableSourceEntry(sourceEntry); | ||
| 142 | } | ||
| 143 | } | ||