From 00fcd0550fcdda621c2e4662f6ddd55ce673b931 Mon Sep 17 00:00:00 2001 From: Gegy Date: Thu, 24 Jan 2019 14:48:32 +0200 Subject: [WIP] Mapping rework (#91) * Move packages * Mapping & entry refactor: first pass * Fix deobf -> obf tree remapping * Resolve various issues * Give all entries the potential for parents and treat inner classes as children * Deobf UI tree elements * Tests pass * Sort mapping output * Fix delta tracking * Index separation and first pass for #97 * Keep track of remapped jar index * Fix child entries not being remapped * Drop non-root entries * Track dropped mappings * Fix enigma mapping ordering * EntryTreeNode interface * Small tweaks * Naive full index remap on rename * Entries can resolve to more than one root entry * Support alternative resolution strategies * Bridge method resolution * Tests pass * Fix mappings being used where there are none * Fix methods with different descriptors being considered unique. closes #89 --- .../java/cuchaz/enigma/analysis/SourceIndex.java | 53 +++++++++++----------- 1 file changed, 26 insertions(+), 27 deletions(-) (limited to 'src/main/java/cuchaz/enigma/analysis/SourceIndex.java') diff --git a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java index 3e0d66b..abdec92 100644 --- a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java @@ -11,17 +11,15 @@ package cuchaz.enigma.analysis; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; +import com.google.common.collect.*; import com.strobel.decompiler.languages.Region; import com.strobel.decompiler.languages.java.ast.AstNode; import com.strobel.decompiler.languages.java.ast.ConstructorDeclaration; import com.strobel.decompiler.languages.java.ast.Identifier; import com.strobel.decompiler.languages.java.ast.TypeDeclaration; -import cuchaz.enigma.mapping.entry.Entry; +import cuchaz.enigma.translation.representation.entry.Entry; +import javax.annotation.Nullable; import java.util.*; import java.util.regex.Pattern; @@ -29,9 +27,9 @@ public class SourceIndex { private static Pattern ANONYMOUS_INNER = Pattern.compile("\\$\\d+$"); private String source; - private TreeMap> tokenToReference; - private Multimap, Token> referenceToTokens; - private Map declarationToToken; + private TreeMap, Entry>> tokenToReference; + private Multimap, Entry>, Token> referenceToTokens; + private Map, Token> declarationToToken; private List lineOffsets; private boolean ignoreBadTokens; @@ -42,7 +40,7 @@ public class SourceIndex { public SourceIndex(String source, boolean ignoreBadTokens) { this.source = source; this.ignoreBadTokens = ignoreBadTokens; - this.tokenToReference = Maps.newTreeMap(); + this.tokenToReference = new TreeMap<>(); this.referenceToTokens = HashMultimap.create(); this.declarationToToken = Maps.newHashMap(); calculateLineOffsets(); @@ -63,12 +61,12 @@ public class SourceIndex { this.source = source; calculateLineOffsets(); - for (Entry entry : Lists.newArrayList(declarationToToken.keySet())) { + for (Entry entry : Lists.newArrayList(declarationToToken.keySet())) { Token token = declarationToToken.get(entry); declarationToToken.put(entry, tokenMap.getOrDefault(token, token)); } - for (EntryReference ref : referenceToTokens.keySet()) { + for (EntryReference, Entry> ref : referenceToTokens.keySet()) { Collection oldTokens = referenceToTokens.get(ref); List newTokens = new ArrayList<>(oldTokens.size()); @@ -79,7 +77,8 @@ public class SourceIndex { referenceToTokens.replaceValues(ref, newTokens); } - Map> tokenToReferenceCopy = Maps.newHashMap(tokenToReference); + TreeMap, Entry>> tokenToReferenceCopy = new TreeMap<>(tokenToReference); + tokenToReference.clear(); for (Token token : tokenToReferenceCopy.keySet()) { tokenToReference.put(tokenMap.getOrDefault(token, token), tokenToReferenceCopy.get(token)); @@ -112,9 +111,9 @@ public class SourceIndex { return null; } - if (node instanceof Identifier && name.indexOf('$') >=0 && node.getParent() instanceof ConstructorDeclaration && name.lastIndexOf('$') >= 0 && !ANONYMOUS_INNER.matcher(name).matches()){ + if (node instanceof Identifier && name.indexOf('$') >= 0 && node.getParent() instanceof ConstructorDeclaration && name.lastIndexOf('$') >= 0 && !ANONYMOUS_INNER.matcher(name).matches()) { TypeDeclaration type = node.getParent().getParent() instanceof TypeDeclaration ? (TypeDeclaration) node.getParent().getParent() : null; - if (type != null){ + if (type != null) { name = type.getName(); token.end = token.start + name.length(); } @@ -133,19 +132,19 @@ public class SourceIndex { return token; } - public void addReference(AstNode node, Entry deobfEntry, Entry deobfContext) { + public void addReference(AstNode node, Entry deobfEntry, Entry deobfContext) { Token token = getToken(node); if (token != null) { - EntryReference deobfReference = new EntryReference<>(deobfEntry, token.text, deobfContext); + EntryReference, Entry> deobfReference = new EntryReference<>(deobfEntry, token.text, deobfContext); this.tokenToReference.put(token, deobfReference); this.referenceToTokens.put(deobfReference, token); } } - public void addDeclaration(AstNode node, Entry deobfEntry) { + public void addDeclaration(AstNode node, Entry deobfEntry) { Token token = getToken(node); if (token != null) { - EntryReference reference = new EntryReference<>(deobfEntry, token.text); + EntryReference, Entry> reference = new EntryReference<>(deobfEntry, token.text); this.tokenToReference.put(token, reference); this.referenceToTokens.put(reference, token); this.declarationToToken.put(deobfEntry, token); @@ -160,22 +159,22 @@ public class SourceIndex { return null; } - public Collection getReferenceTokens(EntryReference deobfReference) { + public Collection getReferenceTokens(EntryReference, Entry> deobfReference) { return this.referenceToTokens.get(deobfReference); } - public EntryReference getDeobfReference(Token token) { + @Nullable + public EntryReference, Entry> getDeobfReference(Token token) { if (token == null) { return null; } return this.tokenToReference.get(token); } - public void replaceDeobfReference(Token token, EntryReference newDeobfReference) { - EntryReference oldDeobfReference = this.tokenToReference.get(token); - this.tokenToReference.put(token, newDeobfReference); - Collection tokens = this.referenceToTokens.get(oldDeobfReference); - this.referenceToTokens.removeAll(oldDeobfReference); + public void replaceDeobfReference(Token token, EntryReference, Entry> newDeobfReference) { + EntryReference, Entry> oldDeobfReferences = this.tokenToReference.replace(token, newDeobfReference); + + Collection tokens = this.referenceToTokens.removeAll(oldDeobfReferences); this.referenceToTokens.putAll(newDeobfReference, tokens); } @@ -187,11 +186,11 @@ public class SourceIndex { return this.declarationToToken.values(); } - public Iterable declarations() { + public Iterable> declarations() { return this.declarationToToken.keySet(); } - public Token getDeclarationToken(Entry deobfEntry) { + public Token getDeclarationToken(Entry deobfEntry) { return this.declarationToToken.get(deobfEntry); } -- cgit v1.2.3