|
4 | 4 | import com.puppycrawl.tools.checkstyle.api.DetailAST; |
5 | 5 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; |
6 | 6 |
|
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
7 | 10 | public class AndroidViewFieldNameCheck extends AbstractCheck { |
8 | 11 |
|
9 | 12 | private static final String MESSAGE_KEY = "AndroidViewFieldNameCheck"; |
10 | 13 |
|
| 14 | + private static final Map<String, String> VIEW_NAMES = new HashMap<>(); |
| 15 | + |
| 16 | + public AndroidViewFieldNameCheck() { |
| 17 | + VIEW_NAMES.put( |
| 18 | + "LinearLayout", |
| 19 | + "ll" |
| 20 | + ); |
| 21 | + VIEW_NAMES.put( |
| 22 | + "RelativeLayout", |
| 23 | + "rl" |
| 24 | + ); |
| 25 | + VIEW_NAMES.put( |
| 26 | + "ConstrainsLayout", |
| 27 | + "cl" |
| 28 | + ); |
| 29 | + VIEW_NAMES.put( |
| 30 | + "FrameLayout", |
| 31 | + "fl" |
| 32 | + ); |
| 33 | + VIEW_NAMES.put( |
| 34 | + "ScrollView", |
| 35 | + "sv" |
| 36 | + ); |
| 37 | + VIEW_NAMES.put( |
| 38 | + "HorizontalScrollView", |
| 39 | + "hsv" |
| 40 | + ); |
| 41 | + VIEW_NAMES.put( |
| 42 | + "TextView", |
| 43 | + "tv" |
| 44 | + ); |
| 45 | + VIEW_NAMES.put( |
| 46 | + "ImageView", |
| 47 | + "iv" |
| 48 | + ); |
| 49 | + VIEW_NAMES.put( |
| 50 | + "ImageButton", |
| 51 | + "ib" |
| 52 | + ); |
| 53 | + VIEW_NAMES.put( |
| 54 | + "EditText", |
| 55 | + "et" |
| 56 | + ); |
| 57 | + VIEW_NAMES.put( |
| 58 | + "Button", |
| 59 | + "btn" |
| 60 | + ); |
| 61 | + VIEW_NAMES.put( |
| 62 | + "RecyclerView", |
| 63 | + "rv" |
| 64 | + ); |
| 65 | + VIEW_NAMES.put( |
| 66 | + "AdaptiveRecyclerView", |
| 67 | + "arv" |
| 68 | + ); |
| 69 | + VIEW_NAMES.put( |
| 70 | + "FloatingActionButton", |
| 71 | + "fab" |
| 72 | + ); |
| 73 | + VIEW_NAMES.put( |
| 74 | + "ViewGroup", |
| 75 | + "vg" |
| 76 | + ); |
| 77 | + VIEW_NAMES.put( |
| 78 | + "ViewPager", |
| 79 | + "vp" |
| 80 | + ); |
| 81 | + VIEW_NAMES.put( |
| 82 | + "CheckBox", |
| 83 | + "cb" |
| 84 | + ); |
| 85 | + VIEW_NAMES.put( |
| 86 | + "Switch", |
| 87 | + "sw" |
| 88 | + ); |
| 89 | + VIEW_NAMES.put( |
| 90 | + "DrawerLayout", |
| 91 | + "dl" |
| 92 | + ); |
| 93 | + VIEW_NAMES.put( |
| 94 | + "Toolbar", |
| 95 | + "tb" |
| 96 | + ); |
| 97 | + } |
| 98 | + |
11 | 99 | @Override |
12 | 100 | public void visitToken(DetailAST ast) { |
13 | 101 | DetailAST identifier = ast.findFirstToken(TokenTypes.TYPE).findFirstToken(TokenTypes.IDENT); |
14 | 102 | if (identifier != null) { |
15 | 103 | String fieldClassName = identifier.getText(); |
16 | | - String ident = ast.findFirstToken(TokenTypes.IDENT).getText(); |
17 | | - switch (fieldClassName) { |
18 | | - case "LinearLayout": |
19 | | - if (!ident.startsWith("lv")) { |
20 | | - log(ast); |
21 | | - } |
22 | | - break; |
23 | | - case "RelativeLayout": |
24 | | - if (!ident.startsWith("rl")) { |
25 | | - log(ast); |
26 | | - } |
27 | | - break; |
28 | | - case "ConstraintLayout": |
29 | | - if (!ident.startsWith("cl")) { |
30 | | - log(ast); |
31 | | - } |
32 | | - break; |
33 | | - case "FrameLayout": |
34 | | - if (!ident.startsWith("fl")) { |
35 | | - log(ast); |
36 | | - } |
37 | | - break; |
38 | | - case "ScrollView": |
39 | | - if (!ident.startsWith("sv")) { |
40 | | - log(ast); |
41 | | - } |
42 | | - break; |
43 | | - case "HorizontalScrollView": |
44 | | - if (!ident.startsWith("hsv")) { |
45 | | - log(ast); |
46 | | - } |
47 | | - break; |
48 | | - case "TextView": |
49 | | - if (!ident.startsWith("tv")) { |
50 | | - log(ast); |
51 | | - } |
52 | | - break; |
53 | | - case "ImageView": |
54 | | - if (!ident.startsWith("iv")) { |
55 | | - log(ast); |
56 | | - } |
57 | | - break; |
58 | | - case "ImageButton": |
59 | | - if (!ident.startsWith("ib")) { |
60 | | - log(ast); |
61 | | - } |
62 | | - break; |
63 | | - case "EditText": |
64 | | - if (!ident.startsWith("et")) { |
65 | | - log(ast); |
66 | | - } |
67 | | - break; |
68 | | - case "Button": |
69 | | - if (!ident.startsWith("btn")) { |
70 | | - log(ast); |
71 | | - } |
72 | | - break; |
73 | | - case "RecyclerView": |
74 | | - if (!ident.startsWith("rv")) { |
75 | | - log(ast); |
76 | | - } |
77 | | - break; |
78 | | - case "AdaptiveRecyclerView": |
79 | | - if (!ident.startsWith("arv")) { |
80 | | - log(ast); |
81 | | - } |
82 | | - break; |
83 | | - case "FloatingActionButton": |
84 | | - if (!ident.startsWith("fab")) { |
85 | | - log(ast); |
86 | | - } |
87 | | - break; |
| 104 | + if (VIEW_NAMES.containsKey(fieldClassName)) { |
| 105 | + String ident = ast.findFirstToken(TokenTypes.IDENT).getText(); |
| 106 | + if (!VIEW_NAMES.get(fieldClassName).startsWith(ident)) { |
| 107 | + log(ast); |
| 108 | + } |
88 | 109 | } |
89 | 110 | } |
90 | 111 | } |
|
0 commit comments