Coverage Report - com.puppycrawl.tools.checkstyle.api.FullIdent
 
Classes in this File Line Coverage Branch Coverage Complexity
FullIdent
96%
30/31
83%
10/12
1.7
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2014  Oliver Burn
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 package com.puppycrawl.tools.checkstyle.api;
 20  
 
 21  
 /**
 22  
  * Represents a full identifier, including dots, with associated
 23  
  * position information.
 24  
  *
 25  
  * <p>
 26  
  * Identifiers such as <code>java.util.HashMap</code> are spread across
 27  
  * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes).
 28  
  * A FullIdent represents the whole String (excluding any intermediate
 29  
  * whitespace), which is often easier to work with in Checks.
 30  
  * </p>
 31  
  *
 32  
  * @author Oliver Burn
 33  
  * @see TokenTypes#DOT
 34  
  * @see TokenTypes#IDENT
 35  
  **/
 36  
 public final class FullIdent
 37  
 {
 38  
     /** the string **/
 39  1254
     private final StringBuffer mBuffer = new StringBuffer();
 40  
     /** the line number **/
 41  
     private int mLineNo;
 42  
     /** the column number **/
 43  
     private int mColNo;
 44  
 
 45  
     /** hide default constructor */
 46  
     private FullIdent()
 47  1254
     {
 48  1254
     }
 49  
 
 50  
     /** @return the text **/
 51  
     public String getText()
 52  
     {
 53  2091
         return mBuffer.toString();
 54  
     }
 55  
 
 56  
     /** @return the line number **/
 57  
     public int getLineNo()
 58  
     {
 59  357
         return mLineNo;
 60  
     }
 61  
 
 62  
     /** @return the column number **/
 63  
     public int getColumnNo()
 64  
     {
 65  214
         return mColNo;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Append the specified text.
 70  
      * @param aText the text to append
 71  
      */
 72  
     private void append(String aText)
 73  
     {
 74  1401
         mBuffer.append(aText);
 75  1401
     }
 76  
 
 77  
     /**
 78  
      * Append the specified token and also recalibrate the first line and
 79  
      * column.
 80  
      * @param aAST the token to append
 81  
      */
 82  
     private void append(DetailAST aAST)
 83  
     {
 84  2618
         mBuffer.append(aAST.getText());
 85  2618
         if (mLineNo == 0) {
 86  1217
             mLineNo = aAST.getLineNo();
 87  
         }
 88  1401
         else if (aAST.getLineNo() > 0) {
 89  1401
             mLineNo = Math.min(mLineNo, aAST.getLineNo());
 90  
         }
 91  
         // TODO: make a function
 92  2618
         if (mColNo == 0) {
 93  1217
             mColNo = aAST.getColumnNo();
 94  
         }
 95  1401
         else if (aAST.getColumnNo() > 0) {
 96  1401
             mColNo = Math.min(mColNo, aAST.getColumnNo());
 97  
         }
 98  2618
     }
 99  
 
 100  
     /**
 101  
      * Creates a new FullIdent starting from the specified node.
 102  
      * @param aAST the node to start from
 103  
      * @return a <code>FullIdent</code> value
 104  
      */
 105  
     public static FullIdent createFullIdent(DetailAST aAST)
 106  
     {
 107  1254
         final FullIdent fi = new FullIdent();
 108  1254
         extractFullIdent(fi, aAST);
 109  1254
         return fi;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Creates a new FullIdent starting from the child of the specified node.
 114  
      * @param aAST the parent node from where to start from
 115  
      * @return a <code>FullIdent</code> value
 116  
      */
 117  
     public static FullIdent createFullIdentBelow(DetailAST aAST)
 118  
     {
 119  286
         return createFullIdent(aAST.getFirstChild());
 120  
     }
 121  
 
 122  
     /**
 123  
      * Recursively extract a FullIdent.
 124  
      *
 125  
      * @param aFull the FullIdent to add to
 126  
      * @param aAST the node to recurse from
 127  
      */
 128  
     private static void extractFullIdent(FullIdent aFull, DetailAST aAST)
 129  
     {
 130  4056
         if (aAST == null) {
 131  37
             return;
 132  
         }
 133  
 
 134  4019
         if (aAST.getType() == TokenTypes.DOT) {
 135  1401
             extractFullIdent(aFull, aAST.getFirstChild());
 136  1401
             aFull.append(".");
 137  1401
             extractFullIdent(
 138  
                 aFull, aAST.getFirstChild().getNextSibling());
 139  
         }
 140  
         else {
 141  2618
             aFull.append(aAST);
 142  
         }
 143  4019
     }
 144  
 
 145  
     @Override
 146  
     public String toString()
 147  
     {
 148  0
         return getText() + "[" + getLineNo() + "x" + getColumnNo() + "]";
 149  
     }
 150  
 
 151  
 }