Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DesignForExtensionCheck |
|
| 8.333333333333334;8.333 |
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.checks.design; | |
20 | ||
21 | import com.puppycrawl.tools.checkstyle.api.Check; | |
22 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
23 | import com.puppycrawl.tools.checkstyle.api.Scope; | |
24 | import com.puppycrawl.tools.checkstyle.api.ScopeUtils; | |
25 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
26 | ||
27 | /** | |
28 | * Checks that classes are designed for inheritance. | |
29 | * | |
30 | * <p> | |
31 | * More specifically, it enforces a programming style | |
32 | * where superclasses provide empty "hooks" that can be | |
33 | * implemented by subclasses. | |
34 | * </p> | |
35 | * | |
36 | * <p> | |
37 | * The exact rule is that nonprivate, nonstatic methods in | |
38 | * nonfinal classes (or classes that do not | |
39 | * only have private constructors) must either be | |
40 | * <ul> | |
41 | * <li>abstract or</li> | |
42 | * <li>final or</li> | |
43 | * <li>have an empty implementation</li> | |
44 | * </ul> | |
45 | * </p> | |
46 | * | |
47 | * <p> | |
48 | * This protects superclasses against being broken by | |
49 | * subclasses. The downside is that subclasses are limited | |
50 | * in their flexibility, in particular they cannot prevent | |
51 | * execution of code in the superclass, but that also | |
52 | * means that subclasses can't forget to call their super | |
53 | * method. | |
54 | * </p> | |
55 | * | |
56 | * @author lkuehne | |
57 | */ | |
58 | 1 | public class DesignForExtensionCheck extends Check |
59 | { | |
60 | @Override | |
61 | public int[] getDefaultTokens() | |
62 | { | |
63 | 1 | return new int[] {TokenTypes.METHOD_DEF}; |
64 | } | |
65 | ||
66 | @Override | |
67 | public void visitToken(DetailAST aAST) | |
68 | { | |
69 | // nothing to do for Interfaces | |
70 | 10 | if (ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) { |
71 | 1 | return; |
72 | } | |
73 | ||
74 | // method is ok if it is private or abstract or final | |
75 | 9 | final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS); |
76 | 9 | if (modifiers.branchContains(TokenTypes.LITERAL_PRIVATE) |
77 | || modifiers.branchContains(TokenTypes.ABSTRACT) | |
78 | || modifiers.branchContains(TokenTypes.FINAL) | |
79 | || modifiers.branchContains(TokenTypes.LITERAL_STATIC)) | |
80 | { | |
81 | 2 | return; |
82 | } | |
83 | ||
84 | // method is ok if containing class is not visible in API and | |
85 | // cannot be extended by 3rd parties (bug #884035) | |
86 | 7 | if (!ScopeUtils.getSurroundingScope(aAST).isIn(Scope.PROTECTED)) { |
87 | 3 | return; |
88 | } | |
89 | ||
90 | // method is ok if it is implementation can verified to be empty | |
91 | // Note: native methods don't have impl in java code, so | |
92 | // implementation can be null even if method not abstract | |
93 | 4 | final DetailAST implementation = aAST.findFirstToken(TokenTypes.SLIST); |
94 | 4 | if ((implementation != null) |
95 | && (implementation.getFirstChild().getType() == TokenTypes.RCURLY)) | |
96 | { | |
97 | 2 | return; |
98 | } | |
99 | ||
100 | // check if the containing class can be subclassed | |
101 | 2 | final DetailAST classDef = findContainingClass(aAST); |
102 | 2 | final DetailAST classMods = |
103 | classDef.findFirstToken(TokenTypes.MODIFIERS); | |
104 | 2 | if ((classDef.getType() == TokenTypes.ENUM_DEF) |
105 | || classMods.branchContains(TokenTypes.FINAL)) | |
106 | { | |
107 | 0 | return; |
108 | } | |
109 | ||
110 | // check if subclassing is prevented by having only private ctors | |
111 | 2 | final DetailAST objBlock = classDef.findFirstToken(TokenTypes.OBJBLOCK); |
112 | ||
113 | 2 | boolean hasDefaultConstructor = true; |
114 | 2 | boolean hasExplNonPrivateCtor = false; |
115 | ||
116 | 2 | DetailAST candidate = objBlock.getFirstChild(); |
117 | ||
118 | 24 | while (candidate != null) { |
119 | 22 | if (candidate.getType() == TokenTypes.CTOR_DEF) { |
120 | 0 | hasDefaultConstructor = false; |
121 | ||
122 | 0 | final DetailAST ctorMods = |
123 | candidate.findFirstToken(TokenTypes.MODIFIERS); | |
124 | 0 | if (!ctorMods.branchContains(TokenTypes.LITERAL_PRIVATE)) { |
125 | 0 | hasExplNonPrivateCtor = true; |
126 | 0 | break; |
127 | } | |
128 | } | |
129 | 22 | candidate = candidate.getNextSibling(); |
130 | } | |
131 | ||
132 | 2 | if (hasDefaultConstructor || hasExplNonPrivateCtor) { |
133 | 2 | final String name = aAST.findFirstToken(TokenTypes.IDENT).getText(); |
134 | 2 | log(aAST.getLineNo(), aAST.getColumnNo(), |
135 | "design.forExtension", name); | |
136 | } | |
137 | ||
138 | ||
139 | ||
140 | 2 | } |
141 | ||
142 | /** | |
143 | * Searches the tree towards the root until it finds a CLASS_DEF node. | |
144 | * @param aAST the start node for searching | |
145 | * @return the CLASS_DEF node. | |
146 | */ | |
147 | private DetailAST findContainingClass(DetailAST aAST) | |
148 | { | |
149 | 2 | DetailAST searchAST = aAST; |
150 | while ((searchAST.getType() != TokenTypes.CLASS_DEF) | |
151 | 6 | && (searchAST.getType() != TokenTypes.ENUM_DEF)) |
152 | { | |
153 | 4 | searchAST = searchAST.getParent(); |
154 | } | |
155 | 2 | return searchAST; |
156 | } | |
157 | } |