19
19
import static com .google .common .collect .ImmutableSet .toImmutableSet ;
20
20
import static com .google .errorprone .BugPattern .SeverityLevel .ERROR ;
21
21
import static com .google .errorprone .matchers .Description .NO_MATCH ;
22
+ import static com .google .errorprone .util .ASTHelpers .getSymbol ;
22
23
23
24
import com .google .common .base .Joiner ;
24
25
import com .google .common .collect .ImmutableSet ;
49
50
import com .sun .source .tree .TypeParameterTree ;
50
51
import com .sun .tools .javac .code .Symbol ;
51
52
import com .sun .tools .javac .code .Symbol .ClassSymbol ;
53
+ import com .sun .tools .javac .code .Symbol .MethodSymbol ;
52
54
import com .sun .tools .javac .code .Symbol .TypeVariableSymbol ;
53
55
import com .sun .tools .javac .code .Type ;
54
56
import com .sun .tools .javac .tree .JCTree .JCMemberReference ;
@@ -90,29 +92,26 @@ private ImmutableChecker(ErrorProneFlags flags, ImmutableSet<String> immutableAn
90
92
// check instantiations of `@ImmutableTypeParameter`s in method references
91
93
@ Override
92
94
public Description matchMemberReference (MemberReferenceTree tree , VisitorState state ) {
93
- checkInvocation (
94
- tree , ((JCMemberReference ) tree ).referentType , state , ASTHelpers .getSymbol (tree ));
95
+ checkInvocation (tree , getSymbol (tree ), ((JCMemberReference ) tree ).referentType , state );
95
96
return NO_MATCH ;
96
97
}
97
98
98
99
// check instantiations of `@ImmutableTypeParameter`s in method invocations
99
100
@ Override
100
101
public Description matchMethodInvocation (MethodInvocationTree tree , VisitorState state ) {
101
- checkInvocation (
102
- tree , ASTHelpers .getType (tree .getMethodSelect ()), state , ASTHelpers .getSymbol (tree ));
102
+ checkInvocation (tree , getSymbol (tree ), ASTHelpers .getType (tree .getMethodSelect ()), state );
103
103
return NO_MATCH ;
104
104
}
105
105
106
106
@ Override
107
107
public Description matchNewClass (NewClassTree tree , VisitorState state ) {
108
108
// check instantiations of `@ImmutableTypeParameter`s in generic constructor invocations
109
- checkInvocation (
110
- tree , ((JCNewClass ) tree ).constructorType , state , ((JCNewClass ) tree ).constructor );
109
+ checkInvocation (tree , getSymbol (tree ), ((JCNewClass ) tree ).constructorType , state );
111
110
// check instantiations of `@ImmutableTypeParameter`s in class constructor invocations
112
111
checkInstantiation (
113
112
tree ,
114
113
state ,
115
- ASTHelpers . getSymbol (tree .getIdentifier ()).getTypeParameters (),
114
+ getSymbol (tree .getIdentifier ()).getTypeParameters (),
116
115
ASTHelpers .getType (tree ).getTypeArguments ());
117
116
118
117
ClassTree classBody = tree .getClassBody ();
@@ -132,7 +131,7 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
132
131
checkInstantiation (
133
132
tree ,
134
133
state ,
135
- ASTHelpers . getSymbol (tree ).getTypeParameters (),
134
+ getSymbol (tree ).getTypeParameters (),
136
135
ASTHelpers .getType (tree ).getTypeArguments ());
137
136
return NO_MATCH ;
138
137
}
@@ -141,7 +140,8 @@ private ImmutableAnalysis createImmutableAnalysis(VisitorState state) {
141
140
return new ImmutableAnalysis (this , state , wellKnownMutability , immutableAnnotations );
142
141
}
143
142
144
- private void checkInvocation (Tree tree , Type methodType , VisitorState state , Symbol symbol ) {
143
+ private void checkInvocation (
144
+ Tree tree , MethodSymbol symbol , Type methodType , VisitorState state ) {
145
145
ImmutableAnalysis analysis = createImmutableAnalysis (state );
146
146
Violation info = analysis .checkInvocation (methodType , symbol );
147
147
if (info .isPresent ()) {
@@ -197,8 +197,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
197
197
// Check that the types in containerOf actually exist
198
198
Map <String , TypeVariableSymbol > typarams = new HashMap <>();
199
199
for (TypeParameterTree typaram : tree .getTypeParameters ()) {
200
- typarams .put (
201
- typaram .getName ().toString (), (TypeVariableSymbol ) ASTHelpers .getSymbol (typaram ));
200
+ typarams .put (typaram .getName ().toString (), (TypeVariableSymbol ) getSymbol (typaram ));
202
201
}
203
202
SetView <String > difference = Sets .difference (annotation .containerOf (), typarams .keySet ());
204
203
if (!difference .isEmpty ()) {
@@ -231,12 +230,12 @@ public Description matchClass(ClassTree tree, VisitorState state) {
231
230
// Check that the fields (including inherited fields) are immutable, and
232
231
// validate the type hierarchy superclass.
233
232
234
- ClassSymbol sym = ASTHelpers . getSymbol (tree );
233
+ ClassSymbol sym = getSymbol (tree );
235
234
236
235
Violation info =
237
236
analysis .checkForImmutability (
238
237
Optional .of (tree ),
239
- immutableTypeParametersInScope (ASTHelpers . getSymbol (tree ), state , analysis ),
238
+ immutableTypeParametersInScope (getSymbol (tree ), state , analysis ),
240
239
ASTHelpers .getType (tree ),
241
240
(Tree matched , Violation violation ) ->
242
241
describeClass (matched , sym , annotation , violation ));
@@ -255,7 +254,7 @@ private void checkClassTreeInstantiation(
255
254
tree ,
256
255
state ,
257
256
analysis ,
258
- ASTHelpers . getSymbol (implementTree ).getTypeParameters (),
257
+ getSymbol (implementTree ).getTypeParameters (),
259
258
ASTHelpers .getType (implementTree ).getTypeArguments ());
260
259
}
261
260
@@ -265,7 +264,7 @@ private void checkClassTreeInstantiation(
265
264
tree ,
266
265
state ,
267
266
analysis ,
268
- ASTHelpers . getSymbol (extendsClause ).getTypeParameters (),
267
+ getSymbol (extendsClause ).getTypeParameters (),
269
268
ASTHelpers .getType (extendsClause ).getTypeArguments ());
270
269
}
271
270
}
@@ -289,7 +288,7 @@ private Description.Builder describeClass(
289
288
/** Check anonymous implementations of {@code @Immutable} types. */
290
289
private Description handleAnonymousClass (
291
290
ClassTree tree , VisitorState state , ImmutableAnalysis analysis ) {
292
- ClassSymbol sym = ASTHelpers . getSymbol (tree );
291
+ ClassSymbol sym = getSymbol (tree );
293
292
Type superType = immutableSupertype (sym , state );
294
293
if (superType == null ) {
295
294
return NO_MATCH ;
@@ -334,7 +333,7 @@ private Description.Builder describeAnonymous(Tree tree, Type superType, Violati
334
333
335
334
/** Check for classes without {@code @Immutable} that have immutable supertypes. */
336
335
private Description checkSubtype (ClassTree tree , VisitorState state ) {
337
- ClassSymbol sym = ASTHelpers . getSymbol (tree );
336
+ ClassSymbol sym = getSymbol (tree );
338
337
Type superType = immutableSupertype (sym , state );
339
338
if (superType == null ) {
340
339
return NO_MATCH ;
0 commit comments