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/TestJarIndexLoneClass.java | 93 ++++++++++++---------- 1 file changed, 50 insertions(+), 43 deletions(-) (limited to 'src/test/java/cuchaz/enigma/TestJarIndexLoneClass.java') diff --git a/src/test/java/cuchaz/enigma/TestJarIndexLoneClass.java b/src/test/java/cuchaz/enigma/TestJarIndexLoneClass.java index b4529dd..1299bcc 100644 --- a/src/test/java/cuchaz/enigma/TestJarIndexLoneClass.java +++ b/src/test/java/cuchaz/enigma/TestJarIndexLoneClass.java @@ -12,12 +12,19 @@ package cuchaz.enigma; import cuchaz.enigma.analysis.*; -import cuchaz.enigma.mapping.*; -import cuchaz.enigma.mapping.entry.*; +import cuchaz.enigma.analysis.index.EntryIndex; +import cuchaz.enigma.analysis.index.InheritanceIndex; +import cuchaz.enigma.analysis.index.JarIndex; +import cuchaz.enigma.translation.VoidTranslator; +import cuchaz.enigma.translation.representation.AccessFlags; +import cuchaz.enigma.translation.representation.entry.ClassEntry; +import cuchaz.enigma.translation.representation.entry.FieldEntry; +import cuchaz.enigma.translation.representation.entry.MethodDefEntry; +import cuchaz.enigma.translation.representation.entry.MethodEntry; import org.junit.Test; import java.util.Collection; -import java.util.Set; +import java.util.List; import java.util.jar.JarFile; import static cuchaz.enigma.TestEntryFactory.*; @@ -30,13 +37,13 @@ public class TestJarIndexLoneClass { public TestJarIndexLoneClass() throws Exception { - index = new JarIndex(new ReferencedEntryPool()); - index.indexJar(new ParsedJar(new JarFile("build/test-obf/loneClass.jar")), false); + index = JarIndex.empty(); + index.indexJar(new ParsedJar(new JarFile("build/test-obf/loneClass.jar")), s -> {}); } @Test public void obfEntries() { - assertThat(index.getObfClassEntries(), containsInAnyOrder( + assertThat(index.getEntryIndex().getClasses(), containsInAnyOrder( newClass("cuchaz/enigma/inputs/Keep"), newClass("a") )); @@ -44,25 +51,28 @@ public class TestJarIndexLoneClass { @Test public void translationIndex() { - assertThat(index.getTranslationIndex().getSuperclass(new ClassEntry("a")), is(new ClassEntry("java/lang/Object"))); - assertThat(index.getTranslationIndex().getSuperclass(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(new ClassEntry("java/lang/Object"))); - assertThat(index.getTranslationIndex().getAncestry(new ClassEntry("a")), contains(new ClassEntry("java/lang/Object"))); - assertThat(index.getTranslationIndex().getAncestry(new ClassEntry("cuchaz/enigma/inputs/Keep")), contains(new ClassEntry("java/lang/Object"))); - assertThat(index.getTranslationIndex().getSubclass(new ClassEntry("a")), is(empty())); - assertThat(index.getTranslationIndex().getSubclass(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(empty())); + InheritanceIndex inheritanceIndex = index.getInheritanceIndex(); + assertThat(inheritanceIndex.getParents(new ClassEntry("a")), is(empty())); + assertThat(inheritanceIndex.getParents(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(empty())); + assertThat(inheritanceIndex.getAncestors(new ClassEntry("a")), is(empty())); + assertThat(inheritanceIndex.getAncestors(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(empty())); + assertThat(inheritanceIndex.getChildren(new ClassEntry("a")), is(empty())); + assertThat(inheritanceIndex.getChildren(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(empty())); } @Test public void access() { - assertThat(index.getAccess(newField("a", "a", "Ljava/lang/String;")), is(Access.PRIVATE)); - assertThat(index.getAccess(newMethod("a", "a", "()Ljava/lang/String;")), is(Access.PUBLIC)); - assertThat(index.getAccess(newField("a", "b", "Ljava/lang/String;")), is(nullValue())); - assertThat(index.getAccess(newField("a", "a", "LFoo;")), is(nullValue())); + EntryIndex entryIndex = index.getEntryIndex(); + assertThat(entryIndex.getFieldAccess(newField("a", "a", "Ljava/lang/String;")), is(AccessFlags.PRIVATE)); + assertThat(entryIndex.getMethodAccess(newMethod("a", "a", "()Ljava/lang/String;")), is(AccessFlags.PUBLIC)); + assertThat(entryIndex.getFieldAccess(newField("a", "b", "Ljava/lang/String;")), is(nullValue())); + assertThat(entryIndex.getFieldAccess(newField("a", "a", "LFoo;")), is(nullValue())); } @Test public void classInheritance() { - ClassInheritanceTreeNode node = index.getClassInheritance(new DirectionalTranslator(new ReferencedEntryPool()), newClass("a")); + IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index); + ClassInheritanceTreeNode node = treeBuilder.buildClassInheritance(VoidTranslator.INSTANCE, newClass("a")); assertThat(node, is(not(nullValue()))); assertThat(node.getObfClassName(), is("a")); assertThat(node.getChildCount(), is(0)); @@ -70,8 +80,9 @@ public class TestJarIndexLoneClass { @Test public void methodInheritance() { + IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index); MethodEntry source = newMethod("a", "a", "()Ljava/lang/String;"); - MethodInheritanceTreeNode node = index.getMethodInheritance(new DirectionalTranslator(new ReferencedEntryPool()), source); + MethodInheritanceTreeNode node = treeBuilder.buildMethodInheritance(VoidTranslator.INSTANCE, source); assertThat(node, is(not(nullValue()))); assertThat(node.getMethodEntry(), is(source)); assertThat(node.getChildCount(), is(0)); @@ -79,19 +90,24 @@ public class TestJarIndexLoneClass { @Test public void classImplementations() { - ClassImplementationsTreeNode node = index.getClassImplementations(new DirectionalTranslator(new ReferencedEntryPool()), newClass("a")); + IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index); + ClassImplementationsTreeNode node = treeBuilder.buildClassImplementations(VoidTranslator.INSTANCE, newClass("a")); assertThat(node, is(nullValue())); } @Test public void methodImplementations() { + IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index); MethodEntry source = newMethod("a", "a", "()Ljava/lang/String;"); - assertThat(index.getMethodImplementations(new DirectionalTranslator(new ReferencedEntryPool()), source), is(empty())); + + List nodes = treeBuilder.buildMethodImplementations(VoidTranslator.INSTANCE, source); + assertThat(nodes, hasSize(1)); + assertThat(nodes.get(0).getMethodEntry(), is(source)); } @Test public void relatedMethodImplementations() { - Set entries = index.getRelatedMethodImplementations(newMethod("a", "a", "()Ljava/lang/String;")); + Collection entries = index.getEntryResolver().resolveEquivalentMethods(newMethod("a", "a", "()Ljava/lang/String;")); assertThat(entries, containsInAnyOrder( newMethod("a", "a", "()Ljava/lang/String;") )); @@ -101,7 +117,7 @@ public class TestJarIndexLoneClass { @SuppressWarnings("unchecked") public void fieldReferences() { FieldEntry source = newField("a", "a", "Ljava/lang/String;"); - Collection> references = index.getFieldReferences(source); + Collection> references = index.getReferenceIndex().getReferencesToField(source); assertThat(references, containsInAnyOrder( newFieldReferenceByMethod(source, "a", "", "(Ljava/lang/String;)V"), newFieldReferenceByMethod(source, "a", "a", "()Ljava/lang/String;") @@ -110,42 +126,33 @@ public class TestJarIndexLoneClass { @Test public void behaviorReferences() { - assertThat(index.getMethodsReferencing(newMethod("a", "a", "()Ljava/lang/String;")), is(empty())); - } - - @Test - public void innerClasses() { - assertThat(index.getInnerClasses(newClass("a")), is(empty())); - } - - @Test - public void outerClass() { - assertThat(index.getOuterClass(newClass("a")), is(nullValue())); + assertThat(index.getReferenceIndex().getReferencesToMethod(newMethod("a", "a", "()Ljava/lang/String;")), is(empty())); } @Test public void interfaces() { - assertThat(index.getInterfaces("a"), is(empty())); + assertThat(index.getInheritanceIndex().getParents(new ClassEntry("a")), is(empty())); } @Test public void implementingClasses() { - assertThat(index.getImplementingClasses("a"), is(empty())); + assertThat(index.getInheritanceIndex().getChildren(new ClassEntry("a")), is(empty())); } @Test public void isInterface() { - assertThat(index.isInterface("a"), is(false)); + assertThat(index.getInheritanceIndex().isParent(new ClassEntry("a")), is(false)); } @Test public void testContains() { - assertThat(index.containsObfClass(newClass("a")), is(true)); - assertThat(index.containsObfClass(newClass("b")), is(false)); - assertThat(index.containsObfField(newField("a", "a", "Ljava/lang/String;")), is(true)); - assertThat(index.containsObfField(newField("a", "b", "Ljava/lang/String;")), is(false)); - assertThat(index.containsObfField(newField("a", "a", "LFoo;")), is(false)); - assertThat(index.containsObfMethod(newMethod("a", "a", "()Ljava/lang/String;")), is(true)); - assertThat(index.containsObfMethod(newMethod("a", "b", "()Ljava/lang/String;")), is(false)); + EntryIndex entryIndex = index.getEntryIndex(); + assertThat(entryIndex.hasClass(newClass("a")), is(true)); + assertThat(entryIndex.hasClass(newClass("b")), is(false)); + assertThat(entryIndex.hasField(newField("a", "a", "Ljava/lang/String;")), is(true)); + assertThat(entryIndex.hasField(newField("a", "b", "Ljava/lang/String;")), is(false)); + assertThat(entryIndex.hasField(newField("a", "a", "LFoo;")), is(false)); + assertThat(entryIndex.hasMethod(newMethod("a", "a", "()Ljava/lang/String;")), is(true)); + assertThat(entryIndex.hasMethod(newMethod("a", "b", "()Ljava/lang/String;")), is(false)); } } -- cgit v1.2.3