{"record":{"id":"9d964b1e46479b73","repo":"alibaba/nacos","slug":"nacostypereference-must-be-created-with-generic-ty","errorCode":null,"errorMessage":"NacosTypeReference must be created with generic type information.","messagePattern":"NacosTypeReference must be created with generic type information\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/com/alibaba/nacos/api/utils/json/NacosTypeReference.java","lineNumber":39,"sourceCode":"\n/**\n * Captures generic type information for JSON deserialization without exposing\n * concrete JSON provider types.\n *\n * @param <T> target type\n * @author nacos\n */\npublic abstract class NacosTypeReference<T> {\n    \n    private final Type type;\n    \n    /**\n     * Create a new type reference and capture generic type from subclass.\n     */\n    protected NacosTypeReference() {\n        Type superClass = getClass().getGenericSuperclass();\n        if (superClass instanceof Class) {\n            throw new IllegalArgumentException(\n                \"NacosTypeReference must be created with generic type information.\");\n        }\n        this.type = ((ParameterizedType) superClass).getActualTypeArguments()[0];\n    }\n    \n    /**\n     * Return captured generic type.\n     *\n     * @return captured type\n     */\n    public Type getType() {\n        return type;\n    }\n}\n","sourceCodeStart":21,"sourceCodeEnd":54,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/api/src/main/java/com/alibaba/nacos/api/utils/json/NacosTypeReference.java#L21-L54","documentation":"NacosTypeReference captures its generic type argument by reflecting on the parameterized superclass. If it is subclassed raw (no type argument), the superclass is a plain Class and the cast to ParameterizedType would fail, so the constructor throws IllegalArgumentException to surface the misuse.","triggerScenarios":"Creating 'new NacosTypeReference(){}' with no generic argument, or subclassing it as a raw type; using a non-anonymous subclass that omits the type parameter.","commonSituations":"Copy-paste of a type-reference pattern without the '<>' generic; refactoring that introduced a raw subclass; framework code that tries to instantiate it reflectively without generics.","solutions":["Always instantiate with a concrete parameterized type, e.g. new NacosTypeReference<List<Instance>>(){}.","Never subclass NacosTypeReference without supplying a type argument.","Add a compile-time check or unit test confirming getType() returns the expected ParameterizedType."],"exampleFix":"// before\nNacosTypeReference ref = new NacosTypeReference(){};  // raw -> throws 554\n\n// after\nNacosTypeReference<List<Instance>> ref = new NacosTypeReference<List<Instance>>(){};","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Compile-time guard: always parameterize. A raw subclass is a programming error.\n// new NacosTypeReference<List<Instance>>() {}  // OK\n// new NacosTypeReference() {}                    // never do this","tryCatchPattern":null,"preventionTips":["Always instantiate NacosTypeReference with a concrete parameterized type.","Never subclass it raw; review any framework code that reflectively creates it.","Add a unit test asserting getType() returns a ParameterizedType."],"tags":["json","generics","reflection","constructor"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}