From ba7a354efae7d49833c887cf147ac940c975a1fa Mon Sep 17 00:00:00 2001 From: Gegy Date: Wed, 30 Jan 2019 21:05:32 +0200 Subject: Remap sources (#106) * Source remapping beginnings * Fix navigation to remapped classes * Translate identifier info reference * Remap local variables with default names in source * Caching translator * Fix lack of highlighting for first opened class * Fix unicode variable names * Unicode checker shouldn't be checking just alphanumeric * Fix package tree being built from obf names * Don't index `this` as method call for method::reference * Apply proposed names * Fix source export issues * Replace unicode var names at bytecode level uniquely * Drop imports from editor source * Class selector fixes * Delta keep track of base mappings to enable lookup of old names * Optimize source remapping by remapping source with a StringBuffer instead of copying * Bump version --- .../enigma/analysis/DropImportAstTransform.java | 33 +++++++++ .../java/cuchaz/enigma/analysis/ParsedJar.java | 34 +++++---- .../java/cuchaz/enigma/analysis/SourceIndex.java | 86 +++++++++++++--------- .../enigma/analysis/SourceIndexClassVisitor.java | 14 ++-- .../enigma/analysis/SourceIndexMethodVisitor.java | 64 +++++++--------- .../cuchaz/enigma/analysis/SourceIndexVisitor.java | 9 +-- src/main/java/cuchaz/enigma/analysis/Token.java | 6 +- .../enigma/analysis/index/BridgeMethodIndex.java | 16 +--- .../cuchaz/enigma/analysis/index/EntryIndex.java | 20 +---- .../enigma/analysis/index/InheritanceIndex.java | 18 +---- .../cuchaz/enigma/analysis/index/JarIndex.java | 22 +----- .../enigma/analysis/index/RemappableIndex.java | 9 --- 12 files changed, 147 insertions(+), 184 deletions(-) create mode 100644 src/main/java/cuchaz/enigma/analysis/DropImportAstTransform.java delete mode 100644 src/main/java/cuchaz/enigma/analysis/index/RemappableIndex.java (limited to 'src/main/java/cuchaz/enigma/analysis') diff --git a/src/main/java/cuchaz/enigma/analysis/DropImportAstTransform.java b/src/main/java/cuchaz/enigma/analysis/DropImportAstTransform.java new file mode 100644 index 0000000..991e91d --- /dev/null +++ b/src/main/java/cuchaz/enigma/analysis/DropImportAstTransform.java @@ -0,0 +1,33 @@ +package cuchaz.enigma.analysis; + +import com.strobel.decompiler.languages.java.ast.AstNode; +import com.strobel.decompiler.languages.java.ast.DepthFirstAstVisitor; +import com.strobel.decompiler.languages.java.ast.ImportDeclaration; +import com.strobel.decompiler.languages.java.ast.PackageDeclaration; +import com.strobel.decompiler.languages.java.ast.transforms.IAstTransform; + +public final class DropImportAstTransform implements IAstTransform { + public static final DropImportAstTransform INSTANCE = new DropImportAstTransform(); + + private DropImportAstTransform() { + } + + @Override + public void run(AstNode compilationUnit) { + compilationUnit.acceptVisitor(new Visitor(), null); + } + + static class Visitor extends DepthFirstAstVisitor { + @Override + public Void visitPackageDeclaration(PackageDeclaration node, Void data) { + node.remove(); + return null; + } + + @Override + public Void visitImportDeclaration(ImportDeclaration node, Void data) { + node.remove(); + return null; + } + } +} diff --git a/src/main/java/cuchaz/enigma/analysis/ParsedJar.java b/src/main/java/cuchaz/enigma/analysis/ParsedJar.java index ad3aceb..ddcda3e 100644 --- a/src/main/java/cuchaz/enigma/analysis/ParsedJar.java +++ b/src/main/java/cuchaz/enigma/analysis/ParsedJar.java @@ -12,9 +12,12 @@ package cuchaz.enigma.analysis; import com.google.common.io.ByteStreams; +import cuchaz.enigma.CompiledSource; +import cuchaz.enigma.bytecode.translators.LocalVariableFixVisitor; import cuchaz.enigma.translation.representation.entry.ClassEntry; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.ClassNode; import javax.annotation.Nullable; @@ -28,12 +31,12 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarInputStream; -public class ParsedJar { +public class ParsedJar implements CompiledSource { private final Map classBytes; private final Map nodeCache = new HashMap<>(); public ParsedJar(JarFile jar) throws IOException { - Map uClassBytes = new LinkedHashMap<>();; + Map uClassBytes = new LinkedHashMap<>(); try { // get the jar entries that correspond to classes Enumeration entries = jar.entries(); @@ -93,29 +96,34 @@ public class ParsedJar { return classBytes.size(); } - public List getClassEntries() { - List entries = new ArrayList<>(classBytes.size()); - for (String s : classBytes.keySet()) { - entries.add(new ClassEntry(s)); - } - return entries; - } - @Nullable + @Override public ClassNode getClassNode(String name) { return nodeCache.computeIfAbsent(name, (n) -> { byte[] bytes = classBytes.get(name); if (bytes == null) { return null; } + ClassReader reader = new ClassReader(bytes); ClassNode node = new ClassNode(); - reader.accept(node, 0); + + LocalVariableFixVisitor visitor = new LocalVariableFixVisitor(Opcodes.ASM5, node); + reader.accept(visitor, 0); + return node; }); } - public Map getClassDataMap() { + public List getClassEntries() { + List entries = new ArrayList<>(classBytes.size()); + for (String s : classBytes.keySet()) { + entries.add(new ClassEntry(s)); + } + return entries; + } + + public Map getClassDataMap() { return classBytes; - } + } } diff --git a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java index abdec92..ed12ce3 100644 --- a/src/main/java/cuchaz/enigma/analysis/SourceIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/SourceIndex.java @@ -11,17 +11,24 @@ package cuchaz.enigma.analysis; -import com.google.common.collect.*; +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.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 com.strobel.decompiler.languages.java.ast.*; +import cuchaz.enigma.gui.SourceRemapper; +import cuchaz.enigma.translation.mapping.EntryResolver; +import cuchaz.enigma.translation.mapping.ResolutionStrategy; import cuchaz.enigma.translation.representation.entry.Entry; import javax.annotation.Nullable; -import java.util.*; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class SourceIndex { private static Pattern ANONYMOUS_INNER = Pattern.compile("\\$\\d+$"); @@ -46,6 +53,13 @@ public class SourceIndex { calculateLineOffsets(); } + public static SourceIndex buildIndex(String sourceString, CompilationUnit sourceTree, boolean ignoreBadTokens) { + SourceIndex index = new SourceIndex(sourceString, ignoreBadTokens); + sourceTree.acceptVisitor(new SourceIndexVisitor(), index); + + return index; + } + private void calculateLineOffsets() { // count the lines this.lineOffsets = Lists.newArrayList(); @@ -57,32 +71,29 @@ public class SourceIndex { } } - public void remap(String source, Map tokenMap) { - this.source = source; - calculateLineOffsets(); + public SourceIndex remapTo(SourceRemapper.Result result) { + SourceIndex remapped = new SourceIndex(result.getSource(), ignoreBadTokens); - for (Entry entry : Lists.newArrayList(declarationToToken.keySet())) { - Token token = declarationToToken.get(entry); - declarationToToken.put(entry, tokenMap.getOrDefault(token, token)); + for (Map.Entry, Token> entry : declarationToToken.entrySet()) { + remapped.declarationToToken.put(entry.getKey(), result.getRemappedToken(entry.getValue())); } - for (EntryReference, Entry> ref : referenceToTokens.keySet()) { - Collection oldTokens = referenceToTokens.get(ref); - List newTokens = new ArrayList<>(oldTokens.size()); + for (Map.Entry, Entry>, Collection> entry : referenceToTokens.asMap().entrySet()) { + EntryReference, Entry> reference = entry.getKey(); + Collection oldTokens = entry.getValue(); - for (Token token : oldTokens) { - newTokens.add(tokenMap.getOrDefault(token, token)); - } + Collection newTokens = oldTokens.stream() + .map(result::getRemappedToken) + .collect(Collectors.toList()); - referenceToTokens.replaceValues(ref, newTokens); + remapped.referenceToTokens.putAll(reference, newTokens); } - TreeMap, Entry>> tokenToReferenceCopy = new TreeMap<>(tokenToReference); - - tokenToReference.clear(); - for (Token token : tokenToReferenceCopy.keySet()) { - tokenToReference.put(tokenMap.getOrDefault(token, token), tokenToReferenceCopy.get(token)); + for (Map.Entry, Entry>> entry : tokenToReference.entrySet()) { + remapped.tokenToReference.put(result.getRemappedToken(entry.getKey()), entry.getValue()); } + + return remapped; } public String getSource() { @@ -164,20 +175,13 @@ public class SourceIndex { } @Nullable - public EntryReference, Entry> getDeobfReference(Token token) { + public EntryReference, Entry> getReference(Token token) { if (token == null) { return null; } return this.tokenToReference.get(token); } - 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); - } - public Iterable referenceTokens() { return this.tokenToReference.keySet(); } @@ -190,8 +194,8 @@ public class SourceIndex { return this.declarationToToken.keySet(); } - public Token getDeclarationToken(Entry deobfEntry) { - return this.declarationToToken.get(deobfEntry); + public Token getDeclarationToken(Entry entry) { + return this.declarationToToken.get(entry); } public int getLineNumber(int pos) { @@ -215,4 +219,18 @@ public class SourceIndex { // line and col are 1-based return this.lineOffsets.get(line - 1) + col - 1; } + + public void resolveReferences(EntryResolver resolver) { + // resolve all the classes in the source references + for (Token token : Lists.newArrayList(referenceToTokens.values())) { + EntryReference, Entry> reference = tokenToReference.get(token); + EntryReference, Entry> resolvedReference = resolver.resolveFirstReference(reference, ResolutionStrategy.RESOLVE_CLOSEST); + + // replace the reference + tokenToReference.replace(token, resolvedReference); + + Collection tokens = referenceToTokens.removeAll(reference); + referenceToTokens.putAll(resolvedReference, tokens); + } + } } diff --git a/src/main/java/cuchaz/enigma/analysis/SourceIndexClassVisitor.java b/src/main/java/cuchaz/enigma/analysis/SourceIndexClassVisitor.java index 486603c..a4fe9ee 100644 --- a/src/main/java/cuchaz/enigma/analysis/SourceIndexClassVisitor.java +++ b/src/main/java/cuchaz/enigma/analysis/SourceIndexClassVisitor.java @@ -18,21 +18,17 @@ import com.strobel.assembler.metadata.TypeReference; import com.strobel.decompiler.languages.TextLocation; import com.strobel.decompiler.languages.java.ast.*; import cuchaz.enigma.translation.representation.ProcyonEntryFactory; -import cuchaz.enigma.translation.representation.ReferencedEntryPool; import cuchaz.enigma.translation.representation.entry.ClassDefEntry; import cuchaz.enigma.translation.representation.entry.ClassEntry; import cuchaz.enigma.translation.representation.entry.FieldDefEntry; import cuchaz.enigma.translation.representation.entry.MethodDefEntry; public class SourceIndexClassVisitor extends SourceIndexVisitor { - private final ReferencedEntryPool entryPool; private final ProcyonEntryFactory entryFactory; private ClassDefEntry classEntry; - public SourceIndexClassVisitor(ReferencedEntryPool entryPool, ClassDefEntry classEntry) { - super(entryPool); - this.entryPool = entryPool; - this.entryFactory = new ProcyonEntryFactory(entryPool); + public SourceIndexClassVisitor(ClassDefEntry classEntry) { + this.entryFactory = new ProcyonEntryFactory(); this.classEntry = classEntry; } @@ -44,7 +40,7 @@ public class SourceIndexClassVisitor extends SourceIndexVisitor { if (!classEntry.equals(this.classEntry)) { // it's a subtype, recurse index.addDeclaration(node.getNameToken(), classEntry); - return node.acceptVisitor(new SourceIndexClassVisitor(entryPool, classEntry), index); + return node.acceptVisitor(new SourceIndexClassVisitor(classEntry), index); } return recurse(node, index); @@ -71,7 +67,7 @@ public class SourceIndexClassVisitor extends SourceIndexVisitor { tokenNode = node.getModifiers().firstOrNullObject(); } index.addDeclaration(tokenNode, methodEntry); - return node.acceptVisitor(new SourceIndexMethodVisitor(entryPool, methodEntry), index); + return node.acceptVisitor(new SourceIndexMethodVisitor(methodEntry), index); } @Override @@ -79,7 +75,7 @@ public class SourceIndexClassVisitor extends SourceIndexVisitor { MethodDefinition def = node.getUserData(Keys.METHOD_DEFINITION); MethodDefEntry methodEntry = entryFactory.getMethodDefEntry(def); index.addDeclaration(node.getNameToken(), methodEntry); - return node.acceptVisitor(new SourceIndexMethodVisitor(entryPool, methodEntry), index); + return node.acceptVisitor(new SourceIndexMethodVisitor(methodEntry), index); } @Override diff --git a/src/main/java/cuchaz/enigma/analysis/SourceIndexMethodVisitor.java b/src/main/java/cuchaz/enigma/analysis/SourceIndexMethodVisitor.java index 73db28f..c4785b6 100644 --- a/src/main/java/cuchaz/enigma/analysis/SourceIndexMethodVisitor.java +++ b/src/main/java/cuchaz/enigma/analysis/SourceIndexMethodVisitor.java @@ -17,8 +17,8 @@ import com.strobel.assembler.metadata.*; import com.strobel.decompiler.ast.Variable; import com.strobel.decompiler.languages.TextLocation; import com.strobel.decompiler.languages.java.ast.*; +import cuchaz.enigma.translation.representation.MethodDescriptor; import cuchaz.enigma.translation.representation.TypeDescriptor; -import cuchaz.enigma.translation.representation.*; import cuchaz.enigma.translation.representation.entry.*; import java.lang.Error; @@ -26,16 +26,12 @@ import java.util.HashMap; import java.util.Map; public class SourceIndexMethodVisitor extends SourceIndexVisitor { - private final ReferencedEntryPool entryPool; - private final MethodDefEntry methodEntry; private Multimap unmatchedIdentifier = HashMultimap.create(); private Map> identifierEntryCache = new HashMap<>(); - public SourceIndexMethodVisitor(ReferencedEntryPool entryPool, MethodDefEntry methodEntry) { - super(entryPool); - this.entryPool = entryPool; + public SourceIndexMethodVisitor(MethodDefEntry methodEntry) { this.methodEntry = methodEntry; } @@ -44,10 +40,10 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); // get the behavior entry - ClassEntry classEntry = entryPool.getClass(ref.getDeclaringType().getInternalName()); + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); MethodEntry methodEntry = null; if (ref instanceof MethodReference) { - methodEntry = entryPool.getMethod(classEntry, ref.getName(), ref.getErasedSignature()); + methodEntry = new MethodEntry(classEntry, ref.getName(), new MethodDescriptor(ref.getErasedSignature())); } if (methodEntry != null) { // get the node for the token @@ -80,11 +76,8 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { throw new Error("Expected a field here! got " + ref); } - ClassEntry classEntry = entryPool.getClass(ref.getDeclaringType().getInternalName()); - FieldEntry fieldEntry = entryPool.getField(classEntry, ref.getName(), new TypeDescriptor(erasedSignature)); - if (fieldEntry == null) { - throw new Error("Failed to find field " + ref.getName() + " on " + classEntry.getFullName()); - } + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); + FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName(), new TypeDescriptor(erasedSignature)); index.addReference(node.getMemberNameToken(), fieldEntry, this.methodEntry); } @@ -95,7 +88,7 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { public Void visitSimpleType(SimpleType node, SourceIndex index) { TypeReference ref = node.getUserData(Keys.TYPE_REFERENCE); if (node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY) { - ClassEntry classEntry = entryPool.getClass(ref.getInternalName()); + ClassEntry classEntry = new ClassEntry(ref.getInternalName()); index.addReference(node.getIdentifierToken(), classEntry, this.methodEntry); } @@ -108,7 +101,8 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { int parameterIndex = def.getSlot(); if (parameterIndex >= 0) { - LocalVariableEntry localVariableEntry = new LocalVariableEntry(methodEntry, parameterIndex, node.getName(), true); + TypeDescriptor parameterType = TypeDescriptor.parse(def.getParameterType()); + LocalVariableDefEntry localVariableEntry = new LocalVariableDefEntry(methodEntry, parameterIndex, node.getName(), true, parameterType); Identifier identifier = node.getNameToken(); // cache the argument entry and the identifier identifierEntryCache.put(identifier.getName(), localVariableEntry); @@ -122,11 +116,8 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { public Void visitIdentifierExpression(IdentifierExpression node, SourceIndex index) { MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); if (ref != null) { - ClassEntry classEntry = entryPool.getClass(ref.getDeclaringType().getInternalName()); - FieldEntry fieldEntry = entryPool.getField(classEntry, ref.getName(), new TypeDescriptor(ref.getErasedSignature())); - if (fieldEntry == null) { - throw new Error("Failed to find field " + ref.getName() + " on " + classEntry.getFullName()); - } + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); + FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName(), new TypeDescriptor(ref.getErasedSignature())); index.addReference(node.getIdentifierToken(), fieldEntry, this.methodEntry); } else this.checkIdentifier(node, index); @@ -154,13 +145,11 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { @Override public Void visitObjectCreationExpression(ObjectCreationExpression node, SourceIndex index) { MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); - if (ref != null) { - ClassEntry classEntry = entryPool.getClass(ref.getDeclaringType().getInternalName()); - MethodEntry constructorEntry = entryPool.getMethod(classEntry, "", ref.getErasedSignature()); - if (node.getType() instanceof SimpleType) { - SimpleType simpleTypeNode = (SimpleType) node.getType(); - index.addReference(simpleTypeNode.getIdentifierToken(), constructorEntry, this.methodEntry); - } + if (ref != null && node.getType() instanceof SimpleType) { + SimpleType simpleTypeNode = (SimpleType) node.getType(); + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); + MethodEntry constructorEntry = new MethodEntry(classEntry, "", new MethodDescriptor(ref.getErasedSignature())); + index.addReference(simpleTypeNode.getIdentifierToken(), constructorEntry, this.methodEntry); } return recurse(node, index); @@ -181,7 +170,8 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { if (originalVariable != null) { int variableIndex = originalVariable.getSlot(); if (variableIndex >= 0) { - LocalVariableEntry localVariableEntry = new LocalVariableEntry(methodEntry, variableIndex, initializer.getName(), false); + TypeDescriptor variableType = TypeDescriptor.parse(originalVariable.getVariableType()); + LocalVariableDefEntry localVariableEntry = new LocalVariableDefEntry(methodEntry, variableIndex, initializer.getName(), false, variableType); identifierEntryCache.put(identifier.getName(), localVariableEntry); addDeclarationToUnmatched(identifier.getName(), index); index.addDeclaration(identifier, localVariableEntry); @@ -199,17 +189,19 @@ public class SourceIndexMethodVisitor extends SourceIndexVisitor { if (ref instanceof MethodReference) { // get the behavior entry - ClassEntry classEntry = entryPool.getClass(ref.getDeclaringType().getInternalName()); - MethodEntry methodEntry = null; + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); + MethodEntry methodEntry = new MethodEntry(classEntry, ref.getName(), new MethodDescriptor(ref.getErasedSignature())); - methodEntry = entryPool.getMethod(classEntry, ref.getName(), ref.getErasedSignature()); // get the node for the token - AstNode tokenNode = node.getMethodNameToken(); - if (tokenNode == null || (tokenNode.getRegion().getBeginLine() == 0 || tokenNode.getRegion().getEndLine() == 0)){ - tokenNode = node.getTarget(); + AstNode methodNameToken = node.getMethodNameToken(); + AstNode targetToken = node.getTarget(); + + if (methodNameToken != null) { + index.addReference(methodNameToken, methodEntry, this.methodEntry); } - if (tokenNode != null) { - index.addReference(tokenNode, methodEntry, this.methodEntry); + + if (targetToken != null && !(targetToken instanceof ThisReferenceExpression)) { + index.addReference(targetToken, methodEntry.getParent(), this.methodEntry); } } diff --git a/src/main/java/cuchaz/enigma/analysis/SourceIndexVisitor.java b/src/main/java/cuchaz/enigma/analysis/SourceIndexVisitor.java index 564830c..75a66a2 100644 --- a/src/main/java/cuchaz/enigma/analysis/SourceIndexVisitor.java +++ b/src/main/java/cuchaz/enigma/analysis/SourceIndexVisitor.java @@ -14,23 +14,16 @@ package cuchaz.enigma.analysis; import com.strobel.assembler.metadata.TypeDefinition; import com.strobel.decompiler.languages.java.ast.*; import com.strobel.decompiler.patterns.Pattern; -import cuchaz.enigma.translation.representation.ReferencedEntryPool; import cuchaz.enigma.translation.representation.entry.ClassDefEntry; public class SourceIndexVisitor implements IAstVisitor { - private final ReferencedEntryPool entryPool; - - public SourceIndexVisitor(ReferencedEntryPool entryPool) { - this.entryPool = entryPool; - } - @Override public Void visitTypeDeclaration(TypeDeclaration node, SourceIndex index) { TypeDefinition def = node.getUserData(Keys.TYPE_DEFINITION); ClassDefEntry classEntry = ClassDefEntry.parse(def); index.addDeclaration(node.getNameToken(), classEntry); - return node.acceptVisitor(new SourceIndexClassVisitor(entryPool, classEntry), index); + return node.acceptVisitor(new SourceIndexClassVisitor(classEntry), index); } protected Void recurse(AstNode node, SourceIndex index) { diff --git a/src/main/java/cuchaz/enigma/analysis/Token.java b/src/main/java/cuchaz/enigma/analysis/Token.java index 14fa7ca..12e0aa6 100644 --- a/src/main/java/cuchaz/enigma/analysis/Token.java +++ b/src/main/java/cuchaz/enigma/analysis/Token.java @@ -30,12 +30,12 @@ public class Token implements Comparable { return to.length() - length; } - public String rename(String source, String to) { + public void rename(StringBuffer source, String to) { int oldEnd = this.end; this.text = to; this.end = this.start + to.length(); - return source.substring(0, this.start) + to + source.substring(oldEnd); + source.replace(start, oldEnd, to); } public Token move(int offset) { @@ -64,7 +64,7 @@ public class Token implements Comparable { } public boolean equals(Token other) { - return start == other.start && end == other.end; + return start == other.start && end == other.end && text.equals(other.text); } @Override diff --git a/src/main/java/cuchaz/enigma/analysis/index/BridgeMethodIndex.java b/src/main/java/cuchaz/enigma/analysis/index/BridgeMethodIndex.java index e1903d9..8f6bd46 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/BridgeMethodIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/index/BridgeMethodIndex.java @@ -1,7 +1,6 @@ package cuchaz.enigma.analysis.index; import com.google.common.collect.Maps; -import cuchaz.enigma.translation.Translator; import cuchaz.enigma.translation.mapping.EntryResolver; import cuchaz.enigma.translation.representation.AccessFlags; import cuchaz.enigma.translation.representation.entry.MethodEntry; @@ -10,7 +9,7 @@ import javax.annotation.Nullable; import java.util.Collection; import java.util.Map; -public class BridgeMethodIndex implements JarIndexer, RemappableIndex { +public class BridgeMethodIndex implements JarIndexer { private final EntryIndex entryIndex; private final ReferenceIndex referenceIndex; @@ -21,19 +20,6 @@ public class BridgeMethodIndex implements JarIndexer, RemappableIndex { this.referenceIndex = referenceIndex; } - @Override - public void remap(Translator translator) { - accessedToBridge = translator.translate(accessedToBridge); - } - - @Override - public BridgeMethodIndex remapped(Translator translator) { - BridgeMethodIndex index = new BridgeMethodIndex(entryIndex, referenceIndex); - index.accessedToBridge = translator.translate(accessedToBridge); - - return index; - } - @Override public void processIndex(EntryResolver resolver) { // look for access and bridged methods diff --git a/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java b/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java index 55bfbc2..773eaf1 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/index/EntryIndex.java @@ -1,6 +1,5 @@ package cuchaz.enigma.analysis.index; -import cuchaz.enigma.translation.Translator; import cuchaz.enigma.translation.representation.AccessFlags; import cuchaz.enigma.translation.representation.entry.*; @@ -9,28 +8,11 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; -public class EntryIndex implements JarIndexer, RemappableIndex { +public class EntryIndex implements JarIndexer { private Map classes = new HashMap<>(); private Map fields = new HashMap<>(); private Map methods = new HashMap<>(); - @Override - public void remap(Translator translator) { - classes = translator.translateKeys(classes); - fields = translator.translateKeys(fields); - methods = translator.translateKeys(methods); - } - - @Override - public EntryIndex remapped(Translator translator) { - EntryIndex index = new EntryIndex(); - index.classes = translator.translateKeys(classes); - index.fields = translator.translateKeys(fields); - index.methods = translator.translateKeys(methods); - - return index; - } - @Override public void indexClass(ClassDefEntry classEntry) { classes.put(classEntry, classEntry.getAccess()); diff --git a/src/main/java/cuchaz/enigma/analysis/index/InheritanceIndex.java b/src/main/java/cuchaz/enigma/analysis/index/InheritanceIndex.java index d165cc8..17bed54 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/InheritanceIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/index/InheritanceIndex.java @@ -14,7 +14,6 @@ package cuchaz.enigma.analysis.index; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; -import cuchaz.enigma.translation.Translator; import cuchaz.enigma.translation.representation.entry.ClassDefEntry; import cuchaz.enigma.translation.representation.entry.ClassEntry; @@ -22,25 +21,10 @@ import java.util.Collection; import java.util.LinkedList; import java.util.Set; -public class InheritanceIndex implements JarIndexer, RemappableIndex { +public class InheritanceIndex implements JarIndexer { private Multimap classParents = HashMultimap.create(); private Multimap classChildren = HashMultimap.create(); - @Override - public void remap(Translator translator) { - classChildren = translator.translate(classChildren); - classParents = translator.translate(classParents); - } - - @Override - public InheritanceIndex remapped(Translator translator) { - InheritanceIndex index = new InheritanceIndex(); - index.classParents = translator.translate(classParents); - index.classChildren = translator.translate(classChildren); - - return index; - } - @Override public void indexClass(ClassDefEntry classEntry) { ClassEntry superClass = classEntry.getSuperClass(); diff --git a/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java b/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java index 0880244..9b21cba 100644 --- a/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java +++ b/src/main/java/cuchaz/enigma/analysis/index/JarIndex.java @@ -14,7 +14,6 @@ package cuchaz.enigma.analysis.index; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import cuchaz.enigma.analysis.ParsedJar; -import cuchaz.enigma.translation.Translator; import cuchaz.enigma.translation.mapping.EntryResolver; import cuchaz.enigma.translation.mapping.IndexEntryResolver; import cuchaz.enigma.translation.representation.entry.*; @@ -25,7 +24,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.function.Consumer; -public class JarIndex implements JarIndexer, RemappableIndex { +public class JarIndex implements JarIndexer { private final EntryIndex entryIndex; private final InheritanceIndex inheritanceIndex; private final ReferenceIndex referenceIndex; @@ -53,25 +52,6 @@ public class JarIndex implements JarIndexer, RemappableIndex { return new JarIndex(entryIndex, inheritanceIndex, referenceIndex, bridgeMethodIndex); } - @Override - public void remap(Translator translator) { - entryIndex.remap(translator); - inheritanceIndex.remap(translator); - bridgeMethodIndex.remap(translator); - } - - @Override - public JarIndex remapped(Translator translator) { - EntryIndex entryIndex = this.entryIndex.remapped(translator); - InheritanceIndex inheritanceIndex = this.inheritanceIndex.remapped(translator); - BridgeMethodIndex bridgeMethodIndex = this.bridgeMethodIndex.remapped(translator); - - JarIndex remappedIndex = new JarIndex(entryIndex, inheritanceIndex, this.referenceIndex, bridgeMethodIndex); - remappedIndex.methodImplementations.putAll(methodImplementations); - - return remappedIndex; - } - public void indexJar(ParsedJar jar, Consumer progress) { progress.accept("Indexing entries (1/3)"); jar.visitReader(name -> new IndexClassVisitor(this, Opcodes.ASM5), ClassReader.SKIP_CODE); diff --git a/src/main/java/cuchaz/enigma/analysis/index/RemappableIndex.java b/src/main/java/cuchaz/enigma/analysis/index/RemappableIndex.java deleted file mode 100644 index 537e772..0000000 --- a/src/main/java/cuchaz/enigma/analysis/index/RemappableIndex.java +++ /dev/null @@ -1,9 +0,0 @@ -package cuchaz.enigma.analysis.index; - -import cuchaz.enigma.translation.Translator; - -public interface RemappableIndex { - void remap(Translator translator); - - RemappableIndex remapped(Translator translator); -} -- cgit v1.2.3