quarkusio/quarkus · error · CacheException

Cache key generator instantiation failed

Error message

Cache key generator instantiation failed

What it means

When no CDI bean of the configured key generator class is resolvable, CacheInterceptor reflectively instantiates it via its default constructor. If that reflective instantiation fails (InstantiationException, IllegalAccessException, or InvocationTargetException from the constructor), it is wrapped in a CacheException.

Source

Thrown at extensions/cache/runtime/src/main/java/io/quarkus/cache/runtime/CacheInterceptor.java:219

            try {
                return keyGen.get().generate(method, methodParameterValues);
            } finally {
                Bean<T> bean = keyGen.getBean();
                if (bean != null && Dependent.class.equals(bean.getScope())) {
                    // Destroy @Dependent beans afterwards
                    keyGen.destroy();
                }
            }
        } else {
            try {
                LOGGER.tracef("Creating a new cache key generator instance [class=%s]", keyGeneratorClass.getName());
                return keyGeneratorClass.getConstructor().newInstance().generate(method, methodParameterValues);
            } catch (NoSuchMethodException e) {
                // This should never be thrown because the default constructor availability is checked at build time.
                throw new CacheException("No default constructor found in cache key generator [class="
                        + keyGeneratorClass.getName() + "]", e);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                throw new CacheException("Cache key generator instantiation failed", e);
            }
        }
    }

    protected static ReturnType determineReturnType(Class<?> returnType) {
        if (Uni.class.isAssignableFrom(returnType)) {
            return ReturnType.Uni;
        }
        if (CompletionStage.class.isAssignableFrom(returnType)) {
            return ReturnType.CompletionStage;
        }
        return ReturnType.NonAsync;
    }

    protected Uni<?> asyncInvocationResultToUni(Object invocationResult, ReturnType returnType) {
        if (returnType == ReturnType.Uni) {
            return (Uni<?>) invocationResult;
        } else if (returnType == ReturnType.CompletionStage) {

View on GitHub (pinned to e1c734241f)

Solutions

  1. Make the key generator a public, static, concrete class with a public no-arg constructor whose body cannot throw.
  2. Register it as a @Dependent CDI bean so the interceptor uses the managed instance path instead of reflection.
  3. Inspect the wrapped cause (e.getCause()) to find what the constructor threw.

Example fix

// before
public class MyKeyGen {
    public MyKeyGen() { loadFromDb(); } // throws
}

// after
@Dependent
public class MyKeyGen implements CacheKeyGenerator {
    public MyKeyGen() { }
    public Object generate(Method m, Object... params) { ... }
}
Defensive patterns

Strategy: try-catch

Validate before calling

Class<?> gen = MyKeyGen.class;
if (java.lang.reflect.Modifier.isAbstract(gen.getModifiers())) {
    throw new IllegalStateException("Key generator must be concrete");
}
gen.getDeclaredConstructor(); // fails fast if no no-arg ctor
if (!java.lang.reflect.Modifier.isPublic(gen.getModifiers())) {
    throw new IllegalStateException("Key generator must be public");
}

Try / catch

try {
    Object key = interceptor.generateKey(gen, method, params);
} catch (CacheException e) {
    LOGGER.error("Key generator failed", e.getCause());
    throw e;
}

Prevention

When it happens

Trigger: A @CacheKeyGenerator class whose constructor throws an exception, is not public, or is abstract/non-instantiable, and which is not registered as a CDI bean (so the reflective path is taken).

Common situations: Key generator with a constructor that does heavy init and throws; inner (non-static) classes whose implicit outer reference makes the no-arg constructor inaccessible; abstract base class mistakenly referenced in @CacheResult(keyGenerator=...).

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/97b83759d902262a. Report an issue: GitHub.