{"record":{"id":"06c30b65af07f1fe","repo":"dotnet/aspnetcore","slug":"typereference-must-be-instantiated-with-a-type-par","errorCode":null,"errorMessage":"TypeReference must be instantiated with a type parameter such as (new TypeReference<Foo<Bar>>() {}).","messagePattern":"TypeReference must be instantiated with a type parameter such as \\(new TypeReference<Foo<Bar>>\\(\\) (.+?)\\)\\.","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java","lineNumber":40,"sourceCode":"     * spite of type erasure since, sadly, {@code Foo<Bar>.class} is not valid Java.\n     *\n     * To get the Type of Class {@code Foo<Bar>}, use the following syntax:\n     * <pre>{@code\n     * Type fooBarType = (new TypeReference<Foo<Bar>>() { }).getType();\n     * }</pre>\n     *\n     * To get the Type of class Foo, use a regular Type Token:\n     * <pre>{@code\n     * Type fooType = Foo.class;\n     * }</pre>\n     *\n     *  @see <a href=\"http://gafter.blogspot.com/2006/12/super-type-tokens.html\">Super Type Tokens</a>\n     */\n    public TypeReference() {\n        try {\n            this.type = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];\n        } catch (ClassCastException ex) {\n            throw new RuntimeException(\"TypeReference must be instantiated with a type parameter such as (new TypeReference<Foo<Bar>>() {}).\");\n        }\n    }\n\n    /**\n     * Gets the referenced type.\n     * @return The Type encapsulated by this TypeReference\n     */\n    public Type getType() {\n        return this.type;\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/SignalR/clients/java/signalr/core/src/main/java/com/microsoft/signalr/TypeReference.java#L22-L52","documentation":"TypeReference uses super-type tokens (Gafter's Gadget) to reify generic types despite erasure. It requires an anonymous subclass that supplies a concrete type argument, e.g. new TypeReference<Foo<Bar>>(){}. If instantiated as a raw new TypeReference(), the generic superclass is not a ParameterizedType and the cast fails.","triggerScenarios":"Writing new TypeReference() with no type argument; omitting the {} anonymous-class body; assigning to a raw TypeReference variable; subclassing TypeReference concretely without a fixed generic.","commonSituations":"Deserializing a generic hub-method return type (List<Foo>, Map<String,Integer>); IDE auto-completed the constructor without the generic braces; copy-paste dropped the (){}.","solutions":["Always instantiate as an anonymous class with the concrete type argument: new TypeReference<Map<String,Integer>>(){}.","For non-generic types use the plain Class token (Foo.class) instead of TypeReference."],"exampleFix":"// before\nType t = new TypeReference();\n\n// after\nType t = new TypeReference<Map<String, Integer>>() {}.getType();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Java - ensure a TypeReference is parameterized before using it\nstatic boolean isParameterized(TypeReference<?> ref) {\n    return ref.getClass().getGenericSuperclass() instanceof java.lang.reflect.ParameterizedType;\n}","tryCatchPattern":null,"preventionTips":["Always write TypeReference with braces and a concrete generic: new TypeReference<Map<String,Integer>>(){}.","Use plain Class tokens (Foo.class) for non-generic types.","Review IDE auto-completion that may drop the (){} suffix."],"tags":["signalr","generics","type-erasure","java"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}