18
18
19
19
import static com .google .common .collect .ImmutableSet .toImmutableSet ;
20
20
import static com .google .errorprone .BugPattern .SeverityLevel .WARNING ;
21
+ import static com .google .errorprone .util .ASTHelpers .getSymbol ;
21
22
import static com .google .errorprone .util .ASTHelpers .isSwitchDefault ;
22
23
23
24
import com .google .common .collect .ImmutableSet ;
27
28
import com .google .errorprone .bugpatterns .BugChecker .SwitchTreeMatcher ;
28
29
import com .google .errorprone .matchers .Description ;
29
30
import com .google .errorprone .util .ASTHelpers ;
30
- import com .sun .source .tree .IdentifierTree ;
31
+ import com .sun .source .tree .CaseTree ;
32
+ import com .sun .source .tree .ExpressionTree ;
31
33
import com .sun .source .tree .SwitchTree ;
32
34
import com .sun .tools .javac .code .Type ;
35
+ import java .util .List ;
33
36
import java .util .Set ;
34
37
import java .util .stream .Collectors ;
35
38
import javax .lang .model .element .ElementKind ;
@@ -44,25 +47,26 @@ public class MissingCasesInEnumSwitch extends BugChecker implements SwitchTreeMa
44
47
45
48
@ Override
46
49
public Description matchSwitch (SwitchTree tree , VisitorState state ) {
47
- Type switchType = ASTHelpers .getType (tree .getExpression ());
50
+ ExpressionTree expression = tree .getExpression ();
51
+ List <? extends CaseTree > cases = tree .getCases ();
52
+ Type switchType = ASTHelpers .getType (expression );
48
53
if (switchType .asElement ().getKind () != ElementKind .ENUM ) {
49
54
return Description .NO_MATCH ;
50
55
}
51
56
// default case is present
52
- if (tree . getCases () .stream ().anyMatch (c -> isSwitchDefault (c ))) {
57
+ if (cases .stream ().anyMatch (c -> isSwitchDefault (c ))) {
53
58
return Description .NO_MATCH ;
54
59
}
55
60
ImmutableSet <String > handled =
56
- tree . getCases () .stream ()
61
+ cases .stream ()
57
62
.flatMap (c -> c .getExpressions ().stream ())
58
- .filter (IdentifierTree .class ::isInstance )
59
- .map (e -> ((IdentifierTree ) e ).getName ().toString ())
63
+ .map (e -> getSymbol (e ).getSimpleName ().toString ())
60
64
.collect (toImmutableSet ());
61
65
Set <String > unhandled = Sets .difference (ASTHelpers .enumValues (switchType .asElement ()), handled );
62
66
if (unhandled .isEmpty ()) {
63
67
return Description .NO_MATCH ;
64
68
}
65
- return buildDescription (tree ).setMessage (buildMessage (unhandled )).build ();
69
+ return buildDescription (expression ).setMessage (buildMessage (unhandled )).build ();
66
70
}
67
71
68
72
/**
0 commit comments