{"record":{"id":"eed6eb69b347ef81","repo":"jwtk/jjwt","slug":"message-object-of-class-objclassname-must-b","errorCode":null,"errorMessage":"${message}Object of class [${objClassName}] must be an instance of ${type}","messagePattern":"(.+?)Object of class \\[(.+?)\\] must be an instance of (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/jsonwebtoken/lang/Assert.java","lineNumber":384,"sourceCode":"    /**\n     * Assert that the provided object is an instance of the provided class.\n     * <pre class=\"code\">Assert.instanceOf(Foo.class, foo);</pre>\n     *\n     * @param type    the type to check against\n     * @param <T>     the object's expected type\n     * @param obj     the object to check\n     * @param message a message which will be prepended to the message produced by\n     *                the function itself, and which may be used to provide context. It should\n     *                normally end in a \": \" or \". \" so that the function generate message looks\n     *                ok when prepended to it.\n     * @return the non-null object IFF it is an instance of the specified {@code type}.\n     * @throws IllegalArgumentException if the object is not an instance of clazz\n     * @see Class#isInstance\n     */\n    public static <T> T isInstanceOf(Class<T> type, Object obj, String message) {\n        notNull(type, \"Type to check against must not be null\");\n        if (!type.isInstance(obj)) {\n            throw new IllegalArgumentException(message +\n                    \"Object of class [\" + (obj != null ? obj.getClass().getName() : \"null\") +\n                    \"] must be an instance of \" + type);\n        }\n        return type.cast(obj);\n    }\n\n    /**\n     * Asserts that the provided object is an instance of the provided class, throwing an\n     * {@link IllegalStateException} otherwise.\n     * <pre class=\"code\">Assert.stateIsInstance(Foo.class, foo);</pre>\n     *\n     * @param type    the type to check against\n     * @param <T>     the object's expected type\n     * @param obj     the object to check\n     * @param message a message which will be prepended to the message produced by\n     *                the function itself, and which may be used to provide context. It should\n     *                normally end in a \": \" or \". \" so that the function generate message looks\n     *                ok when prepended to it.","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/api/src/main/java/io/jsonwebtoken/lang/Assert.java#L366-L402","documentation":"Thrown by Assert.isInstanceOf(Class, Object, String) when the object is not an instance of the required type. The message concatenates the caller's prefix plus 'Object of class [X] must be an instance of Y'. JJWT uses this to enforce internal type contracts (e.g. expected key or provider types), surfacing as IllegalArgumentException.","triggerScenarios":"Passing an object of the wrong concrete type where a specific class is required, e.g. a custom Key implementation that isn't the expected interface/class, or a pluggable component (Serializer, Clock, compressor) of the wrong type handed to a builder or factory.","commonSituations":"Custom implementations after a library upgrade changed the expected interface; reflection-based instantiation returning a supertype; misconfigured plugins/factories returning the wrong class; mixing jjwt 0.11 and 0.12 API types on the classpath.","solutions":["Ensure the object is an instance of the required type (implement/extend it)","Check obj.getClass() against the expected type before the call","Verify you are not mixing incompatible library versions (e.g. old deprecated API types)","If using reflection/factories, cast or validate the produced instance"],"exampleFix":"// before\nObject key = loadKey(); // returns Object\nJwts.builder().signWith((Key) key); // may fail isInstanceOf\n// after\nKey key = loadKey();\nif (!(key instanceof SecretKey)) { throw new IllegalArgumentException(\"need SecretKey\"); }\nJwts.builder().signWith((SecretKey) key);","handlingStrategy":"type-guard","validationCode":"if (obj == null || !requiredType.isInstance(obj)) {\n    throw new IllegalArgumentException(\"Expected \" + requiredType.getName()\n        + \", got: \" + (obj == null ? \"null\" : obj.getClass().getName()));\n}","typeGuard":"if (!(obj instanceof SecretKey)) {\n    throw new IllegalArgumentException(\"Key must be a SecretKey\");\n}\nSecretKey key = (SecretKey) obj;","tryCatchPattern":"try {\n    Assert.isInstanceOf(Key.class, obj, \"Invalid key: \");\n} catch (IllegalArgumentException e) {\n    log.error(\"Type contract violated: {}\", e.getMessage());\n    throw e;\n}","preventionTips":["Program against interfaces and check instanceof before casts","Keep library versions consistent across modules (no mixed 0.11/0.12 types)","Validate objects returned from reflection/ServiceLoader before use"],"tags":["jwt","type-mismatch","assertion"],"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"}