{"record":{"id":"ea37e3a2c9f0fd6d","repo":"jwtk/jjwt","slug":"cannot-cast-value-getclass-getname-to-idi","errorCode":null,"errorMessage":"Cannot cast ${value.getClass().getName()} to ${IDIOMATIC_TYPE.getName()}","messagePattern":"Cannot cast (.+?) to (.+?)","errorType":"validation","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/lang/DefaultParameter.java","lineNumber":88,"sourceCode":"            if (COLLECTION_TYPE != null) { // parameter represents a collection, ensure it and its elements are the expected type:\n                if (!COLLECTION_TYPE.isInstance(value)) {\n                    String msg = \"Cannot cast \" + value.getClass().getName() + \" to \" +\n                            COLLECTION_TYPE.getName() + \"<\" + IDIOMATIC_TYPE.getName() + \">\";\n                    throw new ClassCastException(msg);\n                }\n                Collection<?> c = COLLECTION_TYPE.cast(value);\n                if (!c.isEmpty()) {\n                    Object element = c.iterator().next();\n                    if (!IDIOMATIC_TYPE.isInstance(element)) {\n                        String msg = \"Cannot cast \" + value.getClass().getName() + \" to \" +\n                                COLLECTION_TYPE.getName() + \"<\" + IDIOMATIC_TYPE.getName() + \">: At least one \" +\n                                \"element is not an instance of \" + IDIOMATIC_TYPE.getName();\n                        throw new ClassCastException(msg);\n                    }\n                }\n            } else if (!IDIOMATIC_TYPE.isInstance(value)) {\n                String msg = \"Cannot cast \" + value.getClass().getName() + \" to \" + IDIOMATIC_TYPE.getName();\n                throw new ClassCastException(msg);\n            }\n        }\n        return (T) value;\n    }\n\n    @Override\n    public boolean isSecret() {\n        return SECRET;\n    }\n\n    @Override\n    public int hashCode() {\n        return this.ID.hashCode();\n    }\n\n    @Override\n    public boolean equals(Object obj) {\n        if (obj instanceof Parameter) {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/lang/DefaultParameter.java#L70-L106","documentation":"DefaultParameter.cast, for non-collection parameters, checks the value is an instance of the declared type (IDIOMATIC_TYPE) and throws a ClassCastException otherwise. This is the plain scalar-path type guard applied to every typed header/claim parameter.","triggerScenarios":"Setting a claim/header value whose runtime type differs from the registered parameter type, e.g. an Integer where a String is expected, or a String where a Date/Long parameter expects a number; also triggered during JWT deserialization when the JSON type doesn't match the schema.","commonSituations":"External issuers emitting numeric claims the library declares as strings; copying claims between tokens with different schemas; using raw/untyped builders that bypass generics.","solutions":["Convert the value to the declared type before assignment (String.valueOf(x), Long.parseLong(s), etc.).","Inspect the token's raw JSON to see the actual type and adjust the value or the parameter registration.","When reading, cast defensively via instanceof checks before using the claim value."],"exampleFix":"// before\nObject exp = claims.get(\"exp\"); // Integer\nLong l = (Long) exp; // ClassCastException via parameter cast\n// after\nObject exp = claims.get(\"exp\");\nLong l = (exp instanceof Number) ? ((Number) exp).longValue() : Long.parseLong(exp.toString());","handlingStrategy":"type-guard","validationCode":"if (value != null && !expectedType.isInstance(value)) {\n    value = convert(value, expectedType); // String.valueOf, Long.parseLong, ...\n}","typeGuard":"static <T> T asType(Object v, Class<T> t) {\n    return t.isInstance(v) ? t.cast(v) : null;\n}","tryCatchPattern":"try {\n    String s = (String) claims.get(\"sub\");\n} catch (ClassCastException e) {\n    s = String.valueOf(claims.get(\"sub\"));\n}","preventionTips":["Use instanceof checks instead of blind casts on claim values","Read claim values as Object and convert explicitly","Compare your parameter registrations with the actual token JSON types"],"tags":["classcast","type-mismatch","jwt-claims"],"backgroundTag":"type-mismatch","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}