| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 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 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
@author |
| 57 |
|
@version |
| 58 |
|
|
|
|
|
| 80.7% |
Uncovered Elements: 11 (57) |
Complexity: 20 |
Complexity Density: 0.59 |
|
| 59 |
|
public class DesignForExtensionCheck extends Check |
| 60 |
|
{ |
| 61 |
|
@inheritDoc |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 62 |
1
|
public int[] getDefaultTokens()... |
| 63 |
|
{ |
| 64 |
1
|
return new int[] {TokenTypes.METHOD_DEF}; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
@inheritDoc |
|
|
|
| 76.6% |
Uncovered Elements: 11 (47) |
Complexity: 16 |
Complexity Density: 0.55 |
|
| 68 |
10
|
public void visitToken(DetailAST aAST)... |
| 69 |
|
{ |
| 70 |
|
|
| 71 |
10
|
if (ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) { |
| 72 |
1
|
return; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
9
|
final DetailAST modifiers = aAST.findFirstToken(TokenTypes.MODIFIERS); |
| 77 |
9
|
if (modifiers.branchContains(TokenTypes.LITERAL_PRIVATE) |
| 78 |
|
|| modifiers.branchContains(TokenTypes.ABSTRACT) |
| 79 |
|
|| modifiers.branchContains(TokenTypes.FINAL) |
| 80 |
|
|| modifiers.branchContains(TokenTypes.LITERAL_STATIC)) |
| 81 |
|
{ |
| 82 |
2
|
return; |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
7
|
if (!ScopeUtils.getSurroundingScope(aAST).isIn(Scope.PROTECTED)) { |
| 88 |
3
|
return; |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
4
|
final DetailAST implementation = aAST.findFirstToken(TokenTypes.SLIST); |
| 95 |
4
|
if ((implementation != null) |
| 96 |
|
&& (implementation.getFirstChild().getType() == TokenTypes.RCURLY)) |
| 97 |
|
{ |
| 98 |
2
|
return; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
|
| 102 |
2
|
final DetailAST classDef = findContainingClass(aAST); |
| 103 |
2
|
final DetailAST classMods = |
| 104 |
|
classDef.findFirstToken(TokenTypes.MODIFIERS); |
| 105 |
2
|
if ((classDef.getType() == TokenTypes.ENUM_DEF) |
| 106 |
|
|| classMods.branchContains(TokenTypes.FINAL)) |
| 107 |
|
{ |
| 108 |
0
|
return; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
2
|
final DetailAST objBlock = classDef.findFirstToken(TokenTypes.OBJBLOCK); |
| 113 |
|
|
| 114 |
2
|
boolean hasDefaultConstructor = true; |
| 115 |
2
|
boolean hasExplNonPrivateCtor = false; |
| 116 |
|
|
| 117 |
2
|
DetailAST candidate = (DetailAST) objBlock.getFirstChild(); |
| 118 |
|
|
| 119 |
24
|
while (candidate != null) { |
| 120 |
22
|
if (candidate.getType() == TokenTypes.CTOR_DEF) { |
| 121 |
0
|
hasDefaultConstructor = false; |
| 122 |
|
|
| 123 |
0
|
final DetailAST ctorMods = |
| 124 |
|
candidate.findFirstToken(TokenTypes.MODIFIERS); |
| 125 |
0
|
if (!ctorMods.branchContains(TokenTypes.LITERAL_PRIVATE)) { |
| 126 |
0
|
hasExplNonPrivateCtor = true; |
| 127 |
0
|
break; |
| 128 |
|
} |
| 129 |
|
} |
| 130 |
22
|
candidate = (DetailAST) candidate.getNextSibling(); |
| 131 |
|
} |
| 132 |
|
|
| 133 |
2
|
if (hasDefaultConstructor || hasExplNonPrivateCtor) { |
| 134 |
2
|
final String name = aAST.findFirstToken(TokenTypes.IDENT).getText(); |
| 135 |
2
|
log(aAST.getLineNo(), aAST.getColumnNo(), |
| 136 |
|
"design.forExtension", name); |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
@param |
| 146 |
|
@return |
| 147 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 148 |
2
|
private DetailAST findContainingClass(DetailAST aAST)... |
| 149 |
|
{ |
| 150 |
2
|
DetailAST searchAST = aAST; |
| 151 |
6
|
while ((searchAST.getType() != TokenTypes.CLASS_DEF) |
| 152 |
|
&& (searchAST.getType() != TokenTypes.ENUM_DEF)) |
| 153 |
|
{ |
| 154 |
4
|
searchAST = searchAST.getParent(); |
| 155 |
|
} |
| 156 |
2
|
return searchAST; |
| 157 |
|
} |
| 158 |
|
} |