From 959cb5fd4f9586ec3bd265b452fe25fe1db82e3f Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 13 Jan 2015 23:25:04 -0500 Subject: source format change don't hate me too much if you were planning a big merge. =P --- src/cuchaz/enigma/analysis/Access.java | 30 +- .../enigma/analysis/BehaviorReferenceTreeNode.java | 56 +- src/cuchaz/enigma/analysis/BridgeFixer.java | 51 +- .../analysis/ClassImplementationsTreeNode.java | 48 +- .../enigma/analysis/ClassInheritanceTreeNode.java | 58 +- src/cuchaz/enigma/analysis/EntryReference.java | 94 +-- src/cuchaz/enigma/analysis/EntryRenamer.java | 173 ++--- .../enigma/analysis/FieldReferenceTreeNode.java | 65 +- src/cuchaz/enigma/analysis/JarClassIterator.java | 107 ++- src/cuchaz/enigma/analysis/JarIndex.java | 820 +++++++++------------ .../analysis/MethodImplementationsTreeNode.java | 66 +- .../enigma/analysis/MethodInheritanceTreeNode.java | 80 +- src/cuchaz/enigma/analysis/ReferenceTreeNode.java | 3 +- src/cuchaz/enigma/analysis/SourceIndex.java | 144 ++-- .../analysis/SourceIndexBehaviorVisitor.java | 159 ++-- .../enigma/analysis/SourceIndexClassVisitor.java | 97 ++- src/cuchaz/enigma/analysis/SourceIndexVisitor.java | 370 ++++------ src/cuchaz/enigma/analysis/Token.java | 41 +- src/cuchaz/enigma/analysis/TranslationIndex.java | 93 +-- src/cuchaz/enigma/analysis/TreeDumpVisitor.java | 424 +++++------ 20 files changed, 1186 insertions(+), 1793 deletions(-) (limited to 'src/cuchaz/enigma/analysis') diff --git a/src/cuchaz/enigma/analysis/Access.java b/src/cuchaz/enigma/analysis/Access.java index e35bb21..8d3409a 100644 --- a/src/cuchaz/enigma/analysis/Access.java +++ b/src/cuchaz/enigma/analysis/Access.java @@ -15,37 +15,29 @@ import java.lang.reflect.Modifier; import javassist.CtBehavior; import javassist.CtField; -public enum Access -{ +public enum Access { + Public, Protected, Private; - public static Access get( CtBehavior behavior ) - { - return get( behavior.getModifiers() ); + public static Access get(CtBehavior behavior) { + return get(behavior.getModifiers()); } - public static Access get( CtField field ) - { - return get( field.getModifiers() ); + public static Access get(CtField field) { + return get(field.getModifiers()); } - public static Access get( int modifiers ) - { - if( Modifier.isPublic( modifiers ) ) - { + public static Access get(int modifiers) { + if (Modifier.isPublic(modifiers)) { return Public; - } - else if( Modifier.isProtected( modifiers ) ) - { + } else if (Modifier.isProtected(modifiers)) { return Protected; - } - else if( Modifier.isPrivate( modifiers ) ) - { + } else if (Modifier.isPrivate(modifiers)) { return Private; } // assume public by default return Public; } -} \ No newline at end of file +} diff --git a/src/cuchaz/enigma/analysis/BehaviorReferenceTreeNode.java b/src/cuchaz/enigma/analysis/BehaviorReferenceTreeNode.java index 20f1d47..9adac5e 100644 --- a/src/cuchaz/enigma/analysis/BehaviorReferenceTreeNode.java +++ b/src/cuchaz/enigma/analysis/BehaviorReferenceTreeNode.java @@ -21,8 +21,8 @@ import cuchaz.enigma.mapping.BehaviorEntry; import cuchaz.enigma.mapping.Entry; import cuchaz.enigma.mapping.Translator; -public class BehaviorReferenceTreeNode extends DefaultMutableTreeNode implements ReferenceTreeNode -{ +public class BehaviorReferenceTreeNode extends DefaultMutableTreeNode implements ReferenceTreeNode { + private static final long serialVersionUID = -3658163700783307520L; private Translator m_deobfuscatingTranslator; @@ -30,15 +30,13 @@ public class BehaviorReferenceTreeNode extends DefaultMutableTreeNode implements private EntryReference m_reference; private Access m_access; - public BehaviorReferenceTreeNode( Translator deobfuscatingTranslator, BehaviorEntry entry ) - { + public BehaviorReferenceTreeNode(Translator deobfuscatingTranslator, BehaviorEntry entry) { m_deobfuscatingTranslator = deobfuscatingTranslator; m_entry = entry; m_reference = null; } - public BehaviorReferenceTreeNode( Translator deobfuscatingTranslator, EntryReference reference, Access access ) - { + public BehaviorReferenceTreeNode(Translator deobfuscatingTranslator, EntryReference reference, Access access) { m_deobfuscatingTranslator = deobfuscatingTranslator; m_entry = reference.entry; m_reference = reference; @@ -46,60 +44,48 @@ public class BehaviorReferenceTreeNode extends DefaultMutableTreeNode implements } @Override - public BehaviorEntry getEntry( ) - { + public BehaviorEntry getEntry() { return m_entry; } @Override - public EntryReference getReference( ) - { + public EntryReference getReference() { return m_reference; } @Override - public String toString( ) - { - if( m_reference != null ) - { - return String.format( "%s (%s)", m_deobfuscatingTranslator.translateEntry( m_reference.context ), m_access ); + public String toString() { + if (m_reference != null) { + return String.format("%s (%s)", m_deobfuscatingTranslator.translateEntry(m_reference.context), m_access); } - return m_deobfuscatingTranslator.translateEntry( m_entry ).toString(); + return m_deobfuscatingTranslator.translateEntry(m_entry).toString(); } - public void load( JarIndex index, boolean recurse ) - { + public void load(JarIndex index, boolean recurse) { // get all the child nodes - for( EntryReference reference : index.getBehaviorReferences( m_entry ) ) - { - add( new BehaviorReferenceTreeNode( m_deobfuscatingTranslator, reference, index.getAccess( m_entry ) ) ); + for (EntryReference reference : index.getBehaviorReferences(m_entry)) { + add(new BehaviorReferenceTreeNode(m_deobfuscatingTranslator, reference, index.getAccess(m_entry))); } - if( recurse && children != null ) - { - for( Object child : children ) - { - if( child instanceof BehaviorReferenceTreeNode ) - { + if (recurse && children != null) { + for (Object child : children) { + if (child instanceof BehaviorReferenceTreeNode) { BehaviorReferenceTreeNode node = (BehaviorReferenceTreeNode)child; // don't recurse into ancestor Set ancestors = Sets.newHashSet(); TreeNode n = (TreeNode)node; - while( n.getParent() != null ) - { + while (n.getParent() != null) { n = n.getParent(); - if( n instanceof BehaviorReferenceTreeNode ) - { - ancestors.add( ((BehaviorReferenceTreeNode)n).getEntry() ); + if (n instanceof BehaviorReferenceTreeNode) { + ancestors.add( ((BehaviorReferenceTreeNode)n).getEntry()); } } - if( ancestors.contains( node.getEntry() ) ) - { + if (ancestors.contains(node.getEntry())) { continue; } - node.load( index, true ); + node.load(index, true); } } } diff --git a/src/cuchaz/enigma/analysis/BridgeFixer.java b/src/cuchaz/enigma/analysis/BridgeFixer.java index 112b864..ad23b00 100644 --- a/src/cuchaz/enigma/analysis/BridgeFixer.java +++ b/src/cuchaz/enigma/analysis/BridgeFixer.java @@ -20,61 +20,48 @@ import cuchaz.enigma.mapping.BehaviorEntryFactory; import cuchaz.enigma.mapping.ClassEntry; import cuchaz.enigma.mapping.MethodEntry; -public class BridgeFixer -{ +public class BridgeFixer { + private JarIndex m_index; - public BridgeFixer( JarIndex index ) - { + public BridgeFixer(JarIndex index) { m_index = index; } - - public void fixBridges( CtClass c ) - { + + public void fixBridges(CtClass c) { // rename declared methods - for( CtMethod method : c.getDeclaredMethods() ) - { + for (CtMethod method : c.getDeclaredMethods()) { // get the method entry MethodEntry methodEntry = new MethodEntry( - new ClassEntry( Descriptor.toJvmName( c.getName() ) ), + new ClassEntry(Descriptor.toJvmName(c.getName())), method.getName(), method.getSignature() ); - MethodEntry bridgeMethodEntry = m_index.getBridgeMethod( methodEntry ); - if( bridgeMethodEntry != null ) - { + MethodEntry bridgeMethodEntry = m_index.getBridgeMethod(methodEntry); + if (bridgeMethodEntry != null) { // fix this bridged method - method.setName( bridgeMethodEntry.getName() ); + method.setName(bridgeMethodEntry.getName()); } } // rename method references // translate all the field and method references in the code by editing the constant pool ConstPool constants = c.getClassFile().getConstPool(); - ConstPoolEditor editor = new ConstPoolEditor( constants ); - for( int i=1; i nodes = Lists.newArrayList(); - for( String implementingClassName : index.getImplementingClasses( m_entry.getClassName() ) ) - { - nodes.add( new ClassImplementationsTreeNode( m_deobfuscatingTranslator, new ClassEntry( implementingClassName ) ) ); + for (String implementingClassName : index.getImplementingClasses(m_entry.getClassName())) { + nodes.add(new ClassImplementationsTreeNode(m_deobfuscatingTranslator, new ClassEntry(implementingClassName))); } // add them to this node - for( ClassImplementationsTreeNode node : nodes ) - { - this.add( node ); + for (ClassImplementationsTreeNode node : nodes) { + this.add(node); } } - public static ClassImplementationsTreeNode findNode( ClassImplementationsTreeNode node, MethodEntry entry ) - { + public static ClassImplementationsTreeNode findNode(ClassImplementationsTreeNode node, MethodEntry entry) { // is this the node? - if( node.m_entry.equals( entry ) ) - { + if (node.m_entry.equals(entry)) { return node; } // recurse - for( int i=0; i nodes = Lists.newArrayList(); - for( String subclassName : ancestries.getSubclassNames( m_obfClassName ) ) - { - nodes.add( new ClassInheritanceTreeNode( m_deobfuscatingTranslator, subclassName ) ); + for (String subclassName : ancestries.getSubclassNames(m_obfClassName)) { + nodes.add(new ClassInheritanceTreeNode(m_deobfuscatingTranslator, subclassName)); } // add them to this node - for( ClassInheritanceTreeNode node : nodes ) - { - this.add( node ); + for (ClassInheritanceTreeNode node : nodes) { + this.add(node); } - if( recurse ) - { - for( ClassInheritanceTreeNode node : nodes ) - { - node.load( ancestries, true ); + if (recurse) { + for (ClassInheritanceTreeNode node : nodes) { + node.load(ancestries, true); } } } - - public static ClassInheritanceTreeNode findNode( ClassInheritanceTreeNode node, ClassEntry entry ) - { + + public static ClassInheritanceTreeNode findNode(ClassInheritanceTreeNode node, ClassEntry entry) { // is this the node? - if( node.getObfClassName().equals( entry.getName() ) ) - { + if (node.getObfClassName().equals(entry.getName())) { return node; } // recurse - for( int i=0; i -{ - private static final List ConstructorNonNames = Arrays.asList( "this", "super", "static" ); +public class EntryReference { + + private static final List ConstructorNonNames = Arrays.asList("this", "super", "static"); public E entry; public C context; private boolean m_isNamed; - public EntryReference( E entry, String sourceName ) - { - this( entry, sourceName, null ); + public EntryReference(E entry, String sourceName) { + this(entry, sourceName, null); } - public EntryReference( E entry, String sourceName, C context ) - { - if( entry == null ) - { - throw new IllegalArgumentException( "Entry cannot be null!" ); + public EntryReference(E entry, String sourceName, C context) { + if (entry == null) { + throw new IllegalArgumentException("Entry cannot be null!"); } this.entry = entry; this.context = context; m_isNamed = sourceName != null && sourceName.length() > 0; - if( entry instanceof ConstructorEntry && ConstructorNonNames.contains( sourceName ) ) - { + if (entry instanceof ConstructorEntry && ConstructorNonNames.contains(sourceName)) { m_isNamed = false; } } - public EntryReference( E entry, C context, EntryReference other ) - { + public EntryReference(E entry, C context, EntryReference other) { this.entry = entry; this.context = context; m_isNamed = other.m_isNamed; } - public ClassEntry getLocationClassEntry( ) - { - if( context != null ) - { + public ClassEntry getLocationClassEntry() { + if (context != null) { return context.getClassEntry(); } return entry.getClassEntry(); } - public boolean isNamed( ) - { + public boolean isNamed() { return m_isNamed; } - public Entry getNameableEntry( ) - { - if( entry instanceof ConstructorEntry ) - { + public Entry getNameableEntry() { + if (entry instanceof ConstructorEntry) { // renaming a constructor really means renaming the class return entry.getClassEntry(); } return entry; } - public String getNamableName( ) - { - if( getNameableEntry() instanceof ClassEntry ) - { + public String getNamableName() { + if (getNameableEntry() instanceof ClassEntry) { ClassEntry classEntry = (ClassEntry)getNameableEntry(); - if( classEntry.isInnerClass() ) - { + if (classEntry.isInnerClass()) { // make sure we only rename the inner class name return classEntry.getInnerClassName(); } @@ -95,55 +82,44 @@ public class EntryReference } @Override - public int hashCode( ) - { - if( context != null ) - { - return Util.combineHashesOrdered( entry.hashCode(), context.hashCode() ); + public int hashCode() { + if (context != null) { + return Util.combineHashesOrdered(entry.hashCode(), context.hashCode()); } return entry.hashCode(); } @Override - public boolean equals( Object other ) - { - if( other instanceof EntryReference ) - { - return equals( (EntryReference)other ); + public boolean equals(Object other) { + if (other instanceof EntryReference) { + return equals((EntryReference)other); } return false; } - public boolean equals( EntryReference other ) - { + public boolean equals(EntryReference other) { // check entry first - boolean isEntrySame = entry.equals( other.entry ); - if( !isEntrySame ) - { + boolean isEntrySame = entry.equals(other.entry); + if (!isEntrySame) { return false; } // check caller - if( context == null && other.context == null ) - { + if (context == null && other.context == null) { return true; - } - else if( context != null && other.context != null ) - { - return context.equals( other.context ); + } else if (context != null && other.context != null) { + return context.equals(other.context); } return false; } @Override - public String toString( ) - { + public String toString() { StringBuilder buf = new StringBuilder(); - buf.append( entry ); - if( context != null ) - { - buf.append( " called from " ); - buf.append( context ); + buf.append(entry); + if (context != null) { + buf.append(" called from "); + buf.append(context); } return buf.toString(); } diff --git a/src/cuchaz/enigma/analysis/EntryRenamer.java b/src/cuchaz/enigma/analysis/EntryRenamer.java index 2d59fe9..b54489c 100644 --- a/src/cuchaz/enigma/analysis/EntryRenamer.java +++ b/src/cuchaz/enigma/analysis/EntryRenamer.java @@ -26,100 +26,83 @@ import cuchaz.enigma.mapping.Entry; import cuchaz.enigma.mapping.FieldEntry; import cuchaz.enigma.mapping.MethodEntry; -public class EntryRenamer -{ - public static void renameClassesInSet( Map renames, Set set ) - { +public class EntryRenamer { + + public static void renameClassesInSet(Map renames, Set set) { List entries = Lists.newArrayList(); - for( T val : set ) - { - entries.add( renameClassesInThing( renames, val ) ); + for (T val : set) { + entries.add(renameClassesInThing(renames, val)); } set.clear(); - set.addAll( entries ); + set.addAll(entries); } - public static void renameClassesInMap( Map renames, Map map ) - { + public static void renameClassesInMap(Map renames, Map map) { // for each key/value pair... Set> entriesToAdd = Sets.newHashSet(); - for( Map.Entry entry : map.entrySet() ) - { - entriesToAdd.add( new AbstractMap.SimpleEntry( - renameClassesInThing( renames, entry.getKey() ), - renameClassesInThing( renames, entry.getValue() ) - ) ); + for (Map.Entry entry : map.entrySet()) { + entriesToAdd.add(new AbstractMap.SimpleEntry( + renameClassesInThing(renames, entry.getKey()), + renameClassesInThing(renames, entry.getValue()) + )); } map.clear(); - for( Map.Entry entry : entriesToAdd ) - { - map.put( entry.getKey(), entry.getValue() ); + for (Map.Entry entry : entriesToAdd) { + map.put(entry.getKey(), entry.getValue()); } } - public static void renameClassesInMultimap( Map renames, Multimap map ) - { + public static void renameClassesInMultimap(Map renames, Multimap map) { // for each key/value pair... Set> entriesToAdd = Sets.newHashSet(); - for( Map.Entry entry : map.entries() ) - { - entriesToAdd.add( new AbstractMap.SimpleEntry( - renameClassesInThing( renames, entry.getKey() ), - renameClassesInThing( renames, entry.getValue() ) - ) ); + for (Map.Entry entry : map.entries()) { + entriesToAdd.add(new AbstractMap.SimpleEntry( + renameClassesInThing(renames, entry.getKey()), + renameClassesInThing(renames, entry.getValue()) + )); } map.clear(); - for( Map.Entry entry : entriesToAdd ) - { - map.put( entry.getKey(), entry.getValue() ); + for (Map.Entry entry : entriesToAdd) { + map.put(entry.getKey(), entry.getValue()); } } - public static void renameMethodsInMultimap( Map renames, Multimap map ) - { + public static void renameMethodsInMultimap(Map renames, Multimap map) { // for each key/value pair... Set> entriesToAdd = Sets.newHashSet(); - for( Map.Entry entry : map.entries() ) - { - entriesToAdd.add( new AbstractMap.SimpleEntry( - renameMethodsInThing( renames, entry.getKey() ), - renameMethodsInThing( renames, entry.getValue() ) - ) ); + for (Map.Entry entry : map.entries()) { + entriesToAdd.add(new AbstractMap.SimpleEntry( + renameMethodsInThing(renames, entry.getKey()), + renameMethodsInThing(renames, entry.getValue()) + )); } map.clear(); - for( Map.Entry entry : entriesToAdd ) - { - map.put( entry.getKey(), entry.getValue() ); + for (Map.Entry entry : entriesToAdd) { + map.put(entry.getKey(), entry.getValue()); } } - public static void renameMethodsInMap( Map renames, Map map ) - { + public static void renameMethodsInMap(Map renames, Map map) { // for each key/value pair... Set> entriesToAdd = Sets.newHashSet(); - for( Map.Entry entry : map.entrySet() ) - { - entriesToAdd.add( new AbstractMap.SimpleEntry( - renameMethodsInThing( renames, entry.getKey() ), - renameMethodsInThing( renames, entry.getValue() ) - ) ); + for (Map.Entry entry : map.entrySet()) { + entriesToAdd.add(new AbstractMap.SimpleEntry( + renameMethodsInThing(renames, entry.getKey()), + renameMethodsInThing(renames, entry.getValue()) + )); } map.clear(); - for( Map.Entry entry : entriesToAdd ) - { - map.put( entry.getKey(), entry.getValue() ); + for (Map.Entry entry : entriesToAdd) { + map.put(entry.getKey(), entry.getValue()); } } - @SuppressWarnings( "unchecked" ) - public static T renameMethodsInThing( Map renames, T thing ) - { - if( thing instanceof MethodEntry ) - { + @SuppressWarnings("unchecked") + public static T renameMethodsInThing(Map renames, T thing) { + if (thing instanceof MethodEntry) { MethodEntry methodEntry = (MethodEntry)thing; - MethodEntry newMethodEntry = renames.get( methodEntry ); - if( newMethodEntry != null ) - { + MethodEntry newMethodEntry = renames.get(methodEntry); + if (newMethodEntry != null) { return (T)new MethodEntry( methodEntry.getClassEntry(), newMethodEntry.getName(), @@ -127,81 +110,59 @@ public class EntryRenamer ); } return thing; - } - else if( thing instanceof ArgumentEntry ) - { + } else if (thing instanceof ArgumentEntry) { ArgumentEntry argumentEntry = (ArgumentEntry)thing; return (T)new ArgumentEntry( - renameMethodsInThing( renames, argumentEntry.getBehaviorEntry() ), + renameMethodsInThing(renames, argumentEntry.getBehaviorEntry()), argumentEntry.getIndex(), argumentEntry.getName() ); - } - else if( thing instanceof EntryReference ) - { + } else if (thing instanceof EntryReference) { EntryReference reference = (EntryReference)thing; - reference.entry = renameMethodsInThing( renames, reference.entry ); - reference.context = renameMethodsInThing( renames, reference.context ); + reference.entry = renameMethodsInThing(renames, reference.entry); + reference.context = renameMethodsInThing(renames, reference.context); return thing; } return thing; } - - @SuppressWarnings( "unchecked" ) - public static T renameClassesInThing( Map renames, T thing ) - { - if( thing instanceof String ) - { + + @SuppressWarnings("unchecked") + public static T renameClassesInThing(Map renames, T thing) { + if (thing instanceof String) { String stringEntry = (String)thing; - if( renames.containsKey( stringEntry ) ) - { - return (T)renames.get( stringEntry ); + if (renames.containsKey(stringEntry)) { + return (T)renames.get(stringEntry); } - } - else if( thing instanceof ClassEntry ) - { + } else if (thing instanceof ClassEntry) { ClassEntry classEntry = (ClassEntry)thing; - return (T)new ClassEntry( renameClassesInThing( renames, classEntry.getClassName() ) ); - } - else if( thing instanceof FieldEntry ) - { + return (T)new ClassEntry(renameClassesInThing(renames, classEntry.getClassName())); + } else if (thing instanceof FieldEntry) { FieldEntry fieldEntry = (FieldEntry)thing; - return (T)new FieldEntry( - renameClassesInThing( renames, fieldEntry.getClassEntry() ), - fieldEntry.getName() - ); - } - else if( thing instanceof ConstructorEntry ) - { + return (T)new FieldEntry(renameClassesInThing(renames, fieldEntry.getClassEntry()), fieldEntry.getName()); + } else if (thing instanceof ConstructorEntry) { ConstructorEntry constructorEntry = (ConstructorEntry)thing; return (T)new ConstructorEntry( - renameClassesInThing( renames, constructorEntry.getClassEntry() ), + renameClassesInThing(renames, constructorEntry.getClassEntry()), constructorEntry.getSignature() ); - } - else if( thing instanceof MethodEntry ) - { + } else if (thing instanceof MethodEntry) { MethodEntry methodEntry = (MethodEntry)thing; return (T)new MethodEntry( - renameClassesInThing( renames, methodEntry.getClassEntry() ), + renameClassesInThing(renames, methodEntry.getClassEntry()), methodEntry.getName(), methodEntry.getSignature() ); - } - else if( thing instanceof ArgumentEntry ) - { + } else if (thing instanceof ArgumentEntry) { ArgumentEntry argumentEntry = (ArgumentEntry)thing; return (T)new ArgumentEntry( - renameClassesInThing( renames, argumentEntry.getBehaviorEntry() ), + renameClassesInThing(renames, argumentEntry.getBehaviorEntry()), argumentEntry.getIndex(), argumentEntry.getName() ); - } - else if( thing instanceof EntryReference ) - { + } else if (thing instanceof EntryReference) { EntryReference reference = (EntryReference)thing; - reference.entry = renameClassesInThing( renames, reference.entry ); - reference.context = renameClassesInThing( renames, reference.context ); + reference.entry = renameClassesInThing(renames, reference.entry); + reference.context = renameClassesInThing(renames, reference.context); return thing; } diff --git a/src/cuchaz/enigma/analysis/FieldReferenceTreeNode.java b/src/cuchaz/enigma/analysis/FieldReferenceTreeNode.java index 2652f64..2173eea 100644 --- a/src/cuchaz/enigma/analysis/FieldReferenceTreeNode.java +++ b/src/cuchaz/enigma/analysis/FieldReferenceTreeNode.java @@ -16,24 +16,22 @@ import cuchaz.enigma.mapping.BehaviorEntry; import cuchaz.enigma.mapping.FieldEntry; import cuchaz.enigma.mapping.Translator; -public class FieldReferenceTreeNode extends DefaultMutableTreeNode implements ReferenceTreeNode -{ +public class FieldReferenceTreeNode extends DefaultMutableTreeNode implements ReferenceTreeNode { + private static final long serialVersionUID = -7934108091928699835L; private Translator m_deobfuscatingTranslator; private FieldEntry m_entry; private EntryReference m_reference; private Access m_access; - - public FieldReferenceTreeNode( Translator deobfuscatingTranslator, FieldEntry entry ) - { + + public FieldReferenceTreeNode(Translator deobfuscatingTranslator, FieldEntry entry) { m_deobfuscatingTranslator = deobfuscatingTranslator; m_entry = entry; m_reference = null; } - private FieldReferenceTreeNode( Translator deobfuscatingTranslator, EntryReference reference, Access access ) - { + private FieldReferenceTreeNode(Translator deobfuscatingTranslator, EntryReference reference, Access access) { m_deobfuscatingTranslator = deobfuscatingTranslator; m_entry = reference.entry; m_reference = reference; @@ -41,56 +39,41 @@ public class FieldReferenceTreeNode extends DefaultMutableTreeNode implements Re } @Override - public FieldEntry getEntry( ) - { + public FieldEntry getEntry() { return m_entry; } @Override - public EntryReference getReference( ) - { + public EntryReference getReference() { return m_reference; } @Override - public String toString( ) - { - if( m_reference != null ) - { - return String.format( "%s (%s)", m_deobfuscatingTranslator.translateEntry( m_reference.context ), m_access ); + public String toString() { + if (m_reference != null) { + return String.format("%s (%s)", m_deobfuscatingTranslator.translateEntry(m_reference.context), m_access); } - return m_deobfuscatingTranslator.translateEntry( m_entry ).toString(); + return m_deobfuscatingTranslator.translateEntry(m_entry).toString(); } - public void load( JarIndex index, boolean recurse ) - { + public void load(JarIndex index, boolean recurse) { // get all the child nodes - if( m_reference == null ) - { - for( EntryReference reference : index.getFieldReferences( m_entry ) ) - { - add( new FieldReferenceTreeNode( m_deobfuscatingTranslator, reference, index.getAccess( m_entry ) ) ); + if (m_reference == null) { + for (EntryReference reference : index.getFieldReferences(m_entry)) { + add(new FieldReferenceTreeNode(m_deobfuscatingTranslator, reference, index.getAccess(m_entry))); } - } - else - { - for( EntryReference reference : index.getBehaviorReferences( m_reference.context ) ) - { - add( new BehaviorReferenceTreeNode( m_deobfuscatingTranslator, reference, index.getAccess( m_reference.context ) ) ); + } else { + for (EntryReference reference : index.getBehaviorReferences(m_reference.context)) { + add(new BehaviorReferenceTreeNode(m_deobfuscatingTranslator, reference, index.getAccess(m_reference.context))); } } - if( recurse && children != null ) - { - for( Object node : children ) - { - if( node instanceof BehaviorReferenceTreeNode ) - { - ((BehaviorReferenceTreeNode)node).load( index, true ); - } - else if( node instanceof FieldReferenceTreeNode ) - { - ((FieldReferenceTreeNode)node).load( index, true ); + if (recurse && children != null) { + for (Object node : children) { + if (node instanceof BehaviorReferenceTreeNode) { + ((BehaviorReferenceTreeNode)node).load(index, true); + } else if (node instanceof FieldReferenceTreeNode) { + ((FieldReferenceTreeNode)node).load(index, true); } } } diff --git a/src/cuchaz/enigma/analysis/JarClassIterator.java b/src/cuchaz/enigma/analysis/JarClassIterator.java index f65b8e7..8d9947c 100644 --- a/src/cuchaz/enigma/analysis/JarClassIterator.java +++ b/src/cuchaz/enigma/analysis/JarClassIterator.java @@ -30,132 +30,107 @@ import com.google.common.collect.Lists; import cuchaz.enigma.Constants; import cuchaz.enigma.mapping.ClassEntry; -public class JarClassIterator implements Iterator -{ +public class JarClassIterator implements Iterator { + private JarFile m_jar; private Iterator m_iter; - public JarClassIterator( JarFile jar ) - { + public JarClassIterator(JarFile jar) { m_jar = jar; // get the jar entries that correspond to classes List classEntries = Lists.newArrayList(); Enumeration entries = m_jar.entries(); - while( entries.hasMoreElements() ) - { + while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); // is this a class file? - if( entry.getName().endsWith( ".class" ) ) - { - classEntries.add( entry ); + if (entry.getName().endsWith(".class")) { + classEntries.add(entry); } } m_iter = classEntries.iterator(); } @Override - public boolean hasNext( ) - { + public boolean hasNext() { return m_iter.hasNext(); } - + @Override - public CtClass next( ) - { + public CtClass next() { JarEntry entry = m_iter.next(); - try - { - return getClass( m_jar, entry ); - } - catch( IOException | NotFoundException ex ) - { - throw new Error( "Unable to load class: " + entry.getName() ); + try { + return getClass(m_jar, entry); + } catch (IOException | NotFoundException ex) { + throw new Error("Unable to load class: " + entry.getName()); } } - + @Override - public void remove( ) - { + public void remove() { throw new UnsupportedOperationException(); } - public static List getClassEntries( JarFile jar ) - { + public static List getClassEntries(JarFile jar) { List classEntries = Lists.newArrayList(); Enumeration entries = jar.entries(); - while( entries.hasMoreElements() ) - { + while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); // is this a class file? - if( !entry.isDirectory() && entry.getName().endsWith( ".class" ) ) - { - classEntries.add( getClassEntry( entry ) ); + if (!entry.isDirectory() && entry.getName().endsWith(".class")) { + classEntries.add(getClassEntry(entry)); } } return classEntries; } - public static Iterable classes( final JarFile jar ) - { - return new Iterable( ) - { + public static Iterable classes(final JarFile jar) { + return new Iterable() { @Override - public Iterator iterator( ) - { - return new JarClassIterator( jar ); + public Iterator iterator() { + return new JarClassIterator(jar); } }; } - public static CtClass getClass( JarFile jar, ClassEntry classEntry ) - { - try - { - return getClass( jar, new JarEntry( classEntry.getName() + ".class" ) ); - } - catch( IOException | NotFoundException ex ) - { - throw new Error( "Unable to load class: " + classEntry.getName() ); + public static CtClass getClass(JarFile jar, ClassEntry classEntry) { + try { + return getClass(jar, new JarEntry(classEntry.getName() + ".class")); + } catch (IOException | NotFoundException ex) { + throw new Error("Unable to load class: " + classEntry.getName()); } } - private static CtClass getClass( JarFile jar, JarEntry entry ) - throws IOException, NotFoundException - { + private static CtClass getClass(JarFile jar, JarEntry entry) throws IOException, NotFoundException { // read the class into a buffer ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[Constants.KiB]; int totalNumBytesRead = 0; - InputStream in = jar.getInputStream( entry ); - while( in.available() > 0 ) - { - int numBytesRead = in.read( buf ); - if( numBytesRead < 0 ) - { + InputStream in = jar.getInputStream(entry); + while (in.available() > 0) { + int numBytesRead = in.read(buf); + if (numBytesRead < 0) { break; } - bos.write( buf, 0, numBytesRead ); + bos.write(buf, 0, numBytesRead); // sanity checking totalNumBytesRead += numBytesRead; - if( totalNumBytesRead > Constants.MiB ) - { - throw new Error( "Class file " + entry.getName() + " larger than 1 MiB! Something is wrong!" ); + if (totalNumBytesRead > Constants.MiB) { + throw new Error("Class file " + entry.getName() + " larger than 1 MiB! Something is wrong!"); } } // get a javassist handle for the class - String className = Descriptor.toJavaName( getClassEntry( entry ).getName() ); + String className = Descriptor.toJavaName(getClassEntry(entry).getName()); ClassPool classPool = new ClassPool(); - classPool.insertClassPath( new ByteArrayClassPath( className, bos.toByteArray() ) ); - return classPool.get( className ); + classPool.insertClassPath(new ByteArrayClassPath(className, bos.toByteArray())); + return classPool.get(className); } - private static ClassEntry getClassEntry( JarEntry entry ) - { - return new ClassEntry( entry.getName().substring( 0, entry.getName().length() - ".class".length() ) ); + private static ClassEntry getClassEntry(JarEntry entry) { + return new ClassEntry(entry.getName().substring(0, entry.getName().length() - ".class".length())); } } diff --git a/src/cuchaz/enigma/analysis/JarIndex.java b/src/cuchaz/enigma/analysis/JarIndex.java index 0954564..4b03a33 100644 --- a/src/cuchaz/enigma/analysis/JarIndex.java +++ b/src/cuchaz/enigma/analysis/JarIndex.java @@ -53,8 +53,8 @@ import cuchaz.enigma.mapping.MethodEntry; import cuchaz.enigma.mapping.SignatureUpdater; import cuchaz.enigma.mapping.Translator; -public class JarIndex -{ +public class JarIndex { + private Set m_obfClassEntries; private TranslationIndex m_translationIndex; private Multimap m_interfaces; @@ -68,8 +68,7 @@ public class JarIndex private Map m_anonymousClasses; private Map m_bridgeMethods; - public JarIndex( ) - { + public JarIndex() { m_obfClassEntries = Sets.newHashSet(); m_translationIndex = new TranslationIndex(); m_interfaces = HashMultimap.create(); @@ -84,192 +83,161 @@ public class JarIndex m_bridgeMethods = Maps.newHashMap(); } - public void indexJar( JarFile jar, boolean buildInnerClasses ) - { + public void indexJar(JarFile jar, boolean buildInnerClasses) { // step 1: read the class names - for( ClassEntry classEntry : JarClassIterator.getClassEntries( jar ) ) - { - if( classEntry.isInDefaultPackage() ) - { + for (ClassEntry classEntry : JarClassIterator.getClassEntries(jar)) { + if (classEntry.isInDefaultPackage()) { // move out of default package - classEntry = new ClassEntry( Constants.NonePackage + "/" + classEntry.getName() ); + classEntry = new ClassEntry(Constants.NonePackage + "/" + classEntry.getName()); } - m_obfClassEntries.add( classEntry ); + m_obfClassEntries.add(classEntry); } // step 2: index field/method/constructor access - for( CtClass c : JarClassIterator.classes( jar ) ) - { - ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage ); - ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) ); - for( CtField field : c.getDeclaredFields() ) - { - FieldEntry fieldEntry = new FieldEntry( classEntry, field.getName() ); - m_access.put( fieldEntry, Access.get( field ) ); + for (CtClass c : JarClassIterator.classes(jar)) { + ClassRenamer.moveAllClassesOutOfDefaultPackage(c, Constants.NonePackage); + ClassEntry classEntry = new ClassEntry(Descriptor.toJvmName(c.getName())); + for (CtField field : c.getDeclaredFields()) { + FieldEntry fieldEntry = new FieldEntry(classEntry, field.getName()); + m_access.put(fieldEntry, Access.get(field)); } - for( CtMethod method : c.getDeclaredMethods() ) - { - MethodEntry methodEntry = new MethodEntry( classEntry, method.getName(), method.getSignature() ); - m_access.put( methodEntry, Access.get( method ) ); + for (CtMethod method : c.getDeclaredMethods()) { + MethodEntry methodEntry = new MethodEntry(classEntry, method.getName(), method.getSignature()); + m_access.put(methodEntry, Access.get(method)); } - for( CtConstructor constructor : c.getDeclaredConstructors() ) - { - ConstructorEntry constructorEntry = new ConstructorEntry( classEntry, constructor.getSignature() ); - m_access.put( constructorEntry, Access.get( constructor ) ); + for (CtConstructor constructor : c.getDeclaredConstructors()) { + ConstructorEntry constructorEntry = new ConstructorEntry(classEntry, constructor.getSignature()); + m_access.put(constructorEntry, Access.get(constructor)); } } // step 3: index extends, implements, fields, and methods - for( CtClass c : JarClassIterator.classes( jar ) ) - { - ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage ); - String className = Descriptor.toJvmName( c.getName() ); - m_translationIndex.addSuperclass( className, Descriptor.toJvmName( c.getClassFile().getSuperclass() ) ); - for( String interfaceName : c.getClassFile().getInterfaces() ) - { - className = Descriptor.toJvmName( className ); - interfaceName = Descriptor.toJvmName( interfaceName ); - if( className.equals( interfaceName ) ) - { - throw new IllegalArgumentException( "Class cannot be its own interface! " + className ); + for (CtClass c : JarClassIterator.classes(jar)) { + ClassRenamer.moveAllClassesOutOfDefaultPackage(c, Constants.NonePackage); + String className = Descriptor.toJvmName(c.getName()); + m_translationIndex.addSuperclass(className, Descriptor.toJvmName(c.getClassFile().getSuperclass())); + for (String interfaceName : c.getClassFile().getInterfaces()) { + className = Descriptor.toJvmName(className); + interfaceName = Descriptor.toJvmName(interfaceName); + if (className.equals(interfaceName)) { + throw new IllegalArgumentException("Class cannot be its own interface! " + className); } - m_interfaces.put( className, interfaceName ); + m_interfaces.put(className, interfaceName); } - for( CtField field : c.getDeclaredFields() ) - { - indexField( field ); + for (CtField field : c.getDeclaredFields()) { + indexField(field); } - for( CtBehavior behavior : c.getDeclaredBehaviors() ) - { - indexBehavior( behavior ); + for (CtBehavior behavior : c.getDeclaredBehaviors()) { + indexBehavior(behavior); } } // step 4: index field, method, constructor references - for( CtClass c : JarClassIterator.classes( jar ) ) - { - ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage ); - for( CtBehavior behavior : c.getDeclaredBehaviors() ) - { - indexBehaviorReferences( behavior ); + for (CtClass c : JarClassIterator.classes(jar)) { + ClassRenamer.moveAllClassesOutOfDefaultPackage(c, Constants.NonePackage); + for (CtBehavior behavior : c.getDeclaredBehaviors()) { + indexBehaviorReferences(behavior); } } - if( buildInnerClasses ) - { + if (buildInnerClasses) { // step 5: index inner classes and anonymous classes - for( CtClass c : JarClassIterator.classes( jar ) ) - { - ClassRenamer.moveAllClassesOutOfDefaultPackage( c, Constants.NonePackage ); - String outerClassName = findOuterClass( c ); - if( outerClassName != null ) - { + for (CtClass c : JarClassIterator.classes(jar)) { + ClassRenamer.moveAllClassesOutOfDefaultPackage(c, Constants.NonePackage); + String outerClassName = findOuterClass(c); + if (outerClassName != null) { String innerClassName = c.getSimpleName(); - m_innerClasses.put( outerClassName, innerClassName ); - boolean innerWasAdded = m_outerClasses.put( innerClassName, outerClassName ) == null; - assert( innerWasAdded ); + m_innerClasses.put(outerClassName, innerClassName); + boolean innerWasAdded = m_outerClasses.put(innerClassName, outerClassName) == null; + assert (innerWasAdded); - BehaviorEntry enclosingBehavior = isAnonymousClass( c, outerClassName ); - if( enclosingBehavior != null ) - { - m_anonymousClasses.put( innerClassName, enclosingBehavior ); + BehaviorEntry enclosingBehavior = isAnonymousClass(c, outerClassName); + if (enclosingBehavior != null) { + m_anonymousClasses.put(innerClassName, enclosingBehavior); // DEBUG - //System.out.println( "ANONYMOUS: " + outerClassName + "$" + innerClassName ); - } - else - { + // System.out.println( "ANONYMOUS: " + outerClassName + "$" + innerClassName ); + } else { // DEBUG - //System.out.println( "INNER: " + outerClassName + "$" + innerClassName ); + // System.out.println( "INNER: " + outerClassName + "$" + innerClassName ); } } } // step 6: update other indices with inner class info Map renames = Maps.newHashMap(); - for( Map.Entry entry : m_outerClasses.entrySet() ) - { - renames.put( Constants.NonePackage + "/" + entry.getKey(), entry.getValue() + "$" + entry.getKey() ); + for (Map.Entry entry : m_outerClasses.entrySet()) { + renames.put(Constants.NonePackage + "/" + entry.getKey(), entry.getValue() + "$" + entry.getKey()); } - EntryRenamer.renameClassesInSet( renames, m_obfClassEntries ); - m_translationIndex.renameClasses( renames ); - EntryRenamer.renameClassesInMultimap( renames, m_interfaces ); - EntryRenamer.renameClassesInMultimap( renames, m_methodImplementations ); - EntryRenamer.renameClassesInMultimap( renames, m_behaviorReferences ); - EntryRenamer.renameClassesInMultimap( renames, m_fieldReferences ); - EntryRenamer.renameClassesInMap( renames, m_bridgeMethods ); - EntryRenamer.renameClassesInMap( renames, m_access ); + EntryRenamer.renameClassesInSet(renames, m_obfClassEntries); + m_translationIndex.renameClasses(renames); + EntryRenamer.renameClassesInMultimap(renames, m_interfaces); + EntryRenamer.renameClassesInMultimap(renames, m_methodImplementations); + EntryRenamer.renameClassesInMultimap(renames, m_behaviorReferences); + EntryRenamer.renameClassesInMultimap(renames, m_fieldReferences); + EntryRenamer.renameClassesInMap(renames, m_bridgeMethods); + EntryRenamer.renameClassesInMap(renames, m_access); } // step 6: update other indices with bridge method info - EntryRenamer.renameMethodsInMultimap( m_bridgeMethods, m_methodImplementations ); - EntryRenamer.renameMethodsInMultimap( m_bridgeMethods, m_behaviorReferences ); - EntryRenamer.renameMethodsInMultimap( m_bridgeMethods, m_fieldReferences ); - EntryRenamer.renameMethodsInMap( m_bridgeMethods, m_access ); + EntryRenamer.renameMethodsInMultimap(m_bridgeMethods, m_methodImplementations); + EntryRenamer.renameMethodsInMultimap(m_bridgeMethods, m_behaviorReferences); + EntryRenamer.renameMethodsInMultimap(m_bridgeMethods, m_fieldReferences); + EntryRenamer.renameMethodsInMap(m_bridgeMethods, m_access); } - private void indexField( CtField field ) - { + private void indexField(CtField field) { // get the field entry - String className = Descriptor.toJvmName( field.getDeclaringClass().getName() ); - FieldEntry fieldEntry = new FieldEntry( new ClassEntry( className ), field.getName() ); + String className = Descriptor.toJvmName(field.getDeclaringClass().getName()); + FieldEntry fieldEntry = new FieldEntry(new ClassEntry(className), field.getName()); - m_translationIndex.addField( className, field.getName() ); + m_translationIndex.addField(className, field.getName()); // is the field a class type? - if( field.getSignature().startsWith( "L" ) ) - { - ClassEntry fieldTypeEntry = new ClassEntry( field.getSignature().substring( 1, field.getSignature().length() - 1 ) ); - m_fieldClasses.put( fieldEntry, fieldTypeEntry ); + if (field.getSignature().startsWith("L")) { + ClassEntry fieldTypeEntry = new ClassEntry(field.getSignature().substring(1, field.getSignature().length() - 1)); + m_fieldClasses.put(fieldEntry, fieldTypeEntry); } } - - private void indexBehavior( CtBehavior behavior ) - { + + private void indexBehavior(CtBehavior behavior) { // get the behavior entry - final BehaviorEntry behaviorEntry = BehaviorEntryFactory.create( behavior ); - if( behaviorEntry instanceof MethodEntry ) - { + final BehaviorEntry behaviorEntry = BehaviorEntryFactory.create(behavior); + if (behaviorEntry instanceof MethodEntry) { MethodEntry methodEntry = (MethodEntry)behaviorEntry; // index implementation - m_methodImplementations.put( behaviorEntry.getClassName(), methodEntry ); + m_methodImplementations.put(behaviorEntry.getClassName(), methodEntry); // look for bridge methods - CtMethod bridgedMethod = getBridgedMethod( (CtMethod)behavior ); - if( bridgedMethod != null ) - { + CtMethod bridgedMethod = getBridgedMethod((CtMethod)behavior); + if (bridgedMethod != null) { MethodEntry bridgedMethodEntry = new MethodEntry( behaviorEntry.getClassEntry(), bridgedMethod.getName(), bridgedMethod.getSignature() ); - m_bridgeMethods.put( bridgedMethodEntry, methodEntry ); + m_bridgeMethods.put(bridgedMethodEntry, methodEntry); } } // looks like we don't care about constructors here } - private void indexBehaviorReferences( CtBehavior behavior ) - { + private void indexBehaviorReferences(CtBehavior behavior) { // index method calls - final BehaviorEntry behaviorEntry = BehaviorEntryFactory.create( behavior ); - try - { - behavior.instrument( new ExprEditor( ) - { + final BehaviorEntry behaviorEntry = BehaviorEntryFactory.create(behavior); + try { + behavior.instrument(new ExprEditor() { @Override - public void edit( MethodCall call ) - { - String className = Descriptor.toJvmName( call.getClassName() ); + public void edit(MethodCall call) { + String className = Descriptor.toJvmName(call.getClassName()); MethodEntry calledMethodEntry = new MethodEntry( - new ClassEntry( className ), + new ClassEntry(className), call.getMethodName(), call.getSignature() ); - ClassEntry resolvedClassEntry = resolveEntryClass( calledMethodEntry ); - if( resolvedClassEntry != null && !resolvedClassEntry.equals( calledMethodEntry.getClassEntry() ) ) - { + ClassEntry resolvedClassEntry = resolveEntryClass(calledMethodEntry); + if (resolvedClassEntry != null && !resolvedClassEntry.equals(calledMethodEntry.getClassEntry())) { calledMethodEntry = new MethodEntry( resolvedClassEntry, call.getMethodName(), @@ -281,39 +249,33 @@ public class JarIndex call.getMethodName(), behaviorEntry ); - m_behaviorReferences.put( calledMethodEntry, reference ); + m_behaviorReferences.put(calledMethodEntry, reference); } @Override - public void edit( FieldAccess call ) - { - String className = Descriptor.toJvmName( call.getClassName() ); + public void edit(FieldAccess call) { + String className = Descriptor.toJvmName(call.getClassName()); FieldEntry calledFieldEntry = new FieldEntry( - new ClassEntry( className ), + new ClassEntry(className), call.getFieldName() ); - ClassEntry resolvedClassEntry = resolveEntryClass( calledFieldEntry ); - if( resolvedClassEntry != null && !resolvedClassEntry.equals( calledFieldEntry.getClassEntry() ) ) - { - calledFieldEntry = new FieldEntry( - resolvedClassEntry, - call.getFieldName() - ); + ClassEntry resolvedClassEntry = resolveEntryClass(calledFieldEntry); + if (resolvedClassEntry != null && !resolvedClassEntry.equals(calledFieldEntry.getClassEntry())) { + calledFieldEntry = new FieldEntry(resolvedClassEntry, call.getFieldName()); } EntryReference reference = new EntryReference( calledFieldEntry, call.getFieldName(), behaviorEntry ); - m_fieldReferences.put( calledFieldEntry, reference ); + m_fieldReferences.put(calledFieldEntry, reference); } @Override - public void edit( ConstructorCall call ) - { - String className = Descriptor.toJvmName( call.getClassName() ); + public void edit(ConstructorCall call) { + String className = Descriptor.toJvmName(call.getClassName()); ConstructorEntry calledConstructorEntry = new ConstructorEntry( - new ClassEntry( className ), + new ClassEntry(className), call.getSignature() ); EntryReference reference = new EntryReference( @@ -321,15 +283,14 @@ public class JarIndex call.getMethodName(), behaviorEntry ); - m_behaviorReferences.put( calledConstructorEntry, reference ); + m_behaviorReferences.put(calledConstructorEntry, reference); } @Override - public void edit( NewExpr call ) - { - String className = Descriptor.toJvmName( call.getClassName() ); + public void edit(NewExpr call) { + String className = Descriptor.toJvmName(call.getClassName()); ConstructorEntry calledConstructorEntry = new ConstructorEntry( - new ClassEntry( className ), + new ClassEntry(className), call.getSignature() ); EntryReference reference = new EntryReference( @@ -337,173 +298,141 @@ public class JarIndex call.getClassName(), behaviorEntry ); - m_behaviorReferences.put( calledConstructorEntry, reference ); + m_behaviorReferences.put(calledConstructorEntry, reference); } - } ); - } - catch( CannotCompileException ex ) - { - throw new Error( ex ); + }); + } catch (CannotCompileException ex) { + throw new Error(ex); } } - public ClassEntry resolveEntryClass( Entry obfEntry ) - { + public ClassEntry resolveEntryClass(Entry obfEntry) { + // this entry could refer to a method on a class where the method is not actually implemented // travel up the inheritance tree to find the closest implementation - while( !containsObfEntry( obfEntry ) ) - { + while (!containsObfEntry(obfEntry)) { // is there a parent class? - String superclassName = m_translationIndex.getSuperclassName( obfEntry.getClassName() ); - if( superclassName == null ) - { + String superclassName = m_translationIndex.getSuperclassName(obfEntry.getClassName()); + if (superclassName == null) { // this is probably a method from a class in a library // we can't trace the implementation up any higher unless we index the library return null; } // move up to the parent class - obfEntry = obfEntry.cloneToNewClass( new ClassEntry( superclassName ) ); + obfEntry = obfEntry.cloneToNewClass(new ClassEntry(superclassName)); } return obfEntry.getClassEntry(); } - - private CtMethod getBridgedMethod( CtMethod method ) - { + + private CtMethod getBridgedMethod(CtMethod method) { + // bridge methods just call another method, cast it to the return type, and return the result // let's see if we can detect this scenario // skip non-synthetic methods - if( ( method.getModifiers() & AccessFlag.SYNTHETIC ) == 0 ) - { + if ( (method.getModifiers() & AccessFlag.SYNTHETIC) == 0) { return null; } - + // get all the called methods final List methodCalls = Lists.newArrayList(); - try - { - method.instrument( new ExprEditor( ) - { + try { + method.instrument(new ExprEditor() { @Override - public void edit( MethodCall call ) - { - methodCalls.add( call ); + public void edit(MethodCall call) { + methodCalls.add(call); } - } ); - } - catch( CannotCompileException ex ) - { + }); + } catch (CannotCompileException ex) { // this is stupid... we're not even compiling anything - throw new Error( ex ); + throw new Error(ex); } // is there just one? - if( methodCalls.size() != 1 ) - { + if (methodCalls.size() != 1) { return null; } - MethodCall call = methodCalls.get( 0 ); + MethodCall call = methodCalls.get(0); - try - { + try { // we have a bridge method! return call.getMethod(); - } - catch( NotFoundException ex ) - { + } catch (NotFoundException ex) { // can't find the type? not a bridge method return null; } } - private String findOuterClass( CtClass c ) - { + private String findOuterClass(CtClass c) { + // inner classes: - // have constructors that can (illegally) set synthetic fields - // the outer class is the only class that calls constructors + // have constructors that can (illegally) set synthetic fields + // the outer class is the only class that calls constructors // use the synthetic fields to find the synthetic constructors - for( CtConstructor constructor : c.getDeclaredConstructors() ) - { + for (CtConstructor constructor : c.getDeclaredConstructors()) { Set syntheticFieldTypes = Sets.newHashSet(); - if( !isIllegalConstructor( syntheticFieldTypes, constructor ) ) - { + if (!isIllegalConstructor(syntheticFieldTypes, constructor)) { continue; } - ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) ); - ConstructorEntry constructorEntry = new ConstructorEntry( classEntry, constructor.getMethodInfo().getDescriptor() ); + ClassEntry classEntry = new ClassEntry(Descriptor.toJvmName(c.getName())); + ConstructorEntry constructorEntry = new ConstructorEntry( + classEntry, + constructor.getMethodInfo().getDescriptor() + ); // gather the classes from the illegally-set synthetic fields Set illegallySetClasses = Sets.newHashSet(); - for( String type : syntheticFieldTypes ) - { - if( type.startsWith( "L" ) ) - { - ClassEntry outerClassEntry = new ClassEntry( type.substring( 1, type.length() - 1 ) ); - if( isSaneOuterClass( outerClassEntry, classEntry ) ) - { - illegallySetClasses.add( outerClassEntry ); + for (String type : syntheticFieldTypes) { + if (type.startsWith("L")) { + ClassEntry outerClassEntry = new ClassEntry(type.substring(1, type.length() - 1)); + if (isSaneOuterClass(outerClassEntry, classEntry)) { + illegallySetClasses.add(outerClassEntry); } } } // who calls this constructor? Set callerClasses = Sets.newHashSet(); - for( EntryReference reference : getBehaviorReferences( constructorEntry ) ) - { + for (EntryReference reference : getBehaviorReferences(constructorEntry)) { + // make sure it's not a call to super - if( reference.entry instanceof ConstructorEntry && reference.context instanceof ConstructorEntry ) - { + if (reference.entry instanceof ConstructorEntry && reference.context instanceof ConstructorEntry) { + // is the entry a superclass of the context? String calledClassName = reference.entry.getClassName(); - String callerSuperclassName = m_translationIndex.getSuperclassName( reference.context.getClassName() ); - if( callerSuperclassName != null && callerSuperclassName.equals( calledClassName ) ) - { + String callerSuperclassName = m_translationIndex.getSuperclassName(reference.context.getClassName()); + if (callerSuperclassName != null && callerSuperclassName.equals(calledClassName)) { // it's a super call, skip continue; } } - if( isSaneOuterClass( reference.context.getClassEntry(), classEntry ) ) - { - callerClasses.add( reference.context.getClassEntry() ); + if (isSaneOuterClass(reference.context.getClassEntry(), classEntry)) { + callerClasses.add(reference.context.getClassEntry()); } } // do we have an answer yet? - if( callerClasses.isEmpty() ) - { - if( illegallySetClasses.size() == 1 ) - { + if (callerClasses.isEmpty()) { + if (illegallySetClasses.size() == 1) { return illegallySetClasses.iterator().next().getName(); + } else { + System.out.println(String.format("WARNING: Unable to find outer class for %s. No caller and no illegally set field classes.", classEntry)); } - else - { - System.out.println( String.format( "WARNING: Unable to find outer class for %s. No caller and no illegally set field classes.", classEntry ) ); - } - } - else - { - if( callerClasses.size() == 1 ) - { + } else { + if (callerClasses.size() == 1) { return callerClasses.iterator().next().getName(); - } - else - { + } else { // multiple callers, do the illegally set classes narrow it down? - Set intersection = Sets.newHashSet( callerClasses ); - intersection.retainAll( illegallySetClasses ); - if( intersection.size() == 1 ) - { + Set intersection = Sets.newHashSet(callerClasses); + intersection.retainAll(illegallySetClasses); + if (intersection.size() == 1) { return intersection.iterator().next().getName(); - } - else - { - System.out.println( String.format( "WARNING: Unable to choose outer class for %s among options: %s", - classEntry, callerClasses - ) ); + } else { + System.out.println(String.format("WARNING: Unable to choose outer class for %s among options: %s", classEntry, callerClasses)); } } } @@ -512,99 +441,82 @@ public class JarIndex return null; } - private boolean isSaneOuterClass( ClassEntry outerClassEntry, ClassEntry innerClassEntry ) - { + private boolean isSaneOuterClass(ClassEntry outerClassEntry, ClassEntry innerClassEntry) { + // clearly this would be silly - if( outerClassEntry.equals( innerClassEntry ) ) - { + if (outerClassEntry.equals(innerClassEntry)) { return false; } // is the outer class in the jar? - if( !m_obfClassEntries.contains( outerClassEntry ) ) - { + if (!m_obfClassEntries.contains(outerClassEntry)) { return false; } return true; } - - @SuppressWarnings( "unchecked" ) - private boolean isIllegalConstructor( Set syntheticFieldTypes, CtConstructor constructor ) - { + + @SuppressWarnings("unchecked") + private boolean isIllegalConstructor(Set syntheticFieldTypes, CtConstructor constructor) { + // illegal constructors only set synthetic member fields, then call super() String className = constructor.getDeclaringClass().getName(); // collect all the field accesses, constructor calls, and method calls final List illegalFieldWrites = Lists.newArrayList(); final List constructorCalls = Lists.newArrayList(); - try - { - constructor.instrument( new ExprEditor( ) - { + try { + constructor.instrument(new ExprEditor() { @Override - public void edit( FieldAccess fieldAccess ) - { - if( fieldAccess.isWriter() && constructorCalls.isEmpty() ) - { - illegalFieldWrites.add( fieldAccess ); + public void edit(FieldAccess fieldAccess) { + if (fieldAccess.isWriter() && constructorCalls.isEmpty()) { + illegalFieldWrites.add(fieldAccess); } } @Override - public void edit( ConstructorCall constructorCall ) - { - constructorCalls.add( constructorCall ); + public void edit(ConstructorCall constructorCall) { + constructorCalls.add(constructorCall); } - } ); - } - catch( CannotCompileException ex ) - { + }); + } catch (CannotCompileException ex) { // we're not compiling anything... this is stupid - throw new Error( ex ); + throw new Error(ex); } // are there any illegal field writes? - if( illegalFieldWrites.isEmpty() ) - { + if (illegalFieldWrites.isEmpty()) { return false; } // are all the writes to synthetic fields? - for( FieldAccess fieldWrite : illegalFieldWrites ) - { + for (FieldAccess fieldWrite : illegalFieldWrites) { + // all illegal writes have to be to the local class - if( !fieldWrite.getClassName().equals( className ) ) - { - System.err.println( String.format( "WARNING: illegal write to non-member field %s.%s", fieldWrite.getClassName(), fieldWrite.getFieldName() ) ); + if (!fieldWrite.getClassName().equals(className)) { + System.err.println(String.format("WARNING: illegal write to non-member field %s.%s", fieldWrite.getClassName(), fieldWrite.getFieldName())); return false; } // find the field FieldInfo fieldInfo = null; - for( FieldInfo info : (List)constructor.getDeclaringClass().getClassFile().getFields() ) - { - if( info.getName().equals( fieldWrite.getFieldName() ) && info.getDescriptor().equals( fieldWrite.getSignature() ) ) - { + for (FieldInfo info : (List)constructor.getDeclaringClass().getClassFile().getFields()) { + if (info.getName().equals(fieldWrite.getFieldName()) && info.getDescriptor().equals(fieldWrite.getSignature())) { fieldInfo = info; break; } } - if( fieldInfo == null ) - { + if (fieldInfo == null) { // field is in a superclass or something, can't be a local synthetic member return false; } // is this field synthetic? boolean isSynthetic = (fieldInfo.getAccessFlags() & AccessFlag.SYNTHETIC) != 0; - if( isSynthetic ) - { - syntheticFieldTypes.add( fieldInfo.getDescriptor() ); - } - else - { - System.err.println( String.format( "WARNING: illegal write to non synthetic field %s %s.%s", fieldInfo.getDescriptor(), className, fieldInfo.getName() ) ); + if (isSynthetic) { + syntheticFieldTypes.add(fieldInfo.getDescriptor()); + } else { + System.err.println(String.format("WARNING: illegal write to non synthetic field %s %s.%s", fieldInfo.getDescriptor(), className, fieldInfo.getName())); return false; } } @@ -612,56 +524,51 @@ public class JarIndex // we passed all the tests! return true; } - - private BehaviorEntry isAnonymousClass( CtClass c, String outerClassName ) - { - ClassEntry innerClassEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) ); + + private BehaviorEntry isAnonymousClass(CtClass c, String outerClassName) { + + ClassEntry innerClassEntry = new ClassEntry(Descriptor.toJvmName(c.getName())); // anonymous classes: - // can't be abstract - // have only one constructor - // it's called exactly once by the outer class - // the type the instance is assigned to can't be this type + // can't be abstract + // have only one constructor + // it's called exactly once by the outer class + // the type the instance is assigned to can't be this type // is abstract? - if( Modifier.isAbstract( c.getModifiers() ) ) - { + if (Modifier.isAbstract(c.getModifiers())) { return null; } // is there exactly one constructor? - if( c.getDeclaredConstructors().length != 1 ) - { + if (c.getDeclaredConstructors().length != 1) { return null; } CtConstructor constructor = c.getDeclaredConstructors()[0]; // is this constructor called exactly once? - ConstructorEntry constructorEntry = new ConstructorEntry( innerClassEntry, constructor.getMethodInfo().getDescriptor() ); - Collection> references = getBehaviorReferences( constructorEntry ); - if( references.size() != 1 ) - { + ConstructorEntry constructorEntry = new ConstructorEntry( + innerClassEntry, + constructor.getMethodInfo().getDescriptor() + ); + Collection> references = getBehaviorReferences(constructorEntry); + if (references.size() != 1) { return null; } // does the caller use this type? BehaviorEntry caller = references.iterator().next().context; - for( FieldEntry fieldEntry : getReferencedFields( caller ) ) - { - ClassEntry fieldClass = getFieldClass( fieldEntry ); - if( fieldClass != null && fieldClass.equals( innerClassEntry ) ) - { + for (FieldEntry fieldEntry : getReferencedFields(caller)) { + ClassEntry fieldClass = getFieldClass(fieldEntry); + if (fieldClass != null && fieldClass.equals(innerClassEntry)) { // caller references this type, so it can't be anonymous return null; } } - for( BehaviorEntry behaviorEntry : getReferencedBehaviors( caller ) ) - { + for (BehaviorEntry behaviorEntry : getReferencedBehaviors(caller)) { // get the class types from the signature - for( String className : SignatureUpdater.getClasses( behaviorEntry.getSignature() ) ) - { - if( className.equals( innerClassEntry.getName() ) ) - { + for (String className : SignatureUpdater.getClasses(behaviorEntry.getSignature())) { + if (className.equals(innerClassEntry.getName())) { // caller references this type, so it can't be anonymous return null; } @@ -670,330 +577,275 @@ public class JarIndex return caller; } - - public Set getObfClassEntries( ) - { + + public Set getObfClassEntries() { return m_obfClassEntries; } - public TranslationIndex getTranslationIndex( ) - { + public TranslationIndex getTranslationIndex() { return m_translationIndex; } - public Access getAccess( Entry entry ) - { - return m_access.get( entry ); + public Access getAccess(Entry entry) { + return m_access.get(entry); } - public ClassEntry getFieldClass( FieldEntry fieldEntry ) - { - return m_fieldClasses.get( fieldEntry ); + public ClassEntry getFieldClass(FieldEntry fieldEntry) { + return m_fieldClasses.get(fieldEntry); } - public ClassInheritanceTreeNode getClassInheritance( Translator deobfuscatingTranslator, ClassEntry obfClassEntry ) - { + public ClassInheritanceTreeNode getClassInheritance(Translator deobfuscatingTranslator, ClassEntry obfClassEntry) { + // get the root node List ancestry = Lists.newArrayList(); - ancestry.add( obfClassEntry.getName() ); - ancestry.addAll( m_translationIndex.getAncestry( obfClassEntry.getName() ) ); - ClassInheritanceTreeNode rootNode = new ClassInheritanceTreeNode( deobfuscatingTranslator, ancestry.get( ancestry.size() - 1 ) ); + ancestry.add(obfClassEntry.getName()); + ancestry.addAll(m_translationIndex.getAncestry(obfClassEntry.getName())); + ClassInheritanceTreeNode rootNode = new ClassInheritanceTreeNode( + deobfuscatingTranslator, + ancestry.get(ancestry.size() - 1) + ); // expand all children recursively - rootNode.load( m_translationIndex, true ); + rootNode.load(m_translationIndex, true); return rootNode; } - public ClassImplementationsTreeNode getClassImplementations( Translator deobfuscatingTranslator, ClassEntry obfClassEntry ) - { + public ClassImplementationsTreeNode getClassImplementations(Translator deobfuscatingTranslator, ClassEntry obfClassEntry) { + // is this even an interface? - if( isInterface( obfClassEntry.getClassName() ) ) - { - ClassImplementationsTreeNode node = new ClassImplementationsTreeNode( deobfuscatingTranslator, obfClassEntry ); - node.load( this ); + if (isInterface(obfClassEntry.getClassName())) { + ClassImplementationsTreeNode node = new ClassImplementationsTreeNode(deobfuscatingTranslator, obfClassEntry); + node.load(this); return node; } return null; } - public MethodInheritanceTreeNode getMethodInheritance( Translator deobfuscatingTranslator, MethodEntry obfMethodEntry ) - { + public MethodInheritanceTreeNode getMethodInheritance(Translator deobfuscatingTranslator, MethodEntry obfMethodEntry) { + // travel to the ancestor implementation String baseImplementationClassName = obfMethodEntry.getClassName(); - for( String ancestorClassName : m_translationIndex.getAncestry( obfMethodEntry.getClassName() ) ) - { + for (String ancestorClassName : m_translationIndex.getAncestry(obfMethodEntry.getClassName())) { MethodEntry ancestorMethodEntry = new MethodEntry( - new ClassEntry( ancestorClassName ), + new ClassEntry(ancestorClassName), obfMethodEntry.getName(), obfMethodEntry.getSignature() ); - if( containsObfBehavior( ancestorMethodEntry ) ) - { + if (containsObfBehavior(ancestorMethodEntry)) { baseImplementationClassName = ancestorClassName; } } // make a root node at the base MethodEntry methodEntry = new MethodEntry( - new ClassEntry( baseImplementationClassName ), + new ClassEntry(baseImplementationClassName), obfMethodEntry.getName(), obfMethodEntry.getSignature() ); MethodInheritanceTreeNode rootNode = new MethodInheritanceTreeNode( deobfuscatingTranslator, methodEntry, - containsObfBehavior( methodEntry ) + containsObfBehavior(methodEntry) ); // expand the full tree - rootNode.load( this, true ); + rootNode.load(this, true); return rootNode; } - public MethodImplementationsTreeNode getMethodImplementations( Translator deobfuscatingTranslator, MethodEntry obfMethodEntry ) - { + public MethodImplementationsTreeNode getMethodImplementations(Translator deobfuscatingTranslator, MethodEntry obfMethodEntry) { + MethodEntry interfaceMethodEntry; // is this method on an interface? - if( isInterface( obfMethodEntry.getClassName() ) ) - { + if (isInterface(obfMethodEntry.getClassName())) { interfaceMethodEntry = obfMethodEntry; - } - else - { + } else { // get the interface class List methodInterfaces = Lists.newArrayList(); - for( String interfaceName : getInterfaces( obfMethodEntry.getClassName() ) ) - { + for (String interfaceName : getInterfaces(obfMethodEntry.getClassName())) { // is this method defined in this interface? MethodEntry methodInterface = new MethodEntry( - new ClassEntry( interfaceName ), + new ClassEntry(interfaceName), obfMethodEntry.getName(), obfMethodEntry.getSignature() ); - if( containsObfBehavior( methodInterface ) ) - { - methodInterfaces.add( methodInterface ); + if (containsObfBehavior(methodInterface)) { + methodInterfaces.add(methodInterface); } } - if( methodInterfaces.isEmpty() ) - { + if (methodInterfaces.isEmpty()) { return null; } - if( methodInterfaces.size() > 1 ) - { - throw new Error( "Too many interfaces define this method! This is not yet supported by Enigma!" ); + if (methodInterfaces.size() > 1) { + throw new Error("Too many interfaces define this method! This is not yet supported by Enigma!"); } - interfaceMethodEntry = methodInterfaces.get( 0 ); + interfaceMethodEntry = methodInterfaces.get(0); } - MethodImplementationsTreeNode rootNode = new MethodImplementationsTreeNode( deobfuscatingTranslator, interfaceMethodEntry ); - rootNode.load( this ); + MethodImplementationsTreeNode rootNode = new MethodImplementationsTreeNode(deobfuscatingTranslator, interfaceMethodEntry); + rootNode.load(this); return rootNode; } - public Set getRelatedMethodImplementations( MethodEntry obfMethodEntry ) - { + public Set getRelatedMethodImplementations(MethodEntry obfMethodEntry) { Set methodEntries = Sets.newHashSet(); - getRelatedMethodImplementations( methodEntries, getMethodInheritance( null, obfMethodEntry ) ); + getRelatedMethodImplementations(methodEntries, getMethodInheritance(null, obfMethodEntry)); return methodEntries; } - private void getRelatedMethodImplementations( Set methodEntries, MethodInheritanceTreeNode node ) - { + private void getRelatedMethodImplementations(Set methodEntries, MethodInheritanceTreeNode node) { MethodEntry methodEntry = node.getMethodEntry(); - if( containsObfBehavior( methodEntry ) ) - { + if (containsObfBehavior(methodEntry)) { // collect the entry - methodEntries.add( methodEntry ); + methodEntries.add(methodEntry); } // look at interface methods too - MethodImplementationsTreeNode implementations = getMethodImplementations( null, methodEntry ); - if( implementations != null ) - { - getRelatedMethodImplementations( methodEntries, implementations ); + MethodImplementationsTreeNode implementations = getMethodImplementations(null, methodEntry); + if (implementations != null) { + getRelatedMethodImplementations(methodEntries, implementations); } // recurse - for( int i=0; i methodEntries, MethodImplementationsTreeNode node ) - { + private void getRelatedMethodImplementations(Set methodEntries, MethodImplementationsTreeNode node) { MethodEntry methodEntry = node.getMethodEntry(); - if( containsObfBehavior( methodEntry ) ) - { + if (containsObfBehavior(methodEntry)) { // collect the entry - methodEntries.add( methodEntry ); + methodEntries.add(methodEntry); } // recurse - for( int i=0; i> getFieldReferences( FieldEntry fieldEntry ) - { - return m_fieldReferences.get( fieldEntry ); + + public Collection> getFieldReferences(FieldEntry fieldEntry) { + return m_fieldReferences.get(fieldEntry); } - public Collection getReferencedFields( BehaviorEntry behaviorEntry ) - { + public Collection getReferencedFields(BehaviorEntry behaviorEntry) { // linear search is fast enough for now Set fieldEntries = Sets.newHashSet(); - for( EntryReference reference : m_fieldReferences.values() ) - { - if( reference.context == behaviorEntry ) - { - fieldEntries.add( reference.entry ); + for (EntryReference reference : m_fieldReferences.values()) { + if (reference.context == behaviorEntry) { + fieldEntries.add(reference.entry); } } return fieldEntries; } - public Collection> getBehaviorReferences( BehaviorEntry behaviorEntry ) - { - return m_behaviorReferences.get( behaviorEntry ); + public Collection> getBehaviorReferences(BehaviorEntry behaviorEntry) { + return m_behaviorReferences.get(behaviorEntry); } - - public Collection getReferencedBehaviors( BehaviorEntry behaviorEntry ) - { + + public Collection getReferencedBehaviors(BehaviorEntry behaviorEntry) { // linear search is fast enough for now Set behaviorEntries = Sets.newHashSet(); - for( EntryReference reference : m_behaviorReferences.values() ) - { - if( reference.context == behaviorEntry ) - { - behaviorEntries.add( reference.entry ); + for (EntryReference reference : m_behaviorReferences.values()) { + if (reference.context == behaviorEntry) { + behaviorEntries.add(reference.entry); } } return behaviorEntries; } - public Collection getInnerClasses( String obfOuterClassName ) - { - return m_innerClasses.get( obfOuterClassName ); + public Collection getInnerClasses(String obfOuterClassName) { + return m_innerClasses.get(obfOuterClassName); } - public String getOuterClass( String obfInnerClassName ) - { + public String getOuterClass(String obfInnerClassName) { // make sure we use the right name - if( new ClassEntry( obfInnerClassName ).getPackageName() != null ) - { - throw new IllegalArgumentException( "Don't reference obfuscated inner classes using packages: " + obfInnerClassName ); + if (new ClassEntry(obfInnerClassName).getPackageName() != null) { + throw new IllegalArgumentException("Don't reference obfuscated inner classes using packages: " + obfInnerClassName); } - return m_outerClasses.get( obfInnerClassName ); + return m_outerClasses.get(obfInnerClassName); } - public boolean isAnonymousClass( String obfInnerClassName ) - { - return m_anonymousClasses.containsKey( obfInnerClassName ); + public boolean isAnonymousClass(String obfInnerClassName) { + return m_anonymousClasses.containsKey(obfInnerClassName); } - public BehaviorEntry getAnonymousClassCaller( String obfInnerClassName ) - { - return m_anonymousClasses.get( obfInnerClassName ); + public BehaviorEntry getAnonymousClassCaller(String obfInnerClassName) { + return m_anonymousClasses.get(obfInnerClassName); } - public Set getInterfaces( String className ) - { + public Set getInterfaces(String className) { Set interfaceNames = new HashSet(); - interfaceNames.addAll( m_interfaces.get( className ) ); - for( String ancestor : m_translationIndex.getAncestry( className ) ) - { - interfaceNames.addAll( m_interfaces.get( ancestor ) ); + interfaceNames.addAll(m_interfaces.get(className)); + for (String ancestor : m_translationIndex.getAncestry(className)) { + interfaceNames.addAll(m_interfaces.get(ancestor)); } return interfaceNames; } - public Set getImplementingClasses( String targetInterfaceName ) - { + public Set getImplementingClasses(String targetInterfaceName) { // linear search is fast enough for now Set classNames = Sets.newHashSet(); - for( Map.Entry entry : m_interfaces.entries() ) - { + for (Map.Entry entry : m_interfaces.entries()) { String className = entry.getKey(); String interfaceName = entry.getValue(); - if( interfaceName.equals( targetInterfaceName ) ) - { - classNames.add( className ); - m_translationIndex.getSubclassNamesRecursively( classNames, className ); + if (interfaceName.equals(targetInterfaceName)) { + classNames.add(className); + m_translationIndex.getSubclassNamesRecursively(classNames, className); } } return classNames; } - public boolean isInterface( String className ) - { - return m_interfaces.containsValue( className ); + public boolean isInterface(String className) { + return m_interfaces.containsValue(className); } - public MethodEntry getBridgeMethod( MethodEntry methodEntry ) - { - return m_bridgeMethods.get( methodEntry ); + public MethodEntry getBridgeMethod(MethodEntry methodEntry) { + return m_bridgeMethods.get(methodEntry); } - public boolean containsObfClass( ClassEntry obfClassEntry ) - { - return m_obfClassEntries.contains( obfClassEntry ); + public boolean containsObfClass(ClassEntry obfClassEntry) { + return m_obfClassEntries.contains(obfClassEntry); } - - public boolean containsObfField( FieldEntry obfFieldEntry ) - { - return m_access.containsKey( obfFieldEntry ); + + public boolean containsObfField(FieldEntry obfFieldEntry) { + return m_access.containsKey(obfFieldEntry); } - - public boolean containsObfBehavior( BehaviorEntry obfBehaviorEntry ) - { - return m_access.containsKey( obfBehaviorEntry ); + + public boolean containsObfBehavior(BehaviorEntry obfBehaviorEntry) { + return m_access.containsKey(obfBehaviorEntry); } - public boolean containsObfArgument( ArgumentEntry obfArgumentEntry ) - { + public boolean containsObfArgument(ArgumentEntry obfArgumentEntry) { // check the behavior - if( !containsObfBehavior( obfArgumentEntry.getBehaviorEntry() ) ) - { + if (!containsObfBehavior(obfArgumentEntry.getBehaviorEntry())) { return false; } // check the argument - if( obfArgumentEntry.getIndex() >= Descriptor.numOfParameters( obfArgumentEntry.getBehaviorEntry().getSignature() ) ) - { + if (obfArgumentEntry.getIndex() >= Descriptor.numOfParameters(obfArgumentEntry.getBehaviorEntry().getSignature())) { return false; } return true; } - public boolean containsObfEntry( Entry obfEntry ) - { - if( obfEntry instanceof ClassEntry ) - { - return containsObfClass( (ClassEntry)obfEntry ); - } - else if( obfEntry instanceof FieldEntry ) - { - return containsObfField( (FieldEntry)obfEntry ); - } - else if( obfEntry instanceof BehaviorEntry ) - { - return containsObfBehavior( (BehaviorEntry)obfEntry ); - } - else if( obfEntry instanceof ArgumentEntry ) - { - return containsObfArgument( (ArgumentEntry)obfEntry ); - } - else - { - throw new Error( "Entry type not supported: " + obfEntry.getClass().getName() ); + public boolean containsObfEntry(Entry obfEntry) { + if (obfEntry instanceof ClassEntry) { + return containsObfClass((ClassEntry)obfEntry); + } else if (obfEntry instanceof FieldEntry) { + return containsObfField((FieldEntry)obfEntry); + } else if (obfEntry instanceof BehaviorEntry) { + return containsObfBehavior((BehaviorEntry)obfEntry); + } else if (obfEntry instanceof ArgumentEntry) { + return containsObfArgument((ArgumentEntry)obfEntry); + } else { + throw new Error("Entry type not supported: " + obfEntry.getClass().getName()); } } } diff --git a/src/cuchaz/enigma/analysis/MethodImplementationsTreeNode.java b/src/cuchaz/enigma/analysis/MethodImplementationsTreeNode.java index a050282..1009226 100644 --- a/src/cuchaz/enigma/analysis/MethodImplementationsTreeNode.java +++ b/src/cuchaz/enigma/analysis/MethodImplementationsTreeNode.java @@ -20,94 +20,78 @@ import cuchaz.enigma.mapping.ClassEntry; import cuchaz.enigma.mapping.MethodEntry; import cuchaz.enigma.mapping.Translator; -public class MethodImplementationsTreeNode extends DefaultMutableTreeNode -{ +public class MethodImplementationsTreeNode extends DefaultMutableTreeNode { + private static final long serialVersionUID = 3781080657461899915L; private Translator m_deobfuscatingTranslator; private MethodEntry m_entry; - public MethodImplementationsTreeNode( Translator deobfuscatingTranslator, MethodEntry entry ) - { - if( entry == null ) - { - throw new IllegalArgumentException( "entry cannot be null!" ); + public MethodImplementationsTreeNode(Translator deobfuscatingTranslator, MethodEntry entry) { + if (entry == null) { + throw new IllegalArgumentException("entry cannot be null!"); } m_deobfuscatingTranslator = deobfuscatingTranslator; m_entry = entry; } - public MethodEntry getMethodEntry( ) - { + public MethodEntry getMethodEntry() { return m_entry; } - public String getDeobfClassName( ) - { - return m_deobfuscatingTranslator.translateClass( m_entry.getClassName() ); + public String getDeobfClassName() { + return m_deobfuscatingTranslator.translateClass(m_entry.getClassName()); } - public String getDeobfMethodName( ) - { - return m_deobfuscatingTranslator.translate( m_entry ); + public String getDeobfMethodName() { + return m_deobfuscatingTranslator.translate(m_entry); } @Override - public String toString( ) - { + public String toString() { String className = getDeobfClassName(); - if( className == null ) - { + if (className == null) { className = m_entry.getClassName(); } String methodName = getDeobfMethodName(); - if( methodName == null ) - { + if (methodName == null) { methodName = m_entry.getName(); } return className + "." + methodName + "()"; } - public void load( JarIndex index ) - { + public void load(JarIndex index) { // get all method implementations List nodes = Lists.newArrayList(); - for( String implementingClassName : index.getImplementingClasses( m_entry.getClassName() ) ) - { + for (String implementingClassName : index.getImplementingClasses(m_entry.getClassName())) { MethodEntry methodEntry = new MethodEntry( - new ClassEntry( implementingClassName ), + new ClassEntry(implementingClassName), m_entry.getName(), m_entry.getSignature() ); - if( index.containsObfBehavior( methodEntry ) ) - { - nodes.add( new MethodImplementationsTreeNode( m_deobfuscatingTranslator, methodEntry ) ); + if (index.containsObfBehavior(methodEntry)) { + nodes.add(new MethodImplementationsTreeNode(m_deobfuscatingTranslator, methodEntry)); } } // add them to this node - for( MethodImplementationsTreeNode node : nodes ) - { - this.add( node ); + for (MethodImplementationsTreeNode node : nodes) { + this.add(node); } } - public static MethodImplementationsTreeNode findNode( MethodImplementationsTreeNode node, MethodEntry entry ) - { + public static MethodImplementationsTreeNode findNode(MethodImplementationsTreeNode node, MethodEntry entry) { // is this the node? - if( node.getMethodEntry().equals( entry ) ) - { + if (node.getMethodEntry().equals(entry)) { return node; } // recurse - for( int i=0; i nodes = Lists.newArrayList(); - for( String subclassName : index.getTranslationIndex().getSubclassNames( m_entry.getClassName() ) ) - { + for (String subclassName : index.getTranslationIndex().getSubclassNames(m_entry.getClassName())) { MethodEntry methodEntry = new MethodEntry( - new ClassEntry( subclassName ), + new ClassEntry(subclassName), m_entry.getName(), m_entry.getSignature() ); - nodes.add( new MethodInheritanceTreeNode( + nodes.add(new MethodInheritanceTreeNode( m_deobfuscatingTranslator, methodEntry, - index.containsObfBehavior( methodEntry ) - ) ); + index.containsObfBehavior(methodEntry) + )); } // add them to this node - for( MethodInheritanceTreeNode node : nodes ) - { - this.add( node ); + for (MethodInheritanceTreeNode node : nodes) { + this.add(node); } - if( recurse ) - { - for( MethodInheritanceTreeNode node : nodes ) - { - node.load( index, true ); + if (recurse) { + for (MethodInheritanceTreeNode node : nodes) { + node.load(index, true); } } } - public static MethodInheritanceTreeNode findNode( MethodInheritanceTreeNode node, MethodEntry entry ) - { + public static MethodInheritanceTreeNode findNode(MethodInheritanceTreeNode node, MethodEntry entry) { // is this the node? - if( node.getMethodEntry().equals( entry ) ) - { + if (node.getMethodEntry().equals(entry)) { return node; } // recurse - for( int i=0; i -{ +public interface ReferenceTreeNode { E getEntry(); EntryReference getReference(); } diff --git a/src/cuchaz/enigma/analysis/SourceIndex.java b/src/cuchaz/enigma/analysis/SourceIndex.java index 0e33de0..b43ab61 100644 --- a/src/cuchaz/enigma/analysis/SourceIndex.java +++ b/src/cuchaz/enigma/analysis/SourceIndex.java @@ -25,16 +25,15 @@ import com.strobel.decompiler.languages.java.ast.Identifier; import cuchaz.enigma.mapping.Entry; -public class SourceIndex -{ +public class SourceIndex { + private String m_source; private TreeMap> m_tokenToReference; private Multimap,Token> m_referenceToTokens; private Map m_declarationToToken; private List m_lineOffsets; - public SourceIndex( String source ) - { + public SourceIndex(String source) { m_source = source; m_tokenToReference = Maps.newTreeMap(); m_referenceToTokens = HashMultimap.create(); @@ -42,142 +41,119 @@ public class SourceIndex m_lineOffsets = Lists.newArrayList(); // count the lines - m_lineOffsets.add( 0 ); - for( int i=0; i= 0 ) - { + int pos = name.lastIndexOf('$'); + if (pos >= 0) { token.end -= pos + 1; } return token; } - public void addReference( AstNode node, Entry deobfEntry, Entry deobfContext ) - { - Token token = getToken( node ); - if( token != null ) - { - EntryReference deobfReference = new EntryReference( deobfEntry, token.text, deobfContext ); - m_tokenToReference.put( token, deobfReference ); - m_referenceToTokens.put( deobfReference, token ); + public void addReference(AstNode node, Entry deobfEntry, Entry deobfContext) { + Token token = getToken(node); + if (token != null) { + EntryReference deobfReference = new EntryReference(deobfEntry, token.text, deobfContext); + m_tokenToReference.put(token, deobfReference); + m_referenceToTokens.put(deobfReference, token); } } - public void addDeclaration( AstNode node, Entry deobfEntry ) - { - Token token = getToken( node ); - if( token != null ) - { - EntryReference reference = new EntryReference( deobfEntry, token.text ); - m_tokenToReference.put( token, reference ); - m_referenceToTokens.put( reference, token ); - m_declarationToToken.put( deobfEntry, token ); + public void addDeclaration(AstNode node, Entry deobfEntry) { + Token token = getToken(node); + if (token != null) { + EntryReference reference = new EntryReference(deobfEntry, token.text); + m_tokenToReference.put(token, reference); + m_referenceToTokens.put(reference, token); + m_declarationToToken.put(deobfEntry, token); } } - public Token getReferenceToken( int pos ) - { - Token token = m_tokenToReference.floorKey( new Token( pos, pos, null ) ); - if( token != null && token.contains( pos ) ) - { + public Token getReferenceToken(int pos) { + Token token = m_tokenToReference.floorKey(new Token(pos, pos, null)); + if (token != null && token.contains(pos)) { return token; } return null; } - public Collection getReferenceTokens( EntryReference deobfReference ) - { - return m_referenceToTokens.get( deobfReference ); + public Collection getReferenceTokens(EntryReference deobfReference) { + return m_referenceToTokens.get(deobfReference); } - public EntryReference getDeobfReference( Token token ) - { - if( token == null ) - { + public EntryReference getDeobfReference(Token token) { + if (token == null) { return null; } - return m_tokenToReference.get( token ); + return m_tokenToReference.get(token); } - public void replaceDeobfReference( Token token, EntryReference newDeobfReference ) - { - EntryReference oldDeobfReference = m_tokenToReference.get( token ); - m_tokenToReference.put( token, newDeobfReference ); - Collection tokens = m_referenceToTokens.get( oldDeobfReference ); - m_referenceToTokens.removeAll( oldDeobfReference ); - m_referenceToTokens.putAll( newDeobfReference, tokens ); + public void replaceDeobfReference(Token token, EntryReference newDeobfReference) { + EntryReference oldDeobfReference = m_tokenToReference.get(token); + m_tokenToReference.put(token, newDeobfReference); + Collection tokens = m_referenceToTokens.get(oldDeobfReference); + m_referenceToTokens.removeAll(oldDeobfReference); + m_referenceToTokens.putAll(newDeobfReference, tokens); } - public Iterable referenceTokens( ) - { + public Iterable referenceTokens() { return m_tokenToReference.keySet(); } - public Iterable declarationTokens( ) - { + public Iterable declarationTokens() { return m_declarationToToken.values(); } - public Token getDeclarationToken( Entry deobfEntry ) - { - return m_declarationToToken.get( deobfEntry ); + public Token getDeclarationToken(Entry deobfEntry) { + return m_declarationToToken.get(deobfEntry); } - public int getLineNumber( int pos ) - { + public int getLineNumber(int pos) { // line number is 1-based int line = 0; - for( Integer offset : m_lineOffsets ) - { - if( offset > pos ) - { + for (Integer offset : m_lineOffsets) { + if (offset > pos) { break; } line++; @@ -185,15 +161,13 @@ public class SourceIndex return line; } - public int getColumnNumber( int pos ) - { + public int getColumnNumber(int pos) { // column number is 1-based - return pos - m_lineOffsets.get( getLineNumber( pos ) - 1 ) + 1; + return pos - m_lineOffsets.get(getLineNumber(pos) - 1) + 1; } - - private int toPos( int line, int col ) - { + + private int toPos(int line, int col) { // line and col are 1-based - return m_lineOffsets.get( line - 1 ) + col - 1; + return m_lineOffsets.get(line - 1) + col - 1; } } diff --git a/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java index 7ffd170..43c1749 100644 --- a/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java +++ b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java @@ -36,159 +36,128 @@ import cuchaz.enigma.mapping.ConstructorEntry; import cuchaz.enigma.mapping.FieldEntry; import cuchaz.enigma.mapping.MethodEntry; -public class SourceIndexBehaviorVisitor extends SourceIndexVisitor -{ +public class SourceIndexBehaviorVisitor extends SourceIndexVisitor { + private BehaviorEntry m_behaviorEntry; - public SourceIndexBehaviorVisitor( BehaviorEntry behaviorEntry ) - { + public SourceIndexBehaviorVisitor(BehaviorEntry behaviorEntry) { m_behaviorEntry = behaviorEntry; } @Override - public Void visitMethodDeclaration( MethodDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitMethodDeclaration(MethodDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitConstructorDeclaration( ConstructorDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitConstructorDeclaration(ConstructorDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitInvocationExpression( InvocationExpression node, SourceIndex index ) - { - MemberReference ref = node.getUserData( Keys.MEMBER_REFERENCE ); + public Void visitInvocationExpression(InvocationExpression node, SourceIndex index) { + MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); // get the behavior entry - ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); BehaviorEntry behaviorEntry = null; - if( ref instanceof MethodReference ) - { + if (ref instanceof MethodReference) { MethodReference methodRef = (MethodReference)ref; - if( methodRef.isConstructor() ) - { - behaviorEntry = new ConstructorEntry( classEntry, ref.getSignature() ); - } - else if( methodRef.isTypeInitializer() ) - { - behaviorEntry = new ConstructorEntry( classEntry ); - } - else - { - behaviorEntry = new MethodEntry( classEntry, ref.getName(), ref.getSignature() ); + if (methodRef.isConstructor()) { + behaviorEntry = new ConstructorEntry(classEntry, ref.getSignature()); + } else if (methodRef.isTypeInitializer()) { + behaviorEntry = new ConstructorEntry(classEntry); + } else { + behaviorEntry = new MethodEntry(classEntry, ref.getName(), ref.getSignature()); } } - if( behaviorEntry != null ) - { + if (behaviorEntry != null) { // get the node for the token AstNode tokenNode = null; - if( node.getTarget() instanceof MemberReferenceExpression ) - { + if (node.getTarget() instanceof MemberReferenceExpression) { tokenNode = ((MemberReferenceExpression)node.getTarget()).getMemberNameToken(); - } - else if( node.getTarget() instanceof SuperReferenceExpression ) - { + } else if (node.getTarget() instanceof SuperReferenceExpression) { tokenNode = node.getTarget(); - } - else if( node.getTarget() instanceof ThisReferenceExpression ) - { + } else if (node.getTarget() instanceof ThisReferenceExpression) { tokenNode = node.getTarget(); } - if( tokenNode != null ) - { - index.addReference( tokenNode, behaviorEntry, m_behaviorEntry ); + if (tokenNode != null) { + index.addReference(tokenNode, behaviorEntry, m_behaviorEntry); } } - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitMemberReferenceExpression( MemberReferenceExpression node, SourceIndex index ) - { - MemberReference ref = node.getUserData( Keys.MEMBER_REFERENCE ); - if( ref != null ) - { + public Void visitMemberReferenceExpression(MemberReferenceExpression node, SourceIndex index) { + MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); + if (ref != null) { // make sure this is actually a field - if( ref.getSignature().indexOf( '(' ) >= 0 ) - { - throw new Error( "Expected a field here! got " + ref ); + if (ref.getSignature().indexOf('(') >= 0) { + throw new Error("Expected a field here! got " + ref); } - ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); - FieldEntry fieldEntry = new FieldEntry( classEntry, ref.getName() ); - index.addReference( node.getMemberNameToken(), fieldEntry, m_behaviorEntry ); + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); + FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName()); + index.addReference(node.getMemberNameToken(), fieldEntry, m_behaviorEntry); } - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitSimpleType( SimpleType node, SourceIndex index ) - { - TypeReference ref = node.getUserData( Keys.TYPE_REFERENCE ); - if( node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY ) - { - ClassEntry classEntry = new ClassEntry( ref.getInternalName() ); - index.addReference( node.getIdentifierToken(), classEntry, m_behaviorEntry ); + public Void visitSimpleType(SimpleType node, SourceIndex index) { + TypeReference ref = node.getUserData(Keys.TYPE_REFERENCE); + if (node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY) { + ClassEntry classEntry = new ClassEntry(ref.getInternalName()); + index.addReference(node.getIdentifierToken(), classEntry, m_behaviorEntry); } - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitParameterDeclaration( ParameterDeclaration node, SourceIndex index ) - { - ParameterDefinition def = node.getUserData( Keys.PARAMETER_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); + public Void visitParameterDeclaration(ParameterDeclaration node, SourceIndex index) { + ParameterDefinition def = node.getUserData(Keys.PARAMETER_DEFINITION); + ClassEntry classEntry = new ClassEntry(def.getDeclaringType().getInternalName()); MethodDefinition methodDef = (MethodDefinition)def.getMethod(); BehaviorEntry behaviorEntry; - if( methodDef.isConstructor() ) - { - behaviorEntry = new ConstructorEntry( classEntry, methodDef.getSignature() ); - } - else - { - behaviorEntry = new MethodEntry( classEntry, methodDef.getName(), methodDef.getSignature() ); + if (methodDef.isConstructor()) { + behaviorEntry = new ConstructorEntry(classEntry, methodDef.getSignature()); + } else { + behaviorEntry = new MethodEntry(classEntry, methodDef.getName(), methodDef.getSignature()); } - ArgumentEntry argumentEntry = new ArgumentEntry( behaviorEntry, def.getPosition(), node.getName() ); - index.addDeclaration( node.getNameToken(), argumentEntry ); + ArgumentEntry argumentEntry = new ArgumentEntry(behaviorEntry, def.getPosition(), node.getName()); + index.addDeclaration(node.getNameToken(), argumentEntry); - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitIdentifierExpression( IdentifierExpression node, SourceIndex index ) - { - MemberReference ref = node.getUserData( Keys.MEMBER_REFERENCE ); - if( ref != null ) - { - ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); - FieldEntry fieldEntry = new FieldEntry( classEntry, ref.getName() ); - index.addReference( node.getIdentifierToken(), fieldEntry, m_behaviorEntry ); + public Void visitIdentifierExpression(IdentifierExpression node, SourceIndex index) { + MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); + if (ref != null) { + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); + FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName()); + index.addReference(node.getIdentifierToken(), fieldEntry, m_behaviorEntry); } - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitObjectCreationExpression( ObjectCreationExpression node, SourceIndex index ) - { - MemberReference ref = node.getUserData( Keys.MEMBER_REFERENCE ); - if( ref != null ) - { - ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); - ConstructorEntry constructorEntry = new ConstructorEntry( classEntry, ref.getSignature() ); - if( node.getType() instanceof SimpleType ) - { + public Void visitObjectCreationExpression(ObjectCreationExpression node, SourceIndex index) { + MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); + if (ref != null) { + ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); + ConstructorEntry constructorEntry = new ConstructorEntry(classEntry, ref.getSignature()); + if (node.getType() instanceof SimpleType) { SimpleType simpleTypeNode = (SimpleType)node.getType(); - index.addReference( simpleTypeNode.getIdentifierToken(), constructorEntry, m_behaviorEntry ); + index.addReference(simpleTypeNode.getIdentifierToken(), constructorEntry, m_behaviorEntry); } } - return recurse( node, index ); + return recurse(node, index); } } diff --git a/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java index 24c4822..7b902a9 100644 --- a/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java +++ b/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java @@ -31,95 +31,84 @@ import cuchaz.enigma.mapping.ClassEntry; import cuchaz.enigma.mapping.ConstructorEntry; import cuchaz.enigma.mapping.FieldEntry; -public class SourceIndexClassVisitor extends SourceIndexVisitor -{ +public class SourceIndexClassVisitor extends SourceIndexVisitor { + private ClassEntry m_classEntry; - public SourceIndexClassVisitor( ClassEntry classEntry ) - { + public SourceIndexClassVisitor(ClassEntry classEntry) { m_classEntry = classEntry; } @Override - public Void visitTypeDeclaration( TypeDeclaration node, SourceIndex index ) - { + public Void visitTypeDeclaration(TypeDeclaration node, SourceIndex index) { // is this this class, or a subtype? - TypeDefinition def = node.getUserData( Keys.TYPE_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getInternalName() ); - if( !classEntry.equals( m_classEntry ) ) - { + TypeDefinition def = node.getUserData(Keys.TYPE_DEFINITION); + ClassEntry classEntry = new ClassEntry(def.getInternalName()); + if (!classEntry.equals(m_classEntry)) { // it's a sub-type, recurse - index.addDeclaration( node.getNameToken(), classEntry ); - return node.acceptVisitor( new SourceIndexClassVisitor( classEntry ), index ); + index.addDeclaration(node.getNameToken(), classEntry); + return node.acceptVisitor(new SourceIndexClassVisitor(classEntry), index); } - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitSimpleType( SimpleType node, SourceIndex index ) - { - TypeReference ref = node.getUserData( Keys.TYPE_REFERENCE ); - if( node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY ) - { - ClassEntry classEntry = new ClassEntry( ref.getInternalName() ); - index.addReference( node.getIdentifierToken(), classEntry, m_classEntry ); + public Void visitSimpleType(SimpleType node, SourceIndex index) { + TypeReference ref = node.getUserData(Keys.TYPE_REFERENCE); + if (node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY) { + ClassEntry classEntry = new ClassEntry(ref.getInternalName()); + index.addReference(node.getIdentifierToken(), classEntry, m_classEntry); } - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitMethodDeclaration( MethodDeclaration node, SourceIndex index ) - { - MethodDefinition def = node.getUserData( Keys.METHOD_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); - BehaviorEntry behaviorEntry = BehaviorEntryFactory.create( classEntry, def.getName(), def.getSignature() ); + public Void visitMethodDeclaration(MethodDeclaration node, SourceIndex index) { + MethodDefinition def = node.getUserData(Keys.METHOD_DEFINITION); + ClassEntry classEntry = new ClassEntry(def.getDeclaringType().getInternalName()); + BehaviorEntry behaviorEntry = BehaviorEntryFactory.create(classEntry, def.getName(), def.getSignature()); AstNode tokenNode = node.getNameToken(); - if( behaviorEntry instanceof ConstructorEntry ) - { + if (behaviorEntry instanceof ConstructorEntry) { ConstructorEntry constructorEntry = (ConstructorEntry)behaviorEntry; - if( constructorEntry.isStatic() ) - { + if (constructorEntry.isStatic()) { tokenNode = node.getModifiers().firstOrNullObject(); } } - index.addDeclaration( tokenNode, behaviorEntry ); - return node.acceptVisitor( new SourceIndexBehaviorVisitor( behaviorEntry ), index ); + index.addDeclaration(tokenNode, behaviorEntry); + return node.acceptVisitor(new SourceIndexBehaviorVisitor(behaviorEntry), index); } @Override - public Void visitConstructorDeclaration( ConstructorDeclaration node, SourceIndex index ) - { - MethodDefinition def = node.getUserData( Keys.METHOD_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); - ConstructorEntry constructorEntry = new ConstructorEntry( classEntry, def.getSignature() ); - index.addDeclaration( node.getNameToken(), constructorEntry ); - return node.acceptVisitor( new SourceIndexBehaviorVisitor( constructorEntry ), index ); + public Void visitConstructorDeclaration(ConstructorDeclaration node, SourceIndex index) { + MethodDefinition def = node.getUserData(Keys.METHOD_DEFINITION); + ClassEntry classEntry = new ClassEntry(def.getDeclaringType().getInternalName()); + ConstructorEntry constructorEntry = new ConstructorEntry(classEntry, def.getSignature()); + index.addDeclaration(node.getNameToken(), constructorEntry); + return node.acceptVisitor(new SourceIndexBehaviorVisitor(constructorEntry), index); } @Override - public Void visitFieldDeclaration( FieldDeclaration node, SourceIndex index ) - { - FieldDefinition def = node.getUserData( Keys.FIELD_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); - FieldEntry fieldEntry = new FieldEntry( classEntry, def.getName() ); - assert( node.getVariables().size() == 1 ); + public Void visitFieldDeclaration(FieldDeclaration node, SourceIndex index) { + FieldDefinition def = node.getUserData(Keys.FIELD_DEFINITION); + ClassEntry classEntry = new ClassEntry(def.getDeclaringType().getInternalName()); + FieldEntry fieldEntry = new FieldEntry(classEntry, def.getName()); + assert (node.getVariables().size() == 1); VariableInitializer variable = node.getVariables().firstOrNullObject(); - index.addDeclaration( variable.getNameToken(), fieldEntry ); + index.addDeclaration(variable.getNameToken(), fieldEntry); - return recurse( node, index ); + return recurse(node, index); } @Override - public Void visitEnumValueDeclaration( EnumValueDeclaration node, SourceIndex index ) - { + public Void visitEnumValueDeclaration(EnumValueDeclaration node, SourceIndex index) { // treat enum declarations as field declarations - FieldDefinition def = node.getUserData( Keys.FIELD_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); - FieldEntry fieldEntry = new FieldEntry( classEntry, def.getName() ); - index.addDeclaration( node.getNameToken(), fieldEntry ); + FieldDefinition def = node.getUserData(Keys.FIELD_DEFINITION); + ClassEntry classEntry = new ClassEntry(def.getDeclaringType().getInternalName()); + FieldEntry fieldEntry = new FieldEntry(classEntry, def.getName()); + index.addDeclaration(node.getNameToken(), fieldEntry); - return recurse( node, index ); + return recurse(node, index); } } diff --git a/src/cuchaz/enigma/analysis/SourceIndexVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexVisitor.java index 4e98989..0d5bdc0 100644 --- a/src/cuchaz/enigma/analysis/SourceIndexVisitor.java +++ b/src/cuchaz/enigma/analysis/SourceIndexVisitor.java @@ -87,438 +87,366 @@ import com.strobel.decompiler.patterns.Pattern; import cuchaz.enigma.mapping.ClassEntry; -public class SourceIndexVisitor implements IAstVisitor -{ - @Override - public Void visitTypeDeclaration( TypeDeclaration node, SourceIndex index ) - { - TypeDefinition def = node.getUserData( Keys.TYPE_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getInternalName() ); - index.addDeclaration( node.getNameToken(), classEntry ); +public class SourceIndexVisitor implements IAstVisitor { + + @Override + public Void visitTypeDeclaration(TypeDeclaration node, SourceIndex index) { + TypeDefinition def = node.getUserData(Keys.TYPE_DEFINITION); + ClassEntry classEntry = new ClassEntry(def.getInternalName()); + index.addDeclaration(node.getNameToken(), classEntry); - return node.acceptVisitor( new SourceIndexClassVisitor( classEntry ), index ); + return node.acceptVisitor(new SourceIndexClassVisitor(classEntry), index); } - protected Void recurse( AstNode node, SourceIndex index ) - { - for( final AstNode child : node.getChildren() ) - { - child.acceptVisitor( this, index ); + protected Void recurse(AstNode node, SourceIndex index) { + for (final AstNode child : node.getChildren()) { + child.acceptVisitor(this, index); } return null; } @Override - public Void visitMethodDeclaration( MethodDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitMethodDeclaration(MethodDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitConstructorDeclaration( ConstructorDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitConstructorDeclaration(ConstructorDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitFieldDeclaration( FieldDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitFieldDeclaration(FieldDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitEnumValueDeclaration( EnumValueDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitEnumValueDeclaration(EnumValueDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitParameterDeclaration( ParameterDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitParameterDeclaration(ParameterDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitInvocationExpression( InvocationExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitInvocationExpression(InvocationExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitMemberReferenceExpression( MemberReferenceExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitMemberReferenceExpression(MemberReferenceExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitSimpleType( SimpleType node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitSimpleType(SimpleType node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitIdentifierExpression( IdentifierExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitIdentifierExpression(IdentifierExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitComment( Comment node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitComment(Comment node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitPatternPlaceholder( AstNode node, Pattern pattern, SourceIndex index ) - { - return recurse( node, index ); + public Void visitPatternPlaceholder(AstNode node, Pattern pattern, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitTypeReference( TypeReferenceExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitTypeReference(TypeReferenceExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitJavaTokenNode( JavaTokenNode node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitJavaTokenNode(JavaTokenNode node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitIdentifier( Identifier node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitIdentifier(Identifier node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitNullReferenceExpression( NullReferenceExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitNullReferenceExpression(NullReferenceExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitThisReferenceExpression( ThisReferenceExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitThisReferenceExpression(ThisReferenceExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitSuperReferenceExpression( SuperReferenceExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitSuperReferenceExpression(SuperReferenceExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitClassOfExpression( ClassOfExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitClassOfExpression(ClassOfExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitBlockStatement( BlockStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitBlockStatement(BlockStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitExpressionStatement( ExpressionStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitExpressionStatement(ExpressionStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitBreakStatement( BreakStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitBreakStatement(BreakStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitContinueStatement( ContinueStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitContinueStatement(ContinueStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitDoWhileStatement( DoWhileStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitDoWhileStatement(DoWhileStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitEmptyStatement( EmptyStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitEmptyStatement(EmptyStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitIfElseStatement( IfElseStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitIfElseStatement(IfElseStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitLabelStatement( LabelStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitLabelStatement(LabelStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitLabeledStatement( LabeledStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitLabeledStatement(LabeledStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitReturnStatement( ReturnStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitReturnStatement(ReturnStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitSwitchStatement( SwitchStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitSwitchStatement(SwitchStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitSwitchSection( SwitchSection node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitSwitchSection(SwitchSection node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitCaseLabel( CaseLabel node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitCaseLabel(CaseLabel node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitThrowStatement( ThrowStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitThrowStatement(ThrowStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitCatchClause( CatchClause node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitCatchClause(CatchClause node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitAnnotation( Annotation node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitAnnotation(Annotation node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitNewLine( NewLineNode node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitNewLine(NewLineNode node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitVariableDeclaration( VariableDeclarationStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitVariableDeclaration(VariableDeclarationStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitVariableInitializer( VariableInitializer node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitVariableInitializer(VariableInitializer node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitText( TextNode node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitText(TextNode node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitImportDeclaration( ImportDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitImportDeclaration(ImportDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitInitializerBlock( InstanceInitializer node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitInitializerBlock(InstanceInitializer node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitTypeParameterDeclaration( TypeParameterDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitTypeParameterDeclaration(TypeParameterDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitCompilationUnit( CompilationUnit node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitCompilationUnit(CompilationUnit node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitPackageDeclaration( PackageDeclaration node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitPackageDeclaration(PackageDeclaration node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitArraySpecifier( ArraySpecifier node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitArraySpecifier(ArraySpecifier node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitComposedType( ComposedType node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitComposedType(ComposedType node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitWhileStatement( WhileStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitWhileStatement(WhileStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitPrimitiveExpression( PrimitiveExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitPrimitiveExpression(PrimitiveExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitCastExpression( CastExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitCastExpression(CastExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitBinaryOperatorExpression( BinaryOperatorExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitBinaryOperatorExpression(BinaryOperatorExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitInstanceOfExpression( InstanceOfExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitInstanceOfExpression(InstanceOfExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitIndexerExpression( IndexerExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitIndexerExpression(IndexerExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitUnaryOperatorExpression( UnaryOperatorExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitUnaryOperatorExpression(UnaryOperatorExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitConditionalExpression( ConditionalExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitConditionalExpression(ConditionalExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitArrayInitializerExpression( ArrayInitializerExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitArrayInitializerExpression(ArrayInitializerExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitObjectCreationExpression( ObjectCreationExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitObjectCreationExpression(ObjectCreationExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitArrayCreationExpression( ArrayCreationExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitArrayCreationExpression(ArrayCreationExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitAssignmentExpression( AssignmentExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitAssignmentExpression(AssignmentExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitForStatement( ForStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitForStatement(ForStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitForEachStatement( ForEachStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitForEachStatement(ForEachStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitTryCatchStatement( TryCatchStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitTryCatchStatement(TryCatchStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitGotoStatement( GotoStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitGotoStatement(GotoStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitParenthesizedExpression( ParenthesizedExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitParenthesizedExpression(ParenthesizedExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitSynchronizedStatement( SynchronizedStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitSynchronizedStatement(SynchronizedStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitAnonymousObjectCreationExpression( AnonymousObjectCreationExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitAnonymousObjectCreationExpression(AnonymousObjectCreationExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitWildcardType( WildcardType node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitWildcardType(WildcardType node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitMethodGroupExpression( MethodGroupExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitMethodGroupExpression(MethodGroupExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitAssertStatement( AssertStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitAssertStatement(AssertStatement node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitLambdaExpression( LambdaExpression node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitLambdaExpression(LambdaExpression node, SourceIndex index) { + return recurse(node, index); } @Override - public Void visitLocalTypeDeclarationStatement( LocalTypeDeclarationStatement node, SourceIndex index ) - { - return recurse( node, index ); + public Void visitLocalTypeDeclarationStatement(LocalTypeDeclarationStatement node, SourceIndex index) { + return recurse(node, index); } } diff --git a/src/cuchaz/enigma/analysis/Token.java b/src/cuchaz/enigma/analysis/Token.java index 5e70db7..481d2f4 100644 --- a/src/cuchaz/enigma/analysis/Token.java +++ b/src/cuchaz/enigma/analysis/Token.java @@ -10,56 +10,47 @@ ******************************************************************************/ package cuchaz.enigma.analysis; -public class Token implements Comparable -{ +public class Token implements Comparable { + public int start; public int end; public String text; - public Token( int start, int end ) - { - this( start, end, null ); + public Token(int start, int end) { + this(start, end, null); } - public Token( int start, int end, String source ) - { + public Token(int start, int end, String source) { this.start = start; this.end = end; - if( source != null ) - { - this.text = source.substring( start, end ); + if (source != null) { + this.text = source.substring(start, end); } } - public boolean contains( int pos ) - { + public boolean contains(int pos) { return pos >= start && pos <= end; } - + @Override - public int compareTo( Token other ) - { + public int compareTo(Token other) { return start - other.start; } @Override - public boolean equals( Object other ) - { - if( other instanceof Token ) - { - return equals( (Token)other ); + public boolean equals(Object other) { + if (other instanceof Token) { + return equals((Token)other); } return false; } - public boolean equals( Token other ) - { + public boolean equals(Token other) { return start == other.start && end == other.end; } @Override - public String toString( ) - { - return String.format( "[%d,%d]", start, end ); + public String toString() { + return String.format("[%d,%d]", start, end); } } diff --git a/src/cuchaz/enigma/analysis/TranslationIndex.java b/src/cuchaz/enigma/analysis/TranslationIndex.java index 5311ec7..c14fd59 100644 --- a/src/cuchaz/enigma/analysis/TranslationIndex.java +++ b/src/cuchaz/enigma/analysis/TranslationIndex.java @@ -23,104 +23,85 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Multimap; -public class TranslationIndex implements Serializable -{ +public class TranslationIndex implements Serializable { + private static final long serialVersionUID = 738687982126844179L; private Map m_superclasses; private Multimap m_fields; - public TranslationIndex( ) - { + public TranslationIndex() { m_superclasses = Maps.newHashMap(); m_fields = HashMultimap.create(); } - public TranslationIndex( TranslationIndex other ) - { - m_superclasses = Maps.newHashMap( other.m_superclasses ); - m_fields = HashMultimap.create( other.m_fields ); + public TranslationIndex(TranslationIndex other) { + m_superclasses = Maps.newHashMap(other.m_superclasses); + m_fields = HashMultimap.create(other.m_fields); } - public void addSuperclass( String className, String superclassName ) - { - className = Descriptor.toJvmName( className ); - superclassName = Descriptor.toJvmName( superclassName ); + public void addSuperclass(String className, String superclassName) { + className = Descriptor.toJvmName(className); + superclassName = Descriptor.toJvmName(superclassName); - if( className.equals( superclassName ) ) - { - throw new IllegalArgumentException( "Class cannot be its own superclass! " + className ); + if (className.equals(superclassName)) { + throw new IllegalArgumentException("Class cannot be its own superclass! " + className); } - if( !isJre( className ) && !isJre( superclassName ) ) - { - m_superclasses.put( className, superclassName ); + if (!isJre(className) && !isJre(superclassName)) { + m_superclasses.put(className, superclassName); } } - public void addField( String className, String fieldName ) - { - m_fields.put( className, fieldName ); + public void addField(String className, String fieldName) { + m_fields.put(className, fieldName); } - public void renameClasses( Map renames ) - { - EntryRenamer.renameClassesInMap( renames, m_superclasses ); - EntryRenamer.renameClassesInMultimap( renames, m_fields ); + public void renameClasses(Map renames) { + EntryRenamer.renameClassesInMap(renames, m_superclasses); + EntryRenamer.renameClassesInMultimap(renames, m_fields); } - public String getSuperclassName( String className ) - { - return m_superclasses.get( className ); + public String getSuperclassName(String className) { + return m_superclasses.get(className); } - public List getAncestry( String className ) - { + public List getAncestry(String className) { List ancestors = new ArrayList(); - while( className != null ) - { - className = getSuperclassName( className ); - if( className != null ) - { - ancestors.add( className ); + while (className != null) { + className = getSuperclassName(className); + if (className != null) { + ancestors.add(className); } } return ancestors; } - public List getSubclassNames( String className ) - { + public List getSubclassNames(String className) { // linear search is fast enough for now List subclasses = Lists.newArrayList(); - for( Map.Entry entry : m_superclasses.entrySet() ) - { + for (Map.Entry entry : m_superclasses.entrySet()) { String subclass = entry.getKey(); String superclass = entry.getValue(); - if( className.equals( superclass ) ) - { - subclasses.add( subclass ); + if (className.equals(superclass)) { + subclasses.add(subclass); } } return subclasses; } - public void getSubclassNamesRecursively( Set out, String className ) - { - for( String subclassName : getSubclassNames( className ) ) - { - out.add( subclassName ); - getSubclassNamesRecursively( out, subclassName ); + public void getSubclassNamesRecursively(Set out, String className) { + for (String subclassName : getSubclassNames(className)) { + out.add(subclassName); + getSubclassNamesRecursively(out, subclassName); } } - public boolean containsField( String className, String fieldName ) - { - return m_fields.containsEntry( className, fieldName ); + public boolean containsField(String className, String fieldName) { + return m_fields.containsEntry(className, fieldName); } - private boolean isJre( String className ) - { - return className.startsWith( "java/" ) - || className.startsWith( "javax/" ); + private boolean isJre(String className) { + return className.startsWith("java/") || className.startsWith("javax/"); } } diff --git a/src/cuchaz/enigma/analysis/TreeDumpVisitor.java b/src/cuchaz/enigma/analysis/TreeDumpVisitor.java index e6ecb10..23f8089 100644 --- a/src/cuchaz/enigma/analysis/TreeDumpVisitor.java +++ b/src/cuchaz/enigma/analysis/TreeDumpVisitor.java @@ -90,92 +90,73 @@ import com.strobel.decompiler.languages.java.ast.WhileStatement; import com.strobel.decompiler.languages.java.ast.WildcardType; import com.strobel.decompiler.patterns.Pattern; -public class TreeDumpVisitor implements IAstVisitor -{ +public class TreeDumpVisitor implements IAstVisitor { + private File m_file; private Writer m_out; - public TreeDumpVisitor( File file ) - { + public TreeDumpVisitor(File file) { m_file = file; m_out = null; } @Override - public Void visitCompilationUnit( CompilationUnit node, Void ignored ) - { - try - { - m_out = new FileWriter( m_file ); - recurse( node, ignored ); + public Void visitCompilationUnit(CompilationUnit node, Void ignored) { + try { + m_out = new FileWriter(m_file); + recurse(node, ignored); m_out.close(); return null; - } - catch( IOException ex ) - { - throw new Error( ex ); + } catch (IOException ex) { + throw new Error(ex); } } - - private Void recurse( AstNode node, Void ignored ) - { + + private Void recurse(AstNode node, Void ignored) { // show the tree - try - { - m_out.write( getIndent( node ) + node.getClass().getSimpleName() + " " + getText( node ) + " " + dumpUserData( node ) + " " + node.getRegion() + "\n" ); - } - catch( IOException ex ) - { - throw new Error( ex ); + try { + m_out.write(getIndent(node) + node.getClass().getSimpleName() + " " + getText(node) + " " + dumpUserData(node) + " " + node.getRegion() + "\n"); + } catch (IOException ex) { + throw new Error(ex); } // recurse - for( final AstNode child : node.getChildren() ) - { - child.acceptVisitor( this, ignored ); + for (final AstNode child : node.getChildren()) { + child.acceptVisitor(this, ignored); } return null; } - private String getText( AstNode node ) - { - if( node instanceof Identifier ) - { + private String getText(AstNode node) { + if (node instanceof Identifier) { return "\"" + ((Identifier)node).getName() + "\""; } return ""; } - private String dumpUserData( AstNode node ) - { + private String dumpUserData(AstNode node) { StringBuilder buf = new StringBuilder(); - for( Key key : Keys.ALL_KEYS ) - { - Object val = node.getUserData( key ); - if( val != null ) - { - buf.append( String.format( " [%s=%s]", key, val ) ); + for (Key key : Keys.ALL_KEYS) { + Object val = node.getUserData(key); + if (val != null) { + buf.append(String.format(" [%s=%s]", key, val)); } } return buf.toString(); } - private String getIndent( AstNode node ) - { + private String getIndent(AstNode node) { StringBuilder buf = new StringBuilder(); - int depth = getDepth( node ); - for( int i = 0; i < depth; i++ ) - { - buf.append( "\t" ); + int depth = getDepth(node); + for (int i = 0; i < depth; i++) { + buf.append("\t"); } return buf.toString(); } - private int getDepth( AstNode node ) - { + private int getDepth(AstNode node) { int depth = -1; - while( node != null ) - { + while (node != null) { depth++; node = node.getParent(); } @@ -185,416 +166,347 @@ public class TreeDumpVisitor implements IAstVisitor // OVERRIDES WE DON'T CARE ABOUT @Override - public Void visitInvocationExpression( InvocationExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitInvocationExpression(InvocationExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitMemberReferenceExpression( MemberReferenceExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitMemberReferenceExpression(MemberReferenceExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitSimpleType( SimpleType node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitSimpleType(SimpleType node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitMethodDeclaration( MethodDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitMethodDeclaration(MethodDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitConstructorDeclaration( ConstructorDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitConstructorDeclaration(ConstructorDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitParameterDeclaration( ParameterDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitParameterDeclaration(ParameterDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitFieldDeclaration( FieldDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitFieldDeclaration(FieldDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitTypeDeclaration( TypeDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitTypeDeclaration(TypeDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitComment( Comment node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitComment(Comment node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitPatternPlaceholder( AstNode node, Pattern pattern, Void ignored ) - { - return recurse( node, ignored ); + public Void visitPatternPlaceholder(AstNode node, Pattern pattern, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitTypeReference( TypeReferenceExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitTypeReference(TypeReferenceExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitJavaTokenNode( JavaTokenNode node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitJavaTokenNode(JavaTokenNode node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitIdentifier( Identifier node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitIdentifier(Identifier node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitNullReferenceExpression( NullReferenceExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitNullReferenceExpression(NullReferenceExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitThisReferenceExpression( ThisReferenceExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitThisReferenceExpression(ThisReferenceExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitSuperReferenceExpression( SuperReferenceExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitSuperReferenceExpression(SuperReferenceExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitClassOfExpression( ClassOfExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitClassOfExpression(ClassOfExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitBlockStatement( BlockStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitBlockStatement(BlockStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitExpressionStatement( ExpressionStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitExpressionStatement(ExpressionStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitBreakStatement( BreakStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitBreakStatement(BreakStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitContinueStatement( ContinueStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitContinueStatement(ContinueStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitDoWhileStatement( DoWhileStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitDoWhileStatement(DoWhileStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitEmptyStatement( EmptyStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitEmptyStatement(EmptyStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitIfElseStatement( IfElseStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitIfElseStatement(IfElseStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitLabelStatement( LabelStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitLabelStatement(LabelStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitLabeledStatement( LabeledStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitLabeledStatement(LabeledStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitReturnStatement( ReturnStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitReturnStatement(ReturnStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitSwitchStatement( SwitchStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitSwitchStatement(SwitchStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitSwitchSection( SwitchSection node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitSwitchSection(SwitchSection node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitCaseLabel( CaseLabel node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitCaseLabel(CaseLabel node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitThrowStatement( ThrowStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitThrowStatement(ThrowStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitCatchClause( CatchClause node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitCatchClause(CatchClause node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitAnnotation( Annotation node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitAnnotation(Annotation node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitNewLine( NewLineNode node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitNewLine(NewLineNode node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitVariableDeclaration( VariableDeclarationStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitVariableDeclaration(VariableDeclarationStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitVariableInitializer( VariableInitializer node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitVariableInitializer(VariableInitializer node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitText( TextNode node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitText(TextNode node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitImportDeclaration( ImportDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitImportDeclaration(ImportDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitInitializerBlock( InstanceInitializer node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitInitializerBlock(InstanceInitializer node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitTypeParameterDeclaration( TypeParameterDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitTypeParameterDeclaration(TypeParameterDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitPackageDeclaration( PackageDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitPackageDeclaration(PackageDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitArraySpecifier( ArraySpecifier node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitArraySpecifier(ArraySpecifier node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitComposedType( ComposedType node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitComposedType(ComposedType node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitWhileStatement( WhileStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitWhileStatement(WhileStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitPrimitiveExpression( PrimitiveExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitPrimitiveExpression(PrimitiveExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitCastExpression( CastExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitCastExpression(CastExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitBinaryOperatorExpression( BinaryOperatorExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitBinaryOperatorExpression(BinaryOperatorExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitInstanceOfExpression( InstanceOfExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitInstanceOfExpression(InstanceOfExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitIndexerExpression( IndexerExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitIndexerExpression(IndexerExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitIdentifierExpression( IdentifierExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitIdentifierExpression(IdentifierExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitUnaryOperatorExpression( UnaryOperatorExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitUnaryOperatorExpression(UnaryOperatorExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitConditionalExpression( ConditionalExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitConditionalExpression(ConditionalExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitArrayInitializerExpression( ArrayInitializerExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitArrayInitializerExpression(ArrayInitializerExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitObjectCreationExpression( ObjectCreationExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitObjectCreationExpression(ObjectCreationExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitArrayCreationExpression( ArrayCreationExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitArrayCreationExpression(ArrayCreationExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitAssignmentExpression( AssignmentExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitAssignmentExpression(AssignmentExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitForStatement( ForStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitForStatement(ForStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitForEachStatement( ForEachStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitForEachStatement(ForEachStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitTryCatchStatement( TryCatchStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitTryCatchStatement(TryCatchStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitGotoStatement( GotoStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitGotoStatement(GotoStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitParenthesizedExpression( ParenthesizedExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitParenthesizedExpression(ParenthesizedExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitSynchronizedStatement( SynchronizedStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitSynchronizedStatement(SynchronizedStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitAnonymousObjectCreationExpression( AnonymousObjectCreationExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitAnonymousObjectCreationExpression(AnonymousObjectCreationExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitWildcardType( WildcardType node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitWildcardType(WildcardType node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitMethodGroupExpression( MethodGroupExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitMethodGroupExpression(MethodGroupExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitEnumValueDeclaration( EnumValueDeclaration node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitEnumValueDeclaration(EnumValueDeclaration node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitAssertStatement( AssertStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitAssertStatement(AssertStatement node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitLambdaExpression( LambdaExpression node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitLambdaExpression(LambdaExpression node, Void ignored) { + return recurse(node, ignored); } @Override - public Void visitLocalTypeDeclarationStatement( LocalTypeDeclarationStatement node, Void ignored ) - { - return recurse( node, ignored ); + public Void visitLocalTypeDeclarationStatement(LocalTypeDeclarationStatement node, Void ignored) { + return recurse(node, ignored); } } -- cgit v1.2.3