-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCheckingDetector.java
More file actions
30 lines (26 loc) · 1.17 KB
/
TypeCheckingDetector.java
File metadata and controls
30 lines (26 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.antipattern.detector.AntipatternDetector;
//import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public abstract class TypeCheckingDetector extends SpaghettiCodeDetector {
@Override
public int[] getDefaultTokens() {
return new int[]{TokenTypes.LITERAL_IF, TokenTypes.LITERAL_ELSE};
}
@Override
public void visitToken(DetailAST ast) {
super.visitToken(ast);
DetailAST expression = ast.findFirstToken(TokenTypes.EXPR);
if (expression != null) {
DetailAST dot = expression.findFirstToken(TokenTypes.DOT);
if (dot != null) {
DetailAST methodCall = dot.findFirstToken(TokenTypes.METHOD_CALL);
if (methodCall != null) {
DetailAST ident = methodCall.findFirstToken(TokenTypes.IDENT);
if (ident != null && "getClass".equals(ident.getText()))
log(ast, "Type checking anti-pattern detected: using 'getClass()' for type checking");
}
}
}
}
}