Skip to content

Commit

Permalink
Fix issue #1111 by not creating methodType on each invocation (to wor…
Browse files Browse the repository at this point in the history
…k around a possible concurrency bug in JDK) (#1114)
  • Loading branch information
uschindler committed Aug 8, 2023
1 parent b1462dd commit 82cf9a3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, LocalCacheFactory> FACTORIES = new ConcurrentHashMap<>();

/** Returns a cache optimized for this configuration. */
Expand Down Expand Up @@ -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 <K, V> BoundedLocalCache<K, V> newInstance(Caffeine<K, V> builder,
@Nullable AsyncCacheLoader<? super K, V> cacheLoader, boolean async) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ static NodeFactory<Object, Object> newFactory(String className) {
try {
var clazz = LOOKUP.findClass(Node.class.getPackageName() + "." + className);
var constructor = LOOKUP.findConstructor(clazz, FACTORY);
return (NodeFactory<Object, Object>) constructor
.asType(constructor.type().changeReturnType(NodeFactory.class)).invokeExact();
return (NodeFactory<Object, Object>) constructor.invoke();
} catch (RuntimeException | Error e) {
throw e;
} catch (Throwable t) {
Expand Down

0 comments on commit 82cf9a3

Please sign in to comment.