File tree 2 files changed +53
-1
lines changed
main/java/com/google/errorprone/bugpatterns
test/java/com/google/errorprone/bugpatterns
2 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 25
25
import static com .google .errorprone .util .ASTHelpers .getNullnessValue ;
26
26
import static com .google .errorprone .util .ASTHelpers .getReceiver ;
27
27
import static com .google .errorprone .util .ASTHelpers .getSymbol ;
28
+ import static com .google .errorprone .util .ASTHelpers .isStatic ;
28
29
import static java .lang .String .format ;
29
30
30
31
import com .google .errorprone .BugPattern ;
41
42
import com .sun .source .tree .Tree ;
42
43
import com .sun .tools .javac .code .Symbol .VarSymbol ;
43
44
import java .util .Objects ;
45
+ import java .util .regex .Pattern ;
44
46
45
47
/** See the summary. */
46
48
@ BugPattern (
@@ -119,6 +121,12 @@ private static boolean seemsConstant(Tree tree) {
119
121
return true ;
120
122
}
121
123
var symbol = getSymbol (tree );
122
- return symbol instanceof VarSymbol && symbol .isEnum ();
124
+ if (!(symbol instanceof VarSymbol )) {
125
+ return false ;
126
+ }
127
+ return symbol .isEnum ()
128
+ || (isStatic (symbol ) && CONSTANT_CASE .matcher (symbol .getSimpleName ().toString ()).matches ());
123
129
}
130
+
131
+ private static final Pattern CONSTANT_CASE = Pattern .compile ("[A-Z0-9_]+" );
124
132
}
Original file line number Diff line number Diff line change @@ -53,6 +53,50 @@ public void primitive() {
53
53
.doTest ();
54
54
}
55
55
56
+ @ Test
57
+ public void boxedBoolean () {
58
+ refactoring
59
+ .addInputLines (
60
+ "Test.java" ,
61
+ "class Test {" ,
62
+ " boolean yoda(Boolean a) {" ,
63
+ " return Boolean.TRUE.equals(a);" ,
64
+ " }" ,
65
+ "}" )
66
+ .addOutputLines (
67
+ "Test.java" ,
68
+ "import java.util.Objects;" ,
69
+ "class Test {" ,
70
+ " boolean yoda(Boolean a) {" ,
71
+ " return Objects.equals(a, Boolean.TRUE);" ,
72
+ " }" ,
73
+ "}" )
74
+ .doTest ();
75
+ }
76
+
77
+ @ Test
78
+ public void boxedVsUnboxedBoolean () {
79
+ refactoring
80
+ .addInputLines (
81
+ "Test.java" ,
82
+ "class Test {" ,
83
+ " boolean yoda(boolean a) {" ,
84
+ " return Boolean.TRUE.equals(a);" ,
85
+ " }" ,
86
+ "}" )
87
+ .addOutputLines (
88
+ "Test.java" ,
89
+ "class Test {" ,
90
+ " boolean yoda(boolean a) {" ,
91
+ // NOTE: this is a broken fix! We could detect this if it turns out to be an issue in
92
+ // practice.
93
+ " return a.equals(Boolean.TRUE);" ,
94
+ " }" ,
95
+ "}" )
96
+ .allowBreakingChanges ()
97
+ .doTest ();
98
+ }
99
+
56
100
@ Test
57
101
public void enums () {
58
102
refactoring
You can’t perform that action at this time.
0 commit comments