Created
January 25, 2014 15:12
-
-
Save blundell/8617812 to your computer and use it in GitHub Desktop.
Anti-Hungarian CheckStyle check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.puppycrawl.tools.checkstyle.api.*; | |
public class AntiHungarianCheck extends Check { | |
private static final String CATCH_MSG = "Hungarian notation belongs in the 90's. " + | |
"Don't prefix member variables with 'm'. " + | |
"Use your IDE's shiny colors. Culprit was: "; | |
private final HungarianNotationMemberDetector detector = new HungarianNotationMemberDetector(); | |
@Override | |
public int[] getDefaultTokens() { | |
return new int[] { TokenTypes.VARIABLE_DEF }; | |
} | |
@Override | |
public void visitToken(DetailAST aAST) { | |
String variableName = findVariableName(aAST); | |
if (itsAFieldVariable(aAST) && detector.detectsNotation(variableName)) { | |
reportStyleError(aAST, variableName); | |
} | |
} | |
private String findVariableName(DetailAST aAST) { | |
DetailAST identifier = aAST.findFirstToken(TokenTypes.IDENT); | |
return identifier.toString(); | |
} | |
private boolean itsAFieldVariable(DetailAST aAST) { | |
return aAST.getParent().getType() == TokenTypes.OBJBLOCK; | |
} | |
private void reportStyleError(DetailAST aAST, String variableName) { | |
log(aAST.getLineNo(), CATCH_MSG + variableName); | |
} | |
} | |
______ | |
import java.util.regex.Pattern; | |
class HungarianNotationMemberDetector { | |
private final Pattern pattern = Pattern.compile("m[A-Z0-9].*"); | |
public boolean detectsNotation(String variableName) { | |
return pattern.matcher(variableName).matches(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not just do something like: