From cf3ffcee30083a71e68e3edb9ecbb936cc255992 Mon Sep 17 00:00:00 2001 From: jeff Date: Sun, 28 Sep 2014 15:20:54 -0400 Subject: added proper support for renaming constructors --- src/cuchaz/enigma/analysis/EntryReference.java | 43 ++++++++++++++++++++-- src/cuchaz/enigma/analysis/JarIndex.java | 7 ++-- src/cuchaz/enigma/analysis/SourceIndex.java | 29 +++++++++++---- .../analysis/SourceIndexBehaviorVisitor.java | 27 +++----------- .../enigma/analysis/SourceIndexClassVisitor.java | 6 +-- src/cuchaz/enigma/analysis/Token.java | 10 +++++ src/cuchaz/enigma/analysis/TreeDumpVisitor.java | 2 +- 7 files changed, 83 insertions(+), 41 deletions(-) (limited to 'src/cuchaz/enigma/analysis') diff --git a/src/cuchaz/enigma/analysis/EntryReference.java b/src/cuchaz/enigma/analysis/EntryReference.java index 768c113..df977fb 100644 --- a/src/cuchaz/enigma/analysis/EntryReference.java +++ b/src/cuchaz/enigma/analysis/EntryReference.java @@ -10,21 +10,28 @@ ******************************************************************************/ package cuchaz.enigma.analysis; +import java.util.Arrays; +import java.util.List; + import cuchaz.enigma.Util; import cuchaz.enigma.mapping.ClassEntry; +import cuchaz.enigma.mapping.ConstructorEntry; import cuchaz.enigma.mapping.Entry; public class EntryReference { + private static final List ConstructorNonNames = Arrays.asList( "this", "super" ); public E entry; public C context; - public EntryReference( E entry ) + private boolean m_isNamed; + + public EntryReference( E entry, String sourceName ) { - this( entry, null ); + this( entry, sourceName, null ); } - public EntryReference( E entry, C context ) + public EntryReference( E entry, String sourceName, C context ) { if( entry == null ) { @@ -33,9 +40,22 @@ public class EntryReference this.entry = entry; this.context = context; + + m_isNamed = sourceName != null && sourceName.length() > 0; + if( entry instanceof ConstructorEntry && ConstructorNonNames.contains( sourceName ) ) + { + m_isNamed = false; + } + } + + public EntryReference( E entry, C context, EntryReference other ) + { + this.entry = entry; + this.context = context; + m_isNamed = other.m_isNamed; } - public ClassEntry getClassEntry( ) + public ClassEntry getLocationClassEntry( ) { if( context != null ) { @@ -44,6 +64,21 @@ public class EntryReference return entry.getClassEntry(); } + public boolean isNamed( ) + { + return m_isNamed; + } + + public Entry getNameableEntry( ) + { + if( entry instanceof ConstructorEntry ) + { + // renaming a constructor really means renaming the class + return entry.getClassEntry(); + } + return entry; + } + @Override public int hashCode( ) { diff --git a/src/cuchaz/enigma/analysis/JarIndex.java b/src/cuchaz/enigma/analysis/JarIndex.java index a2f6bf3..604e485 100644 --- a/src/cuchaz/enigma/analysis/JarIndex.java +++ b/src/cuchaz/enigma/analysis/JarIndex.java @@ -277,6 +277,7 @@ public class JarIndex } EntryReference reference = new EntryReference( calledMethodEntry, + call.getMethodName(), behaviorEntry ); m_behaviorReferences.put( calledMethodEntry, reference ); @@ -300,6 +301,7 @@ public class JarIndex } EntryReference reference = new EntryReference( calledFieldEntry, + call.getFieldName(), behaviorEntry ); m_fieldReferences.put( calledFieldEntry, reference ); @@ -308,9 +310,6 @@ public class JarIndex @Override public void edit( ConstructorCall call ) { - // TODO: save isSuper in the reference somehow - boolean isSuper = call.getMethodName().equals( "super" ); - String className = Descriptor.toJvmName( call.getClassName() ); ConstructorEntry calledConstructorEntry = new ConstructorEntry( new ClassEntry( className ), @@ -318,6 +317,7 @@ public class JarIndex ); EntryReference reference = new EntryReference( calledConstructorEntry, + call.getMethodName(), behaviorEntry ); m_behaviorReferences.put( calledConstructorEntry, reference ); @@ -333,6 +333,7 @@ public class JarIndex ); EntryReference reference = new EntryReference( calledConstructorEntry, + call.getClassName(), behaviorEntry ); m_behaviorReferences.put( calledConstructorEntry, reference ); diff --git a/src/cuchaz/enigma/analysis/SourceIndex.java b/src/cuchaz/enigma/analysis/SourceIndex.java index 38d10da..faae1a1 100644 --- a/src/cuchaz/enigma/analysis/SourceIndex.java +++ b/src/cuchaz/enigma/analysis/SourceIndex.java @@ -21,6 +21,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.strobel.decompiler.languages.Region; import com.strobel.decompiler.languages.java.ast.AstNode; +import com.strobel.decompiler.languages.java.ast.Identifier; import cuchaz.enigma.mapping.Entry; @@ -58,40 +59,54 @@ public class SourceIndex public Token getToken( AstNode node ) { + // get the text of the node + String name = ""; + if( node instanceof Identifier ) + { + name = ((Identifier)node).getName(); + } + // get a token for this node's region Region region = node.getRegion(); if( region.getBeginLine() == 0 || region.getEndLine() == 0 ) { // DEBUG - //System.err.println( "WARNING: " + node.getNodeType() + " node has invalid region: " + region ); + System.err.println( String.format( "WARNING: %s \"%s\" has invalid region: %s", node.getNodeType(), name, region ) ); return null; } Token token = new Token( toPos( region.getBeginLine(), region.getBeginColumn() ), - toPos( region.getEndLine(), region.getEndColumn() ) + toPos( region.getEndLine(), region.getEndColumn() ), + m_source ); if( token.start == 0 ) { // DEBUG - //System.err.println( "WARNING: " + node.getNodeType() + " node has invalid start: " + region ); + System.err.println( String.format( "WARNING: %s \"%s\" has invalid start: %s", node.getNodeType(), name, region ) ); return null; } + // DEBUG + //System.out.println( String.format( "%s \"%s\" region: %s", node.getNodeType(), name, region ) ); + + /* TODO: double check that we still need this // for tokens representing inner classes, make sure we only get the simple name - int pos = node.toString().lastIndexOf( '$' ); + int pos = node.getText().lastIndexOf( '$' ); if( pos >= 0 ) { token.end -= pos + 1; } + */ return token; } - public void addReference( AstNode node, EntryReference deobfReference ) + 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 ); } @@ -102,7 +117,7 @@ public class SourceIndex Token token = getToken( node ); if( token != null ) { - EntryReference reference = new EntryReference( deobfEntry ); + EntryReference reference = new EntryReference( deobfEntry, token.text ); m_tokenToReference.put( token, reference ); m_referenceToTokens.put( reference, token ); m_declarationToToken.put( deobfEntry, token ); @@ -111,7 +126,7 @@ public class SourceIndex public Token getReferenceToken( int pos ) { - Token token = m_tokenToReference.floorKey( new Token( pos, pos ) ); + Token token = m_tokenToReference.floorKey( new Token( pos, pos, null ) ); if( token != null && token.contains( pos ) ) { return token; diff --git a/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java index f307c11..b883087 100644 --- a/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java +++ b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java @@ -33,7 +33,6 @@ import cuchaz.enigma.mapping.ArgumentEntry; import cuchaz.enigma.mapping.BehaviorEntry; import cuchaz.enigma.mapping.ClassEntry; import cuchaz.enigma.mapping.ConstructorEntry; -import cuchaz.enigma.mapping.Entry; import cuchaz.enigma.mapping.FieldEntry; import cuchaz.enigma.mapping.MethodEntry; @@ -100,10 +99,7 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor } if( tokenNode != null ) { - index.addReference( - tokenNode, - new EntryReference( behaviorEntry, m_behaviorEntry ) - ); + index.addReference( tokenNode, behaviorEntry, m_behaviorEntry ); } } @@ -124,10 +120,7 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); FieldEntry fieldEntry = new FieldEntry( classEntry, ref.getName() ); - index.addReference( - node.getMemberNameToken(), - new EntryReference( fieldEntry, m_behaviorEntry ) - ); + index.addReference( node.getMemberNameToken(), fieldEntry, m_behaviorEntry ); } return recurse( node, index ); @@ -140,10 +133,7 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor if( node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY ) { ClassEntry classEntry = new ClassEntry( ref.getInternalName() ); - index.addReference( - node.getIdentifierToken(), - new EntryReference( classEntry, m_behaviorEntry ) - ); + index.addReference( node.getIdentifierToken(), classEntry, m_behaviorEntry ); } return recurse( node, index ); @@ -178,10 +168,7 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor { ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); FieldEntry fieldEntry = new FieldEntry( classEntry, ref.getName() ); - index.addReference( - node.getIdentifierToken(), - new EntryReference( fieldEntry, m_behaviorEntry ) - ); + index.addReference( node.getIdentifierToken(), fieldEntry, m_behaviorEntry ); } return recurse( node, index ); @@ -197,10 +184,8 @@ public class SourceIndexBehaviorVisitor extends SourceIndexVisitor ConstructorEntry constructorEntry = new ConstructorEntry( classEntry, ref.getSignature() ); if( node.getType() instanceof SimpleType ) { - index.addReference( - ((SimpleType)node.getType()).getIdentifierToken(), - new EntryReference( constructorEntry, m_behaviorEntry ) - ); + SimpleType simpleTypeNode = (SimpleType)node.getType(); + index.addReference( simpleTypeNode.getIdentifierToken(), constructorEntry, m_behaviorEntry ); } } diff --git a/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java index 5d8a383..fc8cd66 100644 --- a/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java +++ b/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java @@ -28,7 +28,6 @@ import cuchaz.enigma.mapping.BehaviorEntry; import cuchaz.enigma.mapping.BehaviorEntryFactory; import cuchaz.enigma.mapping.ClassEntry; import cuchaz.enigma.mapping.ConstructorEntry; -import cuchaz.enigma.mapping.Entry; import cuchaz.enigma.mapping.FieldEntry; public class SourceIndexClassVisitor extends SourceIndexVisitor @@ -63,10 +62,7 @@ public class SourceIndexClassVisitor extends SourceIndexVisitor if( node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY ) { ClassEntry classEntry = new ClassEntry( ref.getInternalName() ); - index.addReference( - node.getIdentifierToken(), - new EntryReference( classEntry, m_classEntry ) - ); + index.addReference( node.getIdentifierToken(), classEntry, m_classEntry ); } return recurse( node, index ); diff --git a/src/cuchaz/enigma/analysis/Token.java b/src/cuchaz/enigma/analysis/Token.java index d0f2b70..5e70db7 100644 --- a/src/cuchaz/enigma/analysis/Token.java +++ b/src/cuchaz/enigma/analysis/Token.java @@ -14,11 +14,21 @@ 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, String source ) { this.start = start; this.end = end; + if( source != null ) + { + this.text = source.substring( start, end ); + } } public boolean contains( int pos ) diff --git a/src/cuchaz/enigma/analysis/TreeDumpVisitor.java b/src/cuchaz/enigma/analysis/TreeDumpVisitor.java index 12febef..e6ecb10 100644 --- a/src/cuchaz/enigma/analysis/TreeDumpVisitor.java +++ b/src/cuchaz/enigma/analysis/TreeDumpVisitor.java @@ -141,7 +141,7 @@ public class TreeDumpVisitor implements IAstVisitor { if( node instanceof Identifier ) { - return "\"" + node.getText() + "\""; + return "\"" + ((Identifier)node).getName() + "\""; } return ""; } -- cgit v1.2.3