Coverage Report - com.puppycrawl.tools.checkstyle.api.DetailAST
 
Classes in this File Line Coverage Branch Coverage Complexity
DetailAST
100%
92/92
100%
44/44
2.3
 
 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  
 import java.util.BitSet;
 22  
 
 23  
 import antlr.CommonAST;
 24  
 import antlr.Token;
 25  
 import antlr.collections.AST;
 26  
 
 27  
 /**
 28  
  * An extension of the CommonAST that records the line and column
 29  
  * number.  The idea was taken from <a target="_top"
 30  
  * href="http://www.jguru.com/jguru/faq/view.jsp?EID=62654">Java Guru
 31  
  * FAQ: How can I include line numbers in automatically generated
 32  
  * ASTs?</a>.
 33  
  * @author Oliver Burn
 34  
  * @author lkuehne
 35  
  * @version 1.0
 36  
  * @see <a href="http://www.antlr.org/">ANTLR Website</a>
 37  
  */
 38  1812047
 public final class DetailAST extends CommonAST
 39  
 {
 40  
     /** For Serialisation that will never happen. */
 41  
     private static final long serialVersionUID = -2580884815577559874L;
 42  
 
 43  
     /** constant to indicate if not calculated the child count */
 44  
     private static final int NOT_INITIALIZED = Integer.MIN_VALUE;
 45  
 
 46  
     /** the line number **/
 47  295396
     private int mLineNo = NOT_INITIALIZED;
 48  
     /** the column number **/
 49  295396
     private int mColumnNo = NOT_INITIALIZED;
 50  
 
 51  
     /** number of children */
 52  295396
     private int mChildCount = NOT_INITIALIZED;
 53  
     /** the parent token */
 54  
     private DetailAST mParent;
 55  
     /** previous sibling */
 56  
     private DetailAST mPreviousSibling;
 57  
 
 58  
     /**
 59  
      * All token types in this branch.
 60  
      * Token 'x' (where x is an int) is in this branch
 61  
      * if mBranchTokenTypes.get(x) is true.
 62  
      */
 63  
     private BitSet mBranchTokenTypes;
 64  
 
 65  
     @Override
 66  
     public void initialize(Token aTok)
 67  
     {
 68  194382
         super.initialize(aTok);
 69  194382
         mLineNo = aTok.getLine();
 70  194382
         mColumnNo = aTok.getColumn() - 1; // expect columns to start @ 0
 71  194382
     }
 72  
 
 73  
     @Override
 74  
     public void initialize(AST aAST)
 75  
     {
 76  25497
         final DetailAST da = (DetailAST) aAST;
 77  25497
         setText(da.getText());
 78  25497
         setType(da.getType());
 79  25497
         mLineNo = da.getLineNo();
 80  25497
         mColumnNo = da.getColumnNo();
 81  25497
     }
 82  
 
 83  
     @Override
 84  
     public void setFirstChild(AST aAST)
 85  
     {
 86  186790
         mChildCount = NOT_INITIALIZED;
 87  186790
         super.setFirstChild(aAST);
 88  186790
         if (aAST != null) {
 89  92372
             ((DetailAST) aAST).setParent(this);
 90  
         }
 91  186790
     }
 92  
 
 93  
     @Override
 94  
     public void setNextSibling(AST aAST)
 95  
     {
 96  160954
         super.setNextSibling(aAST);
 97  160954
         if ((aAST != null) && (mParent != null)) {
 98  109774
             ((DetailAST) aAST).setParent(mParent);
 99  
         }
 100  160954
         if (aAST != null) {
 101  145592
             ((DetailAST) aAST).setPreviousSibling(this);
 102  
         }
 103  160954
     }
 104  
 
 105  
     /**
 106  
      * Sets previous sibling.
 107  
      * @param aAST a previous sibling
 108  
      */
 109  
     void setPreviousSibling(DetailAST aAST)
 110  
     {
 111  190829
         mPreviousSibling = aAST;
 112  190829
     }
 113  
 
 114  
     @Override
 115  
     public void addChild(AST aAST)
 116  
     {
 117  46622
         super.addChild(aAST);
 118  46622
         if (aAST != null) {
 119  20651
             ((DetailAST) aAST).setParent(this);
 120  20651
             (getFirstChild()).setParent(this);
 121  
         }
 122  46622
     }
 123  
 
 124  
     /**
 125  
      * Returns the number of child nodes one level below this node. That is is
 126  
      * does not recurse down the tree.
 127  
      * @return the number of child nodes
 128  
      */
 129  
     public int getChildCount()
 130  
     {
 131  
         // lazy init
 132  495
         if (mChildCount == NOT_INITIALIZED) {
 133  411
             mChildCount = 0;
 134  411
             AST child = getFirstChild();
 135  
 
 136  1011
             while (child != null) {
 137  600
                 mChildCount += 1;
 138  600
                 child = child.getNextSibling();
 139  
             }
 140  
         }
 141  495
         return mChildCount;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Set the parent token.
 146  
      * @param aParent the parent token
 147  
      */
 148  
     // TODO: should be private but that breaks the DetailASTTest
 149  
     // until we manage parent in DetailAST instead of externally
 150  
     void setParent(DetailAST aParent)
 151  
     {
 152  
         // TODO: Check visibility, could be private
 153  
         // if set in setFirstChild() and friends
 154  288688
         mParent = aParent;
 155  288688
         final DetailAST nextSibling = getNextSibling();
 156  288688
         if (nextSibling != null) {
 157  45237
             nextSibling.setParent(aParent);
 158  45237
             nextSibling.setPreviousSibling(this);
 159  
         }
 160  288688
     }
 161  
 
 162  
     /**
 163  
      * Returns the parent token.
 164  
      * @return the parent token
 165  
      */
 166  
     public DetailAST getParent()
 167  
     {
 168  163194
         return mParent;
 169  
     }
 170  
 
 171  
     /** @return the line number **/
 172  
     public int getLineNo()
 173  
     {
 174  638244
         if (mLineNo == NOT_INITIALIZED) {
 175  
             // an inner AST that has been initialized
 176  
             // with initialize(String text)
 177  220931
             final DetailAST child = getFirstChild();
 178  220931
             final DetailAST sibling = getNextSibling();
 179  220931
             if (child != null) {
 180  183980
                 return child.getLineNo();
 181  
             }
 182  36951
             else if (sibling != null) {
 183  33740
                 return sibling.getLineNo();
 184  
             }
 185  
         }
 186  420524
         return mLineNo;
 187  
     }
 188  
 
 189  
     /** @return the column number **/
 190  
     public int getColumnNo()
 191  
     {
 192  589725
         if (mColumnNo == NOT_INITIALIZED) {
 193  
             // an inner AST that has been initialized
 194  
             // with initialize(String text)
 195  206165
             final DetailAST child = getFirstChild();
 196  206165
             final DetailAST sibling = getNextSibling();
 197  206165
             if (child != null) {
 198  171697
                 return child.getColumnNo();
 199  
             }
 200  34468
             else if (sibling != null) {
 201  31257
                 return sibling.getColumnNo();
 202  
             }
 203  
         }
 204  386771
         return mColumnNo;
 205  
     }
 206  
 
 207  
     /** @return the last child node */
 208  
     public DetailAST getLastChild()
 209  
     {
 210  648
         DetailAST ast = getFirstChild();
 211  1444
         while ((ast != null) && (ast.getNextSibling() != null)) {
 212  796
             ast = ast.getNextSibling();
 213  
         }
 214  648
         return ast;
 215  
     }
 216  
 
 217  
     /**
 218  
      * @return the token types that occur in the branch as a sorted set.
 219  
      */
 220  
     private BitSet getBranchTokenTypes()
 221  
     {
 222  
         // lazy init
 223  10504
         if (mBranchTokenTypes == null) {
 224  
 
 225  7791
             mBranchTokenTypes = new BitSet();
 226  7791
             mBranchTokenTypes.set(getType());
 227  
 
 228  
             // add union of all childs
 229  7791
             DetailAST child = getFirstChild();
 230  13799
             while (child != null) {
 231  6008
                 final BitSet childTypes = child.getBranchTokenTypes();
 232  6008
                 mBranchTokenTypes.or(childTypes);
 233  
 
 234  6008
                 child = child.getNextSibling();
 235  6008
             }
 236  
         }
 237  10504
         return mBranchTokenTypes;
 238  
     }
 239  
 
 240  
     /**
 241  
      * Checks if this branch of the parse tree contains a token
 242  
      * of the provided type.
 243  
      * @param aType a TokenType
 244  
      * @return true if and only if this branch (including this node)
 245  
      * contains a token of type <code>aType</code>.
 246  
      */
 247  
     public boolean branchContains(int aType)
 248  
     {
 249  4496
         return getBranchTokenTypes().get(aType);
 250  
     }
 251  
 
 252  
     /**
 253  
      * Returns the number of direct child tokens that have the specified type.
 254  
      * @param aType the token type to match
 255  
      * @return the number of matching token
 256  
      */
 257  
     public int getChildCount(int aType)
 258  
     {
 259  114
         int count = 0;
 260  523
         for (AST i = getFirstChild(); i != null; i = i.getNextSibling()) {
 261  409
             if (i.getType() == aType) {
 262  99
                 count++;
 263  
             }
 264  
         }
 265  114
         return count;
 266  
     }
 267  
 
 268  
     /**
 269  
      * Returns the previous sibling or null if no such sibling exists.
 270  
      * @return the previous sibling or null if no such sibling exists.
 271  
      */
 272  
     public DetailAST getPreviousSibling()
 273  
     {
 274  52717
         return mPreviousSibling;
 275  
     }
 276  
 
 277  
     /**
 278  
      * Returns the first child token that makes a specified type.
 279  
      * @param aType the token type to match
 280  
      * @return the matching token, or null if no match
 281  
      */
 282  
     public DetailAST findFirstToken(int aType)
 283  
     {
 284  24157
         DetailAST retVal = null;
 285  79579
         for (DetailAST i = getFirstChild(); i != null; i = i.getNextSibling()) {
 286  76287
             if (i.getType() == aType) {
 287  20865
                 retVal = i;
 288  20865
                 break;
 289  
             }
 290  
         }
 291  24157
         return retVal;
 292  
     }
 293  
 
 294  
     @Override
 295  
     public String toString()
 296  
     {
 297  339286
         return super.toString() + "[" + getLineNo() + "x" + getColumnNo() + "]";
 298  
     }
 299  
 
 300  
     @Override
 301  
     public DetailAST getNextSibling()
 302  
     {
 303  2351215
         return (DetailAST) super.getNextSibling();
 304  
     }
 305  
 
 306  
     @Override
 307  
     public DetailAST getFirstChild()
 308  
     {
 309  1011917
         return (DetailAST) super.getFirstChild();
 310  
     }
 311  
 }