@ VisibleForTesting static String getArgumentName ( ExpressionTree expressionTree ) { switch ( expressionTree . getKind ( ) ) { case MEMBER_SELECT : return ( ( MemberSelectTree ) expressionTree ) . getIdentifier ( ) . toString ( ) ; case NULL_LITERAL : // null could match anything pretty well return NAME_NULL ; case IDENTIFIER : IdentifierTree idTree = ( IdentifierTree ) expressionTree ; if ( idTree . getName ( ) . contentEquals ( "this" ) ) { // for the 'this' keyword the argument name is the name of the object's class Symbol sym = ASTHelpers . getSymbol ( idTree ) ; return sym != null ? getClassName ( ASTHelpers . enclosingClass ( sym ) ) : NAME_NOT_PRESENT ; } else { // if we have a variable, just extract its name return idTree . getName ( ) . toString ( ) ; } case METHOD_INVOCATION : MethodInvocationTree methodInvocationTree = ( MethodInvocationTree ) expressionTree ; MethodSymbol methodSym = ASTHelpers . getSymbol ( methodInvocationTree ) ; if ( methodSym != null ) { String name = methodSym . getSimpleName ( ) . toString ( ) ; List < String > terms = NamingConventions . splitToLowercaseTerms ( name ) ; String firstTerm = Iterables . getFirst ( terms , null ) ; if ( METHODNAME_PREFIXES_TO_REMOVE . contains ( firstTerm ) ) { if ( terms . size ( ) == 1 ) { ExpressionTree receiver = ASTHelpers . getReceiver ( methodInvocationTree ) ; if ( receiver == null ) { return getClassName ( ASTHelpers . enclosingClass ( methodSym ) ) ; } // recursively try to get a name from the receiver return getArgumentName ( receiver ) ; } else { return name . substring ( firstTerm . length ( ) ) ; } } else { return name ; } } else { return NAME_NOT_PRESENT ; } case NEW_CLASS : MethodSymbol constructorSym = ASTHelpers . getSymbol ( ( NewClassTree ) expressionTree ) ; return constructorSym != null && constructorSym . owner != null ? getClassName ( ( ClassSymbol ) constructorSym . owner ) : NAME_NOT_PRESENT ; default : return NAME_NOT_PRESENT ; } }
Extract the name from an argument .