Clover Coverage Report - Checkstyle
Coverage timestamp: Fri May 9 2008 10:48:13 EST
../../../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
51   232   32   5.1
32   128   0.63   5
10     3.2  
2    
 
  AbstractSuperCheck       Line # 38 88.1% 0.88095236
29.32 46 28 28 0.61
  AbstractSuperCheck.MethodNode       Line # 46 100% 1.0
4 5 4 4 0.8
 
  (2)
 
1    ////////////////////////////////////////////////////////////////////////////////
2    // checkstyle: Checks Java source code for adherence to a set of rules.
3    // Copyright (C) 2001-2005 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   
20    package com.puppycrawl.tools.checkstyle.checks.coding;
21   
22    import java.util.LinkedList;
23   
24    import antlr.collections.AST;
25   
26    import com.puppycrawl.tools.checkstyle.api.Check;
27    import com.puppycrawl.tools.checkstyle.api.DetailAST;
28    import com.puppycrawl.tools.checkstyle.api.ScopeUtils;
29    import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30   
31    /**
32    * <p>
33    * Abstract class for checking that an overriding method with no parameters
34    * invokes the super method.
35    * </p>
36    * @author Rick Giles
37    */
 
38    public abstract class AbstractSuperCheck
39    extends Check
40    {
41    /**
42    * Stack node for a method definition and a record of
43    * whether the method has a call to the super method.
44    * @author Rick Giles
45    */
 
46    private class MethodNode
47    {
48    /** method definition */
49    private DetailAST mMethod;
50   
51    /** true if the overriding method calls the super method */
52    private boolean mCallsSuper;
53   
54    /**
55    * Constructs a stack node for a method definition.
56    * @param aAST AST for the method definition.
57    */
 
58  10 toggle public MethodNode(DetailAST aAST)
59    {
60  10 mMethod = aAST;
61  10 mCallsSuper = false;
62    }
63   
64    /**
65    * Records that the overriding method has a call to the super method.
66    */
 
67  5 toggle public void setCallsSuper()
68    {
69  5 mCallsSuper = true;
70    }
71   
72    /**
73    * Determines whether the overriding method has a call to the super
74    * method.
75    * @return true if the overriding method has a call to the super
76    * method.
77    */
 
78  10 toggle public boolean getCallsSuper()
79    {
80  10 return mCallsSuper;
81    }
82   
83    /**
84    * Returns the overriding method definition AST.
85    * @return the overriding method definition AST.
86    */
 
87  5 toggle public DetailAST getMethod()
88    {
89  5 return mMethod;
90    }
91    }
92   
93    /** stack of methods */
94    private final LinkedList mMethodStack = new LinkedList();
95   
96    /** {@inheritDoc} */
 
97  2 toggle public int[] getDefaultTokens()
98    {
99  2 return new int[] {
100    TokenTypes.METHOD_DEF,
101    TokenTypes.LITERAL_SUPER,
102    };
103    }
104   
105    /**
106    * Returns the name of the overriding method.
107    * @return the name of the overriding method.
108    */
109    protected abstract String getMethodName();
110   
111    /**
112    * {@inheritDoc}
113    */
 
114  2 toggle public void beginTree(DetailAST aRootAST)
115    {
116  2 mMethodStack.clear();
117    }
118   
119    /**
120    *
121    * {@inheritDoc}
122    */
 
123  27 toggle public void visitToken(DetailAST aAST)
124    {
125  27 if (isOverridingMethod(aAST)) {
126  10 mMethodStack.add(new MethodNode(aAST));
127    }
128  17 else if (isSuperCall(aAST)) {
129  5 final MethodNode methodNode = (MethodNode) mMethodStack.getLast();
130  5 methodNode.setCallsSuper();
131    }
132    }
133   
134    /**
135    * Determines whether a 'super' literal is a call to the super method
136    * for this check.
137    * @param aAST the AST node of a 'super' literal.
138    * @return true if aAST is a call to the super method
139    * for this check.
140    */
 
141  17 toggle private boolean isSuperCall(DetailAST aAST)
142    {
143  17 if (aAST.getType() != TokenTypes.LITERAL_SUPER) {
144  4 return false;
145    }
146    // dot operator?
147  13 DetailAST parent = aAST.getParent();
148  13 if ((parent == null) || (parent.getType() != TokenTypes.DOT)) {
149  0 return false;
150    }
151   
152    // same name of method
153  13 AST sibling = aAST.getNextSibling();
154    // ignore type parameters
155  13 if ((sibling != null)
156    && (sibling.getType() == TokenTypes.TYPE_ARGUMENTS))
157    {
158  1 sibling = sibling.getNextSibling();
159    }
160  13 if ((sibling == null) || (sibling.getType() != TokenTypes.IDENT)) {
161  0 return false;
162