Skip to content

Commit

Permalink
Replace uses of newParameterizedType
Browse files Browse the repository at this point in the history
This uses the "well known" trick, used already elsewhere in the code, to
use the anonymous inner class to force reification of type parameters,
so that `Key` can get the exact type parameter information via
reflection. This saves us a utility method and an unchecked cast.
  • Loading branch information
ksobolew authored and wendigo committed Apr 24, 2024
1 parent 373dccf commit 54c4f76
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions core/trino-main/src/main/java/io/trino/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Types;
import io.airlift.bootstrap.ApplicationConfigurationException;
import io.airlift.bootstrap.Bootstrap;
import io.airlift.discovery.client.Announcer;
Expand Down Expand Up @@ -148,7 +147,7 @@ private void doStart(String trinoVersion)

injector.getInstance(PluginInstaller.class).loadPlugins();

var catalogStoreManager = injector.getInstance(optionalKey(CatalogStoreManager.class));
var catalogStoreManager = injector.getInstance(Key.get(new TypeLiteral<Optional<CatalogStoreManager>>() {}));
if (catalogStoreManager.isPresent()) {
catalogStoreManager.get().loadConfiguredCatalogStore();
}
Expand All @@ -173,16 +172,17 @@ private void doStart(String trinoVersion)
injector.getInstance(SessionPropertyDefaults.class).loadConfigurationManager();
injector.getInstance(ResourceGroupManager.class).loadConfigurationManager();
injector.getInstance(AccessControlManager.class).loadSystemAccessControl();
injector.getInstance(optionalKey(PasswordAuthenticatorManager.class))
injector.getInstance(Key.get(new TypeLiteral<Optional<PasswordAuthenticatorManager>>() {}))
.ifPresent(PasswordAuthenticatorManager::loadPasswordAuthenticator);
injector.getInstance(EventListenerManager.class).loadEventListeners();
injector.getInstance(GroupProviderManager.class).loadConfiguredGroupProvider();
injector.getInstance(ExchangeManagerRegistry.class).loadExchangeManager();
injector.getInstance(CertificateAuthenticatorManager.class).loadCertificateAuthenticator();
injector.getInstance(optionalKey(HeaderAuthenticatorManager.class))
injector.getInstance(Key.get(new TypeLiteral<Optional<HeaderAuthenticatorManager>>() {}))
.ifPresent(HeaderAuthenticatorManager::loadHeaderAuthenticator);

injector.getInstance(optionalKey(OAuth2Client.class)).ifPresent(OAuth2Client::load);
injector.getInstance(Key.get(new TypeLiteral<Optional<OAuth2Client>>() {}))
.ifPresent(OAuth2Client::load);

injector.getInstance(Announcer.class).start();

Expand Down Expand Up @@ -225,12 +225,6 @@ public static void addConnectorEventListeners(
.forEach(eventListenerManager::addEventListener);
}

@SuppressWarnings("unchecked")
private static <T> Key<Optional<T>> optionalKey(Class<T> type)
{
return Key.get((TypeLiteral<Optional<T>>) TypeLiteral.get(Types.newParameterizedType(Optional.class, type)));
}

private static void addMessages(StringBuilder output, String type, List<Object> messages)
{
if (messages.isEmpty()) {
Expand Down

0 comments on commit 54c4f76

Please sign in to comment.