Skip to content

Commit 6d2f852

Browse files
committed
Keep class import when class is used as a type reference
When a class in `REQUIRE_STATIC_IMPORTS` appears as a type reference (parameter, field, variable), its class import must be retained even if all its method calls are converted to static imports. The pre-pass now also visits `SimpleType` nodes to detect such usages.
1 parent a3240d6 commit 6d2f852

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

airstyle-core/src/main/java/io/airlift/airstyle/normalizer/StaticImportRuleNormalizer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.jdt.core.dom.ImportDeclaration;
2222
import org.eclipse.jdt.core.dom.MethodInvocation;
2323
import org.eclipse.jdt.core.dom.Name;
24+
import org.eclipse.jdt.core.dom.SimpleType;
2425
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
2526

2627
import java.util.ArrayList;
@@ -290,6 +291,16 @@ public boolean visit(MethodInvocation node)
290291
}
291292
return true;
292293
}
294+
295+
@Override
296+
public boolean visit(SimpleType node)
297+
{
298+
String typeFqn = resolveClassName(node.getName(), importedClassesBySimpleName);
299+
if (REQUIRE_STATIC_IMPORTS.containsKey(typeFqn)) {
300+
result.add(typeFqn);
301+
}
302+
return true;
303+
}
293304
});
294305
return result;
295306
}

airstyle-core/src/test/java/io/airlift/airstyle/normalizer/TestStaticImportRuleNormalizer.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,64 @@ String run(Map<String, Integer> values, List<String> names, Set<Integer> numbers
240240
assertFormatsOldToNew(oldCode, newCode);
241241
}
242242

243+
@Test
244+
void testFormatterFixesRequiredStaticImportsWhenClassStillUsedAsParameter()
245+
{
246+
String oldCode =
247+
"""
248+
import com.google.common.collect.ImmutableList;
249+
import com.google.common.collect.ImmutableMap;
250+
import com.google.common.collect.ImmutableSet;
251+
252+
class Test {
253+
String run(ImmutableMap<String, Integer> values, ImmutableList<String> names, ImmutableSet<Integer> numbers)
254+
{
255+
String mapped = values.entrySet().stream()
256+
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, entry -> Math.toIntExact(entry.getValue())))
257+
.toString();
258+
String listed = names.stream()
259+
.collect(ImmutableList.toImmutableList())
260+
.toString();
261+
String settled = numbers.stream()
262+
.collect(ImmutableSet.toImmutableSet())
263+
.toString();
264+
return mapped + listed + settled;
265+
}
266+
}
267+
""";
268+
269+
String newCode =
270+
"""
271+
import com.google.common.collect.ImmutableList;
272+
import com.google.common.collect.ImmutableMap;
273+
import com.google.common.collect.ImmutableSet;
274+
275+
import static com.google.common.collect.ImmutableList.toImmutableList;
276+
import static com.google.common.collect.ImmutableMap.toImmutableMap;
277+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
278+
import static java.lang.Math.toIntExact;
279+
280+
class Test
281+
{
282+
String run(ImmutableMap<String, Integer> values, ImmutableList<String> names, ImmutableSet<Integer> numbers)
283+
{
284+
String mapped = values.entrySet().stream()
285+
.collect(toImmutableMap(Map.Entry::getKey, entry -> toIntExact(entry.getValue())))
286+
.toString();
287+
String listed = names.stream()
288+
.collect(toImmutableList())
289+
.toString();
290+
String settled = numbers.stream()
291+
.collect(toImmutableSet())
292+
.toString();
293+
return mapped + listed + settled;
294+
}
295+
}
296+
""";
297+
298+
assertFormatsOldToNew(oldCode, newCode);
299+
}
300+
243301
@Test
244302
void testFormatterFixesBannedPlainUtilityImports()
245303
{

0 commit comments

Comments
 (0)