{"record":{"id":"bd6847b88de3ad9d","repo":"quarkusio/quarkus","slug":"unable-to-instantiate-weigher-class-classname","errorCode":null,"errorMessage":"Unable to instantiate weigher class: <className>","messagePattern":"Unable to instantiate weigher class: <className>","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/cache/QuarkusPersistenceUnitCaffeineCacheManager.java","lineNumber":121,"sourceCode":"        }\n\n        caffeineConfig.setExpireAfterAccess(OptionalLong.of(quarkusConfig.maxIdle().toNanos()));\n        return delegate.createCache(cacheName, caffeineConfig);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    private <K, V> Weigher<K, V> instantiateWeigher(String className) {\n        try {\n            Class<?> weigherClass = Thread.currentThread().getContextClassLoader().loadClass(className);\n            if (!Weigher.class.isAssignableFrom(weigherClass)) {\n                throw new IllegalStateException(\n                        \"Weigher class '\" + className + \"' must implement com.github.benmanes.caffeine.cache.Weigher\");\n            }\n            return (Weigher<K, V>) weigherClass.getConstructor().newInstance();\n        } catch (IllegalStateException e) {\n            throw e;\n        } catch (Exception e) {\n            throw new IllegalStateException(\"Unable to instantiate weigher class: \" + className, e);\n        }\n    }\n\n    @Override\n    public Iterable<String> getCacheNames() {\n        return delegate.getCacheNames();\n    }\n\n    @Override\n    public void destroyCache(String cacheName) {\n        throw new UnsupportedOperationException(\"This should not be used by Hibernate ORM\");\n    }\n\n    @Override\n    public void enableManagement(String cacheName, boolean enabled) {\n        throw new UnsupportedOperationException(\"This should not be used by Hibernate ORM\");\n    }\n","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/cache/QuarkusPersistenceUnitCaffeineCacheManager.java#L103-L139","documentation":"instantiateWeigher() creates the configured weigher via weigherClass.getConstructor().newInstance(). Any failure while loading, constructing, or reflectively instantiating the class (other than the interface check) is wrapped in this IllegalStateException naming the class that could not be instantiated.","triggerScenarios":"Configuring a cache weigher class whose class name cannot be loaded (ClassNotFoundException), which has no public no-arg constructor (NoSuchMethodException), whose constructor throws (InvocationTargetException), or whose constructor is not accessible (IllegalAccessException).","commonSituations":"Typo in the fully-qualified class name; weigher class missing from the packaged artifact or not registered for reflection in native mode; weigher with only parameterized constructors; weigher constructor that performs failing initialization (e.g. reads a missing resource).","solutions":["Check the class name spelling/package in the config and confirm the class is on the application classpath (and in the native image if running native).","Add a public no-arg constructor to the weigher class.","In native mode, register the weigher class for reflection (e.g. @RegisterForReflection or reflect-config.json).","Debug the wrapped cause (getCause() in the log) to see whether it was load failure vs constructor failure, and fix accordingly."],"exampleFix":"// before\nclass MyWeigher implements Weigher<Object,Object> {\n  MyWeigher(int weightFactor) { ... }\n}\n\n// after: public no-arg constructor\nclass MyWeigher implements Weigher<Object,Object> {\n  private final int weightFactor = 1;\n  public MyWeigher() { }\n  public int weigh(Object k, Object v) { return weightFactor; }\n}","handlingStrategy":"validation","validationCode":"// Verify the class loads and can be constructed before configuring it\nClass<?> c = Class.forName(weigherClassName);\njava.lang.reflect.Constructor<?> ctor = c.getDeclaredConstructor();\nif (!java.lang.reflect.Modifier.isPublic(ctor.getModifiers())\n        || !java.lang.reflect.Modifier.isPublic(c.getModifiers())) {\n    throw new IllegalStateException(weigherClassName + \" must have a public no-arg constructor\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    startPu();\n} catch (IllegalStateException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Unable to instantiate weigher class\")) {\n        log.error(\"Weigher class missing, not public, or constructor threw; cause: \"\n            + (e.getCause() == null ? \"?\" : e.getCause()), e);\n    }\n    throw e;\n}","preventionTips":["Give weigher classes a public no-arg constructor","In native mode, register the weigher for reflection (@RegisterForReflection)","Keep the fully-qualified class name in a constant shared between test and config to avoid typos","Smoke-test cache config early in application startup"],"tags":["hibernate-orm","caffeine","cache","reflection","quarkus"],"backgroundTag":"class-instantiation-failed","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}