Coverage Report - com.puppycrawl.tools.checkstyle.api.FullIdent
 
Classes in this File Line Coverage Branch Coverage Complexity
FullIdent
100%
31/31
100%
12/12
1.6
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2018 the original author or authors.
 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  
 
 20  
 package com.puppycrawl.tools.checkstyle.api;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * Represents a full identifier, including dots, with associated
 27  
  * position information.
 28  
  *
 29  
  * <p>
 30  
  * Identifiers such as {@code java.util.HashMap} are spread across
 31  
  * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes).
 32  
  * A FullIdent represents the whole String (excluding any intermediate
 33  
  * whitespace), which is often easier to work with in Checks.
 34  
  * </p>
 35  
  *
 36  
  * @author Oliver Burn
 37  
  * @see TokenTypes#DOT
 38  
  * @see TokenTypes#IDENT
 39  
  **/
 40  
 public final class FullIdent {
 41  
 
 42  
     /** The list holding subsequent elements of identifier. **/
 43  2710
     private final List<String> elements = new ArrayList<>();
 44  
     /** The line number. **/
 45  
     private int lineNo;
 46  
     /** The column number. **/
 47  
     private int columnNo;
 48  
 
 49  
     /** Hide default constructor. */
 50  2710
     private FullIdent() {
 51  2710
     }
 52  
 
 53  
     /**
 54  
      * Creates a new FullIdent starting from the specified node.
 55  
      * @param ast the node to start from
 56  
      * @return a {@code FullIdent} value
 57  
      */
 58  
     public static FullIdent createFullIdent(DetailAST ast) {
 59  2710
         final FullIdent ident = new FullIdent();
 60  2710
         extractFullIdent(ident, ast);
 61  2710
         return ident;
 62  
     }
 63  
 
 64  
     /**
 65  
      * Creates a new FullIdent starting from the child of the specified node.
 66  
      * @param ast the parent node from where to start from
 67  
      * @return a {@code FullIdent} value
 68  
      */
 69  
     public static FullIdent createFullIdentBelow(DetailAST ast) {
 70  886
         return createFullIdent(ast.getFirstChild());
 71  
     }
 72  
 
 73  
     /**
 74  
      * Gets the text.
 75  
      * @return the text
 76  
      */
 77  
     public String getText() {
 78  3929
         return String.join("", elements);
 79  
     }
 80  
 
 81  
     /**
 82  
      * Gets the line number.
 83  
      * @return the line number
 84  
      */
 85  
     public int getLineNo() {
 86  507
         return lineNo;
 87  
     }
 88  
 
 89  
     /**
 90  
      * Gets the column number.
 91  
      * @return the column number
 92  
      */
 93  
     public int getColumnNo() {
 94  198
         return columnNo;
 95  
     }
 96  
 
 97  
     @Override
 98  
     public String toString() {
 99  5
         return String.join("", elements) + "[" + lineNo + "x" + columnNo + "]";
 100  
     }
 101  
 
 102  
     /**
 103  
      * Recursively extract a FullIdent.
 104  
      *
 105  
      * @param full the FullIdent to add to
 106  
      * @param ast the node to recurse from
 107  
      */
 108  
     private static void extractFullIdent(FullIdent full, DetailAST ast) {
 109  12154
         if (ast != null) {
 110  12104
             if (ast.getType() == TokenTypes.DOT) {
 111  4722
                 extractFullIdent(full, ast.getFirstChild());
 112  4722
                 full.append(".");
 113  9444
                 extractFullIdent(
 114  4722
                     full, ast.getFirstChild().getNextSibling());
 115  
             }
 116  
             else {
 117  7382
                 full.append(ast);
 118  
             }
 119  
         }
 120  12154
     }
 121  
 
 122  
     /**
 123  
      * Append the specified text.
 124  
      * @param text the text to append
 125  
      */
 126  
     private void append(String text) {
 127  4722
         elements.add(text);
 128  4722
     }
 129  
 
 130  
     /**
 131  
      * Append the specified token and also recalibrate the first line and
 132  
      * column.
 133  
      * @param ast the token to append
 134  
      */
 135  
     private void append(DetailAST ast) {
 136  7382
         elements.add(ast.getText());
 137  7382
         if (lineNo == 0) {
 138  2660
             lineNo = ast.getLineNo();
 139  
         }
 140  4722
         else if (ast.getLineNo() > 0) {
 141  4721
             lineNo = Math.min(lineNo, ast.getLineNo());
 142  
         }
 143  7382
         if (columnNo == 0) {
 144  2660
             columnNo = ast.getColumnNo();
 145  
         }
 146  4722
         else if (ast.getColumnNo() > 0) {
 147  4721
             columnNo = Math.min(columnNo, ast.getColumnNo());
 148  
         }
 149  7382
     }
 150  
 
 151  
 }