From 044f109bfa9ae55430456b1cf21ed56a3902bff2 Mon Sep 17 00:00:00 2001 From: jeff Date: Sun, 10 Aug 2014 16:58:49 -0400 Subject: fixed recognition of static initializers fixed identifier off-by-one error --- src/cuchaz/enigma/analysis/SourceIndexVisitor.java | 271 +++++++++++---------- 1 file changed, 138 insertions(+), 133 deletions(-) (limited to 'src/cuchaz/enigma/analysis/SourceIndexVisitor.java') diff --git a/src/cuchaz/enigma/analysis/SourceIndexVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexVisitor.java index 2a26c85..5a64e4e 100644 --- a/src/cuchaz/enigma/analysis/SourceIndexVisitor.java +++ b/src/cuchaz/enigma/analysis/SourceIndexVisitor.java @@ -42,7 +42,6 @@ import com.strobel.decompiler.languages.java.ast.ContinueStatement; import com.strobel.decompiler.languages.java.ast.DoWhileStatement; import com.strobel.decompiler.languages.java.ast.EmptyStatement; import com.strobel.decompiler.languages.java.ast.EnumValueDeclaration; -import com.strobel.decompiler.languages.java.ast.Expression; import com.strobel.decompiler.languages.java.ast.ExpressionStatement; import com.strobel.decompiler.languages.java.ast.FieldDeclaration; import com.strobel.decompiler.languages.java.ast.ForEachStatement; @@ -101,289 +100,344 @@ import cuchaz.enigma.mapping.MethodEntry; public class SourceIndexVisitor implements IAstVisitor { @Override - public Void visitComment( Comment node, SourceIndex index ) + public Void visitInvocationExpression( InvocationExpression node, SourceIndex index ) { + MemberReference ref = node.getUserData( Keys.MEMBER_REFERENCE ); + ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); + MethodEntry methodEntry = new MethodEntry( classEntry, ref.getName(), ref.getSignature() ); + if( node.getTarget() instanceof MemberReferenceExpression ) + { + index.add( ((MemberReferenceExpression)node.getTarget()).getMemberNameToken(), methodEntry ); + } + return recurse( node, index ); } @Override - public Void visitPatternPlaceholder( AstNode node, Pattern pattern, SourceIndex index ) + public Void visitMemberReferenceExpression( MemberReferenceExpression 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.add( node.getMemberNameToken(), fieldEntry ); + } + return recurse( node, index ); } @Override - public Void visitInvocationExpression( InvocationExpression node, SourceIndex index ) + public Void visitSimpleType( SimpleType node, SourceIndex index ) { - MemberReference ref = node.getUserData( Keys.MEMBER_REFERENCE ); - ClassEntry classEntry = new ClassEntry( ref.getDeclaringType().getInternalName() ); - MethodEntry methodEntry = new MethodEntry( classEntry, ref.getName(), ref.getSignature() ); - if( node.getTarget() instanceof MemberReferenceExpression ) + TypeReference ref = node.getUserData( Keys.TYPE_REFERENCE ); + if( node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY ) { - index.add( ((MemberReferenceExpression)node.getTarget()).getMemberNameToken(), methodEntry ); + index.add( node.getIdentifierToken(), new ClassEntry( ref.getInternalName() ) ); } return recurse( node, index ); } @Override - public Void visitTypeReference( TypeReferenceExpression node, SourceIndex index ) + public Void visitMethodDeclaration( MethodDeclaration node, SourceIndex index ) { + MethodDefinition def = node.getUserData( Keys.METHOD_DEFINITION ); + + // static initializers don't have identifier tokens + if( !def.getName().equals( "" ) ) + { + ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); + MethodEntry methodEntry = new MethodEntry( classEntry, def.getName(), def.getSignature() ); + index.add( node.getNameToken(), methodEntry ); + } + return recurse( node, index ); } @Override - public Void visitJavaTokenNode( JavaTokenNode node, SourceIndex index ) + public Void visitConstructorDeclaration( ConstructorDeclaration node, SourceIndex index ) { + MethodDefinition def = node.getUserData( Keys.METHOD_DEFINITION ); + index.add( node.getNameToken(), new ClassEntry( def.getDeclaringType().getInternalName() ) ); + return recurse( node, index ); } @Override - public Void visitMemberReferenceExpression( MemberReferenceExpression node, SourceIndex index ) + public Void visitParameterDeclaration( ParameterDeclaration 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.add( node.getMemberNameToken(), fieldEntry ); - } + ParameterDefinition def = node.getUserData( Keys.PARAMETER_DEFINITION ); + ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); + MethodDefinition methodDef = (MethodDefinition)def.getMethod(); + MethodEntry methodEntry = new MethodEntry( classEntry, methodDef.getName(), methodDef.getSignature() ); + ArgumentEntry argumentEntry = new ArgumentEntry( methodEntry, def.getPosition(), def.getName() ); + index.add( node.getNameToken(), argumentEntry ); return recurse( node, index ); } @Override - public Void visitIdentifier( Identifier node, SourceIndex index ) + 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.add( variable.getNameToken(), fieldEntry ); + return recurse( node, index ); } @Override - public Void visitNullReferenceExpression( NullReferenceExpression node, SourceIndex index ) + public Void visitTypeDeclaration( TypeDeclaration node, SourceIndex index ) { + TypeDefinition def = node.getUserData( Keys.TYPE_DEFINITION ); + index.add( node.getNameToken(), new ClassEntry( def.getInternalName() ) ); + return recurse( node, index ); } + private Void recurse( AstNode node, SourceIndex index ) + { + // TEMP: show the tree + System.out.println( getIndent( node ) + node.getClass().getSimpleName() + dumpUserData( node ) + " " + node.getRegion() ); + + for( final AstNode child : node.getChildren() ) + { + child.acceptVisitor( this, index ); + } + return null; + } + + 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 ) ); + } + } + return buf.toString(); + } + + private String getIndent( AstNode node ) + { + StringBuilder buf = new StringBuilder(); + int depth = getDepth( node ); + for( int i = 0; i < depth; i++ ) + { + buf.append( "\t" ); + } + return buf.toString(); + } + + private int getDepth( AstNode node ) + { + int depth = -1; + while( node != null ) + { + depth++; + node = node.getParent(); + } + return depth; + } + + // OVERRIDES WE DON'T CARE ABOUT + @Override - public Void visitThisReferenceExpression( ThisReferenceExpression node, SourceIndex index ) + public Void visitComment( Comment node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitSuperReferenceExpression( SuperReferenceExpression node, SourceIndex index ) + public Void visitPatternPlaceholder( AstNode node, Pattern pattern, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitClassOfExpression( ClassOfExpression node, SourceIndex index ) + public Void visitTypeReference( TypeReferenceExpression node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitBlockStatement( BlockStatement node, SourceIndex index ) + public Void visitJavaTokenNode( JavaTokenNode node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitExpressionStatement( ExpressionStatement node, SourceIndex index ) + public Void visitIdentifier( Identifier node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitBreakStatement( BreakStatement node, SourceIndex index ) + public Void visitNullReferenceExpression( NullReferenceExpression node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitContinueStatement( ContinueStatement node, SourceIndex index ) + public Void visitThisReferenceExpression( ThisReferenceExpression node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitDoWhileStatement( DoWhileStatement node, SourceIndex index ) + public Void visitSuperReferenceExpression( SuperReferenceExpression node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitEmptyStatement( EmptyStatement node, SourceIndex index ) + public Void visitClassOfExpression( ClassOfExpression node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitIfElseStatement( IfElseStatement node, SourceIndex index ) + public Void visitBlockStatement( BlockStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitLabelStatement( LabelStatement node, SourceIndex index ) + public Void visitExpressionStatement( ExpressionStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitLabeledStatement( LabeledStatement node, SourceIndex index ) + public Void visitBreakStatement( BreakStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitReturnStatement( ReturnStatement node, SourceIndex index ) + public Void visitContinueStatement( ContinueStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitSwitchStatement( SwitchStatement node, SourceIndex index ) + public Void visitDoWhileStatement( DoWhileStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitSwitchSection( SwitchSection node, SourceIndex index ) + public Void visitEmptyStatement( EmptyStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitCaseLabel( CaseLabel node, SourceIndex index ) + public Void visitIfElseStatement( IfElseStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitThrowStatement( ThrowStatement node, SourceIndex index ) + public Void visitLabelStatement( LabelStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitCatchClause( CatchClause node, SourceIndex index ) + public Void visitLabeledStatement( LabeledStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitAnnotation( Annotation node, SourceIndex index ) + public Void visitReturnStatement( ReturnStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitNewLine( NewLineNode node, SourceIndex index ) + public Void visitSwitchStatement( SwitchStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitVariableDeclaration( VariableDeclarationStatement node, SourceIndex index ) + public Void visitSwitchSection( SwitchSection node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitVariableInitializer( VariableInitializer node, SourceIndex index ) + public Void visitCaseLabel( CaseLabel node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitText( TextNode node, SourceIndex index ) + public Void visitThrowStatement( ThrowStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitImportDeclaration( ImportDeclaration node, SourceIndex index ) + public Void visitCatchClause( CatchClause node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitSimpleType( SimpleType node, SourceIndex index ) + public Void visitAnnotation( Annotation node, SourceIndex index ) { - TypeReference ref = node.getUserData( Keys.TYPE_REFERENCE ); - if( node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY ) - { - index.add( node.getIdentifierToken(), new ClassEntry( ref.getInternalName() ) ); - } - return recurse( node, index ); } @Override - public Void visitMethodDeclaration( MethodDeclaration node, SourceIndex index ) + public Void visitNewLine( NewLineNode node, SourceIndex index ) { - MethodDefinition def = node.getUserData( Keys.METHOD_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); - MethodEntry methodEntry = new MethodEntry( classEntry, def.getName(), def.getSignature() ); - index.add( node.getNameToken(), methodEntry ); - return recurse( node, index ); } @Override - public Void visitInitializerBlock( InstanceInitializer node, SourceIndex index ) + public Void visitVariableDeclaration( VariableDeclarationStatement node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitConstructorDeclaration( ConstructorDeclaration node, SourceIndex index ) + public Void visitVariableInitializer( VariableInitializer node, SourceIndex index ) { - MethodDefinition def = node.getUserData( Keys.METHOD_DEFINITION ); - index.add( node.getNameToken(), new ClassEntry( def.getDeclaringType().getInternalName() ) ); - return recurse( node, index ); } @Override - public Void visitTypeParameterDeclaration( TypeParameterDeclaration node, SourceIndex index ) + public Void visitText( TextNode node, SourceIndex index ) { return recurse( node, index ); } @Override - public Void visitParameterDeclaration( ParameterDeclaration node, SourceIndex index ) + public Void visitImportDeclaration( ImportDeclaration node, SourceIndex index ) { - ParameterDefinition def = node.getUserData( Keys.PARAMETER_DEFINITION ); - ClassEntry classEntry = new ClassEntry( def.getDeclaringType().getInternalName() ); - MethodDefinition methodDef = (MethodDefinition)def.getMethod(); - MethodEntry methodEntry = new MethodEntry( classEntry, methodDef.getName(), methodDef.getSignature() ); - ArgumentEntry argumentEntry = new ArgumentEntry( methodEntry, def.getPosition(), def.getName() ); - index.add( node.getNameToken(), argumentEntry ); - return recurse( node, index ); } @Override - public Void visitFieldDeclaration( FieldDeclaration node, SourceIndex index ) + public Void visitInitializerBlock( InstanceInitializer 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.add( variable.getNameToken(), fieldEntry ); - return recurse( node, index ); } @Override - public Void visitTypeDeclaration( TypeDeclaration node, SourceIndex index ) + public Void visitTypeParameterDeclaration( TypeParameterDeclaration node, SourceIndex index ) { - TypeDefinition def = node.getUserData( Keys.TYPE_DEFINITION ); - index.add( node.getNameToken(), new ClassEntry( def.getInternalName() ) ); - return recurse( node, index ); } @@ -450,7 +504,6 @@ public class SourceIndexVisitor implements IAstVisitor @Override public Void visitIdentifierExpression( IdentifierExpression node, SourceIndex index ) { - // TODO return recurse( node, index ); } @@ -567,52 +620,4 @@ public class SourceIndexVisitor implements IAstVisitor { return recurse( node, index ); } - - private Void recurse( AstNode node, SourceIndex index ) - { - // TEMP: show the tree - System.out.println( getIndent( node ) + node.getClass().getSimpleName() + dumpUserData( node ) + " " + node.getRegion() ); - - for( final AstNode child : node.getChildren() ) - { - child.acceptVisitor( this, index ); - } - return null; - } - - 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 ) ); - } - } - return buf.toString(); - } - - private String getIndent( AstNode node ) - { - StringBuilder buf = new StringBuilder(); - int depth = getDepth( node ); - for( int i = 0; i < depth; i++ ) - { - buf.append( "\t" ); - } - return buf.toString(); - } - - private int getDepth( AstNode node ) - { - int depth = -1; - while( node != null ) - { - depth++; - node = node.getParent(); - } - return depth; - } } -- cgit v1.2.3