Best-effort static type import handling
MOE_MIGRATED_REVID=136542480
diff --git a/java/com/google/turbine/binder/lookup/ImportIndex.java b/java/com/google/turbine/binder/lookup/ImportIndex.java
index 45a60c0..aa1e0c5 100644
--- a/java/com/google/turbine/binder/lookup/ImportIndex.java
+++ b/java/com/google/turbine/binder/lookup/ImportIndex.java
@@ -25,6 +25,7 @@
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.tree.Tree;
import com.google.turbine.tree.Tree.ImportDecl;
+import java.util.HashMap;
import java.util.Map;
/**
@@ -53,11 +54,9 @@
final TopLevelIndex cpi,
ImmutableList<ImportDecl> imports) {
ImmutableList.Builder<Supplier<Scope>> packageScopes = ImmutableList.builder();
- ImmutableMap.Builder<String, Supplier<ClassSymbol>> thunks = ImmutableMap.builder();
+ Map<String, Supplier<ClassSymbol>> thunks = new HashMap<>();
for (final Tree.ImportDecl i : imports) {
if (i.stat()) {
- // static imports of compile-time constant fields are handled later; static imports of
- // types are not supported
continue;
}
if (i.wild()) {
@@ -87,21 +86,38 @@
}
}));
}
- thunks.put(
- getLast(i.type()),
- Suppliers.memoize(
- new Supplier<ClassSymbol>() {
- @Override
- public ClassSymbol get() {
- LookupResult result = cpi.lookup(new LookupKey(i.type()));
- if (result == null) {
- return null;
- }
- return resolve.resolve(result);
- }
- }));
+ thunks.put(getLast(i.type()), thunk(resolve, cpi, i));
}
- return new ImportIndex(thunks.build(), packageScopes.build());
+ // Best-effort static type import handling.
+ // Static imports that cannot be resolved to canonical types (either because they
+ // are field or method imports, or because they are non-canonical type imports)
+ // are silently ignored.
+ for (final Tree.ImportDecl i : imports) {
+ if (!i.stat()) {
+ continue;
+ }
+ String last = getLast(i.type());
+ if (thunks.containsKey(last)) {
+ continue;
+ }
+ thunks.put(last, thunk(resolve, cpi, i));
+ }
+ return new ImportIndex(ImmutableMap.copyOf(thunks), packageScopes.build());
+ }
+
+ private static Supplier<ClassSymbol> thunk(
+ final CanonicalSymbolResolver resolve, final TopLevelIndex cpi, final ImportDecl i) {
+ return Suppliers.memoize(
+ new Supplier<ClassSymbol>() {
+ @Override
+ public ClassSymbol get() {
+ LookupResult result = cpi.lookup(new LookupKey(i.type()));
+ if (result == null) {
+ return null;
+ }
+ return resolve.resolve(result);
+ }
+ });
}
@Override
diff --git a/javatests/com/google/turbine/lower/LowerIntegrationTest.java b/javatests/com/google/turbine/lower/LowerIntegrationTest.java
index 2310d38..0d3984a 100644
--- a/javatests/com/google/turbine/lower/LowerIntegrationTest.java
+++ b/javatests/com/google/turbine/lower/LowerIntegrationTest.java
@@ -218,6 +218,7 @@
"const_hiding.test",
"interface_field.test",
"concat.test",
+ "static_type_import.test",
};
List<Object[]> tests =
ImmutableList.copyOf(testCases).stream().map(x -> new Object[] {x}).collect(toList());
diff --git a/javatests/com/google/turbine/lower/testdata/static_type_import.test b/javatests/com/google/turbine/lower/testdata/static_type_import.test
new file mode 100644
index 0000000..e841c5f
--- /dev/null
+++ b/javatests/com/google/turbine/lower/testdata/static_type_import.test
@@ -0,0 +1,5 @@
+=== Test.java ===
+import static java.util.Map.Entry;
+public interface Test {
+ Entry f();
+}
\ No newline at end of file