@@ -51,8 +51,15 @@ public enum ImportPolicy {
51
51
* Import the outermost class and explicitly qualify references below that. For example, to
52
52
* reference {@code com.google.Foo.Bar}, we import {@code com.google.Foo} and explicitly qualify
53
53
* {@code Foo.Bar}.
54
+ *
55
+ * <p><b>Note:</b> static methods named {@code assertThat}, {@code assertWithMessage} and {@code
56
+ * assertAbout} are always statically imported.
54
57
*/
55
58
IMPORT_TOP_LEVEL {
59
+
60
+ private static final ImmutableSet <String > METHOD_NAMES_TO_STATICALLY_IMPORT =
61
+ ImmutableSet .of ("assertThat" , "assertAbout" , "assertWithMessage" );
62
+
56
63
@ Override
57
64
public JCExpression classReference (
58
65
Inliner inliner , CharSequence topLevelClazz , CharSequence fullyQualifiedClazz ) {
@@ -111,6 +118,11 @@ public JCExpression staticReference(
111
118
CharSequence topLevelClazz ,
112
119
CharSequence fullyQualifiedClazz ,
113
120
CharSequence member ) {
121
+ // NOTE(b/17121704): we always statically import certain method names
122
+ if (METHOD_NAMES_TO_STATICALLY_IMPORT .contains (member .toString ())) {
123
+ return STATIC_IMPORT_ALWAYS .staticReference (
124
+ inliner , topLevelClazz , fullyQualifiedClazz , member );
125
+ }
114
126
return inliner
115
127
.maker ()
116
128
.Select (
@@ -206,32 +218,6 @@ public JCExpression staticReference(
206
218
}
207
219
return inliner .maker ().Ident (inliner .asName (member ));
208
220
}
209
- },
210
-
211
- /**
212
- * When inlining static methods, always static import the method if it is called {@code
213
- * assertThat}. Non-static references to classes are imported from the top level as in {@code
214
- * IMPORT_TOP_LEVEL}.
215
- */
216
- STATIC_IMPORT_ASSERT_THAT {
217
- @ Override
218
- public JCExpression classReference (
219
- Inliner inliner , CharSequence topLevelClazz , CharSequence fullyQualifiedClazz ) {
220
- return IMPORT_TOP_LEVEL .classReference (inliner , topLevelClazz , fullyQualifiedClazz );
221
- }
222
-
223
- @ Override
224
- public JCExpression staticReference (
225
- Inliner inliner ,
226
- CharSequence topLevelClazz ,
227
- CharSequence fullyQualifiedClazz ,
228
- CharSequence member ) {
229
- if (member .toString ().equals ("assertThat" )) {
230
- return STATIC_IMPORT_ALWAYS .staticReference (
231
- inliner , topLevelClazz , fullyQualifiedClazz , member );
232
- }
233
- return IMPORT_TOP_LEVEL .staticReference (inliner , topLevelClazz , fullyQualifiedClazz , member );
234
- }
235
221
};
236
222
237
223
public static void bind (Context context , ImportPolicy policy ) {
0 commit comments