| 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.modifier; |
| 20 | |
|
| 21 | |
import com.puppycrawl.tools.checkstyle.api.Check; |
| 22 | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; |
| 23 | |
import com.puppycrawl.tools.checkstyle.api.ScopeUtils; |
| 24 | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 1 | public class RedundantModifierCheck |
| 33 | |
extends Check |
| 34 | |
{ |
| 35 | |
@Override |
| 36 | |
public int[] getDefaultTokens() |
| 37 | |
{ |
| 38 | 1 | return new int[] { |
| 39 | |
TokenTypes.METHOD_DEF, |
| 40 | |
TokenTypes.VARIABLE_DEF, |
| 41 | |
TokenTypes.ANNOTATION_FIELD_DEF, |
| 42 | |
TokenTypes.INTERFACE_DEF, |
| 43 | |
}; |
| 44 | |
} |
| 45 | |
|
| 46 | |
@Override |
| 47 | |
public int[] getRequiredTokens() |
| 48 | |
{ |
| 49 | 0 | return new int[] {}; |
| 50 | |
} |
| 51 | |
|
| 52 | |
@Override |
| 53 | |
public void visitToken(DetailAST aAST) |
| 54 | |
{ |
| 55 | 27 | if (TokenTypes.INTERFACE_DEF == aAST.getType()) { |
| 56 | 2 | final DetailAST modifiers = |
| 57 | |
aAST.findFirstToken(TokenTypes.MODIFIERS); |
| 58 | 2 | if (null != modifiers) { |
| 59 | 2 | final DetailAST modifier = |
| 60 | |
modifiers.findFirstToken(TokenTypes.LITERAL_STATIC); |
| 61 | 2 | if (null != modifier) { |
| 62 | 1 | log(modifier.getLineNo(), modifier.getColumnNo(), |
| 63 | |
"redundantModifier", modifier.getText()); |
| 64 | |
} |
| 65 | |
} |
| 66 | 2 | } |
| 67 | 25 | else if (ScopeUtils.inInterfaceOrAnnotationBlock(aAST)) { |
| 68 | 14 | final DetailAST modifiers = |
| 69 | |
aAST.findFirstToken(TokenTypes.MODIFIERS); |
| 70 | |
|
| 71 | 14 | DetailAST modifier = modifiers.getFirstChild(); |
| 72 | 14 | while (modifier != null) { |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 9 | final int type = modifier.getType(); |
| 79 | 9 | if ((type == TokenTypes.LITERAL_PUBLIC) |
| 80 | |
|| (type == TokenTypes.ABSTRACT) |
| 81 | |
|| (type == TokenTypes.LITERAL_STATIC) |
| 82 | |
|| (type == TokenTypes.FINAL)) |
| 83 | |
{ |
| 84 | 9 | log(modifier.getLineNo(), modifier.getColumnNo(), |
| 85 | |
"redundantModifier", modifier.getText()); |
| 86 | 9 | break; |
| 87 | |
} |
| 88 | |
|
| 89 | 0 | modifier = modifier.getNextSibling(); |
| 90 | 0 | } |
| 91 | 14 | } |
| 92 | 11 | else if (aAST.getType() == TokenTypes.METHOD_DEF) { |
| 93 | 10 | final DetailAST modifiers = |
| 94 | |
aAST.findFirstToken(TokenTypes.MODIFIERS); |
| 95 | |
|
| 96 | 10 | boolean checkFinal = |
| 97 | |
modifiers.branchContains(TokenTypes.LITERAL_PRIVATE); |
| 98 | |
|
| 99 | 10 | DetailAST parent = aAST.getParent(); |
| 100 | 26 | while (parent != null) { |
| 101 | 25 | if (parent.getType() == TokenTypes.CLASS_DEF) { |
| 102 | 9 | final DetailAST classModifiers = |
| 103 | |
parent.findFirstToken(TokenTypes.MODIFIERS); |
| 104 | 9 | checkFinal |= |
| 105 | |
classModifiers.branchContains(TokenTypes.FINAL); |
| 106 | 9 | break; |
| 107 | |
} |
| 108 | 16 | parent = parent.getParent(); |
| 109 | |
} |
| 110 | 10 | if (checkFinal) { |
| 111 | 9 | DetailAST modifier = modifiers.getFirstChild(); |
| 112 | 28 | while (modifier != null) { |
| 113 | 21 | final int type = modifier.getType(); |
| 114 | 21 | if (type == TokenTypes.FINAL) { |
| 115 | 2 | log(modifier.getLineNo(), modifier.getColumnNo(), |
| 116 | |
"redundantModifier", modifier.getText()); |
| 117 | 2 | break; |
| 118 | |
} |
| 119 | 19 | modifier = modifier.getNextSibling(); |
| 120 | 19 | } |
| 121 | |
} |
| 122 | |
} |
| 123 | 27 | } |
| 124 | |
} |