diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalCacheFactory.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalCacheFactory.java index 7ae63564b4..0123856a4b 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalCacheFactory.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LocalCacheFactory.java @@ -32,6 +32,7 @@ interface LocalCacheFactory { MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); MethodType FACTORY = MethodType.methodType( void.class, Caffeine.class, AsyncCacheLoader.class, boolean.class); + MethodType FACTORY_CALL = FACTORY.changeReturnType(BoundedLocalCache.class); ConcurrentMap FACTORIES = new ConcurrentHashMap<>(); /** Returns a cache optimized for this configuration. */ @@ -118,10 +119,9 @@ final class MethodHandleBasedFactory implements LocalCacheFactory { final MethodHandle methodHandle; MethodHandleBasedFactory(Class clazz) throws NoSuchMethodException, IllegalAccessException { - var constructor = LOOKUP.findConstructor(clazz, FACTORY); - this.methodHandle = constructor.asType( - constructor.type().changeReturnType(BoundedLocalCache.class)); + this.methodHandle = LOOKUP.findConstructor(clazz, FACTORY).asType(FACTORY_CALL); } + @SuppressWarnings("unchecked") @Override public BoundedLocalCache newInstance(Caffeine builder, @Nullable AsyncCacheLoader cacheLoader, boolean async) throws Throwable { diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/NodeFactory.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/NodeFactory.java index 8faa1ca01e..c7369ae546 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/NodeFactory.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/NodeFactory.java @@ -148,8 +148,7 @@ static NodeFactory newFactory(String className) { try { var clazz = LOOKUP.findClass(Node.class.getPackageName() + "." + className); var constructor = LOOKUP.findConstructor(clazz, FACTORY); - return (NodeFactory) constructor - .asType(constructor.type().changeReturnType(NodeFactory.class)).invokeExact(); + return (NodeFactory) constructor.invoke(); } catch (RuntimeException | Error e) { throw e; } catch (Throwable t) {