samedi 1 janvier 2022

What does

I was digging into some infrastructure code, when I came across this method declaration:

<T> T resolve(Class<T> var1);

Is this some kind of pattern or technique? If so, what's its purpose?

Edit: I understand generics, etc. I'm more wondering in what case would a method of that form (taking a Class and returning an instance of it) be useful?

Here's more of the context, in case it helps:

public interface Environment {
  String domain();

  Client client();

  Config config();

  Environment.RoutingEngine routingEngine();

  Closer closer();

  <T> T resolve(Class<T> var1);

  public interface RoutingEngine {
    Environment.RoutingEngine registerAutoRoutes(RouteProvider var1);

    Environment.RoutingEngine registerAutoRoute(Route<? extends AsyncHandler<?>> var1);

    Environment.RoutingEngine registerRoutes(Stream<? extends Route<? extends AsyncHandler<? extends Response<ByteString>>>> var1);

    Environment.RoutingEngine registerRoute(Route<? extends AsyncHandler<? extends Response<ByteString>>> var1);
  }
}

And an example use of it, though I doubt it helps:

  static void configure(final Environment environment) {
    final GrpcServer grpcServer = environment.resolve(GrpcServer.class);
    grpcServer.addService(new ExampleResource());
  }

Aucun commentaire:

Enregistrer un commentaire