{"record":{"id":"0d888f095c4fbc6d","repo":"apache/maven","slug":"expected-type-from-key-to-be-parameterized","errorCode":null,"errorMessage":"Expected type from key {} to be parameterized","messagePattern":"Expected type from key (.+?) to be parameterized","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"impl/maven-di/src/main/java/org/apache/maven/di/Key.java","lineNumber":149,"sourceCode":"    /**\n     * A shortcut for <code>{@link Types#getRawType(Type)}(key.getType())</code>.\n     * Also casts the result to a properly parameterized class.\n     */\n    @SuppressWarnings(\"unchecked\")\n    public Class<T> getRawType() {\n        return (Class<T>) Types.getRawType(type);\n    }\n\n    /**\n     * Returns a type parameter of the underlying type wrapped as a key with no qualifier.\n     *\n     * @throws IllegalStateException when underlying type is not a parameterized one.\n     */\n    public <U> Key<U> getTypeParameter(int index) {\n        if (type instanceof ParameterizedType parameterizedType) {\n            return new KeyImpl<>(parameterizedType.getActualTypeArguments()[index], null);\n        }\n        throw new IllegalStateException(\"Expected type from key \" + getDisplayString() + \" to be parameterized\");\n    }\n\n    /**\n     * Returns the qualifier associated with this key, if any.\n     *\n     * @return the qualifier object or null if none exists\n     */\n    public @Nullable Object getQualifier() {\n        return qualifier;\n    }\n\n    /**\n     * Returns an underlying type with display string formatting (package names stripped)\n     * and prepended qualifier display string if this key has a qualifier.\n     */\n    public String getDisplayString() {\n        StringBuilder result = new StringBuilder();\n        if (qualifier instanceof String s) {","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-di/src/main/java/org/apache/maven/di/Key.java#L131-L167","documentation":"Key.getTypeParameter(int) extracts a type argument of the key's underlying java.lang.reflect.Type (the String in List<String>). If the type is not a ParameterizedType — the key was built from a raw class and carries no generic arguments — it throws IllegalStateException. The injector itself calls this while compiling List and Map dependencies, so the error usually surfaces from getInstance/getCompiledBinding.","triggerScenarios":"Requesting a raw generic type from the injector, e.g. getInstance(Key.of(List.class)); requesting a Map key whose arguments are not <String, X>; or calling getTypeParameter directly on a key built from a raw class.","commonSituations":"Refactorings that drop the type argument from an injection point; registering bindings under Class<List> instead of List<Foo>; hand-built keys that erase generic type information.","solutions":["Build the key from a parameterized type so reflection retains the element type (TypeReference-style or a Key.ofType/ParameterizedType construction)","Inject the concrete element type instead of a raw List or Map","Check the key is parameterized before requesting or extracting type parameters"],"exampleFix":"// before\nList<Object> all = injector.getInstance(Key.of(List.class)); // raw key -> IllegalStateException\n\n// after\n// construct the key from a parameterized List<MyService> type so getTypeParameter(0) can resolve MyService\nList<MyService> all = injector.getInstance(listOfMyServiceKey);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isParameterized(Key<?> key) {\n    return key.getType() instanceof java.lang.reflect.ParameterizedType;\n}","tryCatchPattern":"if (!isParameterized(key)) {\n    throw new IllegalArgumentException(\"key must be parameterized, got \" + key.getDisplayString());\n}\nkey.getTypeParameter(0);","preventionTips":["Never create DI keys from raw generic classes","Keep injection points generic all the way through key construction"],"tags":["di","generics","reflection","type-erasure","maven"],"backgroundTag":"missing-generic-type-parameter","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}