Skip to content

Commit 0f96f2a

Browse files
authored
Merge pull request #10 from fartem/1.2.2
2021-05-02 Version 1.2.2: Updated AndroidViewFieldNameCheck
2 parents 206cb92 + da8ce32 commit 0f96f2a

File tree

2 files changed

+94
-73
lines changed

2 files changed

+94
-73
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'com.smlnskgmail.jaman.checkstyle'
8-
version '1.2.1'
8+
version '1.2.2'
99

1010
checkstyle {
1111
toolVersion '8.41.1'

src/main/java/com/smlnskgmail/jaman/checkstyle/checks/AndroidViewFieldNameCheck.java

Lines changed: 93 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,108 @@
44
import com.puppycrawl.tools.checkstyle.api.DetailAST;
55
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
66

7+
import java.util.HashMap;
8+
import java.util.Map;
9+
710
public class AndroidViewFieldNameCheck extends AbstractCheck {
811

912
private static final String MESSAGE_KEY = "AndroidViewFieldNameCheck";
1013

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+
1199
@Override
12100
public void visitToken(DetailAST ast) {
13101
DetailAST identifier = ast.findFirstToken(TokenTypes.TYPE).findFirstToken(TokenTypes.IDENT);
14102
if (identifier != null) {
15103
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+
}
88109
}
89110
}
90111
}

0 commit comments

Comments
 (0)