Skip to content

Commit 5484aae

Browse files
committed
Simplify cancel methods cache
1 parent 31c5d53 commit 5484aae

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

src/main/java/net/tascalate/concurrent/core/Cache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
import java.util.function.Function;
2424

2525

26-
public final class Cache<K, V> {
26+
final class Cache<K, V> {
2727
private final Map<Reference<K>, V> entries = new ConcurrentHashMap<>();
2828
private final ReferenceQueue<K> queue = new ReferenceQueue<K>();
2929

30-
public V get(K key, Function<? super K, ? extends V> valueFactory) {
30+
V get(K key, Function<? super K, ? extends V> valueFactory) {
3131
expungeStaleEntries();
3232
return entries.computeIfAbsent(new KeyReference<>(key), __ -> valueFactory.apply(key));
3333
}

src/main/java/net/tascalate/concurrent/core/CancelMethodsCache.java

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.concurrent.CancellationException;
2828
import java.util.concurrent.CompletionStage;
2929
import java.util.function.Function;
30+
import java.util.function.Supplier;
3031
import java.util.stream.Stream;
3132

3233
public final class CancelMethodsCache {
@@ -60,58 +61,41 @@ public static Cancellation cancellationOf(Class<?> stageClass) {
6061

6162
private static ExceptionalCancellation cancelInterruptibleMethodOf(Class<?> clazz) {
6263
try {
63-
Method m = clazz.getMethod("cancel", boolean.class);
64-
Method mf = firstUnreflectableMethod(m);
65-
if (null != mf) {
66-
try {
67-
MethodHandle mh = MethodHandles.publicLookup()
68-
.unreflect(mf)
69-
.asType(MethodType.methodType(boolean.class, CompletionStage.class, boolean.class));
70-
return (p, b) -> (boolean)mh.invokeExact(p, b);
71-
} catch (IllegalAccessException ex) {
72-
// will try reflective
73-
}
64+
Method m = firstUnreflectableMethod( clazz.getMethod("cancel", boolean.class) );
65+
if (null == m) {
66+
return null;
7467
}
75-
return (p, b) -> (Boolean)m.invoke(p, b);
68+
MethodHandle mh = unreflect(m).asType(MethodType.methodType(boolean.class, CompletionStage.class, boolean.class));
69+
return (p, b) -> (boolean)mh.invokeExact(p, b);
70+
//return (p, b) -> (Boolean)m.invoke(p, b);
7671
} catch (ReflectiveOperationException | SecurityException ex) {
7772
return null;
7873
}
7974
}
8075

8176
private static ExceptionalCancellation cancelMethodOf(Class<?> clazz) {
8277
try {
83-
Method m = clazz.getMethod("cancel");
84-
Method mf = firstUnreflectableMethod(m);
85-
if (null != mf) {
86-
try {
87-
MethodHandle mh = MethodHandles.publicLookup()
88-
.unreflect(mf)
89-
.asType(MethodType.methodType(boolean.class, CompletionStage.class));
90-
return (p, b) -> (boolean)mh.invokeExact(p);
91-
} catch (IllegalAccessException ex) {
92-
// will try reflective
93-
}
78+
Method m = firstUnreflectableMethod( clazz.getMethod("cancel") );
79+
if (null == m) {
80+
return null;
9481
}
95-
return (p, b) -> (Boolean)m.invoke(p);
82+
MethodHandle mh = unreflect(m).asType(MethodType.methodType(boolean.class, CompletionStage.class));
83+
return (p, b) -> (boolean)mh.invokeExact(p);
84+
//return (p, b) -> (Boolean)m.invoke(p);
9685
} catch (ReflectiveOperationException | SecurityException ex) {
9786
return null;
9887
}
9988
}
10089

10190
private static ExceptionalCancellation completeExceptionallyMethodOf(Class<?> clazz) {
10291
try {
103-
Method m = clazz.getMethod("completeExceptionally", Throwable.class);
104-
Method mf = firstUnreflectableMethod(m);
105-
if (null != mf) {
106-
try {
107-
MethodHandle mh = MethodHandles.publicLookup()
108-
.unreflect(mf)
109-
.asType(MethodType.methodType(boolean.class, CompletionStage.class, CancellationException.class));
110-
return (p, b) -> (boolean)mh.invokeExact(p, new CancellationException());
111-
} catch (IllegalAccessException ex) {
112-
}
92+
Method m = firstUnreflectableMethod( clazz.getMethod("completeExceptionally", Throwable.class) );
93+
if (null == m) {
94+
return null;
11395
}
114-
return (p, b) -> (Boolean)m.invoke(p, new CancellationException());
96+
MethodHandle mh = unreflect(m).asType(MethodType.methodType(boolean.class, CompletionStage.class, CancellationException.class));
97+
return (p, b) -> (boolean)mh.invokeExact(p, new CancellationException());
98+
//return (p, b) -> (Boolean)m.invoke(p, new CancellationException());
11599
} catch (ReflectiveOperationException | SecurityException ex) {
116100
return null;
117101
}
@@ -139,15 +123,19 @@ private static Method firstUnreflectableMethod(Class<?> clazz, Method m, Set<Cla
139123
}
140124
}
141125
return
142-
Stream.concat(Stream.of(clazz.getSuperclass()), Stream.of(clazz.getInterfaces()))
143-
.filter(Objects::nonNull)
126+
Stream.concat( Stream.of(clazz.getSuperclass()), Stream.of(clazz.getInterfaces()) )
127+
.filter(c -> c != null && !visited.contains(c))
144128
.map(superClazz -> firstUnreflectableMethod(superClazz, m, visited))
145129
.filter(Objects::nonNull)
146130
.findFirst()
147131
.orElse(null)
148132
;
149133
}
150134

135+
private static MethodHandle unreflect(Method m) throws IllegalAccessException {
136+
return MethodHandles.publicLookup().unreflect(m);
137+
}
138+
151139
@FunctionalInterface
152140
static interface ExceptionalCancellation {
153141
boolean apply(CompletionStage<?> p, boolean b) throws Throwable;

0 commit comments

Comments
 (0)