-3

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());
  }
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
  • 1
    `` is a generic parameters. I recommend reading a tutorial on the topic, e.g. [this one from `oracle.com`](https://docs.oracle.com/javase/tutorial/java/generics/). – Turing85 Jan 01 '22 at 15:00
  • @Turing85 - I understand generics. What I don't understand is in what situation or application would a method of that form (taking a Class, returning an instance of that class) be useful? – Andrew Cheong Jan 01 '22 at 15:06
  • In this case, it would be helpful if you [edit] the post and clarify your question. – Turing85 Jan 01 '22 at 15:07
  • Are you looking for this https://stackoverflow.com/questions/6503942/how-to-interpret-java-generics-like-t-t-t-v-queryt-classt ? – cigien Jan 01 '22 at 15:15
  • 2
    Or https://stackoverflow.com/q/462297/6296561 or https://stackoverflow.com/q/9406025/6296561 ? – Zoe Jan 01 '22 at 15:16
  • 2
    Ah, thank you @Zoe! This statement in one of your links, _"to enable one specific idiom1 - use of Class objects as type-safe object factories"_, made it clear to me. The Oracle page linked from there is great too. – Andrew Cheong Jan 01 '22 at 15:30

2 Answers2

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

<T> declares a formal type variable. So the method returns something of type T.

Class<T> is the class of T, so it requires an argument of type 'the Class that is the class of T'. You'll sometimes see this in Java, since the erasure of generic types makes it necessary to specify the actual class.

Here is an actual example from the Java standard library.

public static <E> List<E> checkedList​(List<E> list, Class<E> type)

Use is like this

List<String> myList2 = Collections.checkedList(myList1, String.class);

The body of a generic method cannot, for example, call new T(), due to type erasure. Given the class object, it can obtain a constructor for the specific class and thus allocate an instance of T.

passer-by
  • 1,103
  • 2
  • 4
2

It gets an object of a certain type.

<T> is a generic. Generics are wildcards for types. This means that in the context of the method, T is a type (like a class or an interface).

The generic is inferred when calling the method but note that generics are only checked at compile-time, not at runtime.

The method signature also declares T as the return type. This means that the method should return an object of the wildcard (generic) type.

Class is an object representing a type. Any type has a corresponding Class-object that can be obtained via YourType.class or objectFromType.getClass().

Class<T> means you have a Class object of the T-type. This is needed since methods cannot know the real type of the generic (this limitation of Java is called type erasure).

<T> T resolve(Class<T> var1);
 ^  ^         ^
 |  |         |- object declaring the type at runtime so the method knows the actual type
 |  |- return type (it returns an object of the generic) 
 |- generic (wildcard type) declaration

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?

As mentioned, the information of the actual type of T is erased at runtime. If the method wants the actual type of the generic, it needs a Class-object.

Returning an instance of a class when just knowing the class may be useful in different scenarios, for example for dependency injection. For the actual use-case, you need to dive deeper in the code (usage, documentation and the implementation of resolve).

dan1st
  • 12,568
  • 8
  • 34
  • 67