diff --git a/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java b/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java index 24213e663b..4bbe32e929 100644 --- a/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java +++ b/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java @@ -3,36 +3,48 @@ package com.microsoft.signalr; +import java.lang.ClassCastException; import java.lang.reflect.Type; import java.lang.reflect.ParameterizedType; + /** - * A utility for getting a Java Type from a literal Class. + * A utility for getting a Java Type from a literal generic Class. */ -public class TypeReference { +public abstract class TypeReference { private final Type type; /** * Creates a new instance of {@link TypeReference}. * - * To get the Type of Class Foo, use the following syntax: + * This class implements Super Type Tokens (Gafter's Gadget) as a way to get a reference to generic types in + * spite of type erasure since, sadly, {@code Foo.class} is not valid Java. + * + * To get the Type of Class {@code Foo}, use the following syntax: *
{@code
-     * Type fooType = (new TypeReference() { }).getType();
+     * Type fooBarType = (new TypeReference>() { }).getType();
      * }
+ * + * To get the Type of class Foo, use a regular Type Token: + *
{@code
+     * Type fooType = Foo.class;
+     * }
+ * + * @see Super Type Tokens */ public TypeReference() { - Type superclass = getClass().getGenericSuperclass(); - if (superclass instanceof Class) { - throw new RuntimeException("Missing type parameter."); + try { + this.type = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; + } catch (ClassCastException ex) { + throw new RuntimeException("TypeReference must be instantiated with a type parameter such as (new TypeReference>() {})."); } - this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; } /** * Gets the referenced type. * @return The Type encapsulated by this TypeReference - */ + */ public Type getType() { return this.type; }