|
| 1 | +/** |
| 2 | + * Copyright 2015-2020 Valery Silaev (http://vsilaev.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package net.tascalate.concurrent.core; |
| 17 | + |
| 18 | +import java.lang.invoke.MethodHandle; |
| 19 | +import java.lang.invoke.MethodHandles; |
| 20 | +import java.lang.invoke.MethodType; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.lang.reflect.Modifier; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.CancellationException; |
| 28 | +import java.util.concurrent.CompletionStage; |
| 29 | +import java.util.function.Function; |
| 30 | +import java.util.stream.Stream; |
| 31 | + |
| 32 | +public final class CancelMethodsCache { |
| 33 | + |
| 34 | + @FunctionalInterface |
| 35 | + public static interface Cancellation { |
| 36 | + boolean apply(CompletionStage<?> p, boolean b); |
| 37 | + } |
| 38 | + |
| 39 | + private CancelMethodsCache() {} |
| 40 | + |
| 41 | + public static Cancellation cancellationOf(Class<?> stageClass) { |
| 42 | + return CANCEL_METHODS.get(stageClass, LOOKUP_CANCEL_METHOD); |
| 43 | + } |
| 44 | + |
| 45 | + private static final Cache<Class<?>, Cancellation> CANCEL_METHODS = new Cache<>(); |
| 46 | + private static final Cancellation NO_CANCELATION = (p, b) -> false; |
| 47 | + private static final Function<Class<?>, Cancellation> LOOKUP_CANCEL_METHOD = c -> { |
| 48 | + Stream<Function<Class<?>, ExceptionalCancellation>> options = Stream.of( |
| 49 | + CancelMethodsCache::cancelInterruptibleMethodOf, |
| 50 | + CancelMethodsCache::cancelMethodOf, |
| 51 | + CancelMethodsCache::completeExceptionallyMethodOf |
| 52 | + ); |
| 53 | + return options.map(option -> Optional.ofNullable( option.apply(c) )) |
| 54 | + .filter(Optional::isPresent) |
| 55 | + .map(Optional::get) |
| 56 | + .map(ExceptionalCancellation::unchecked) |
| 57 | + .findFirst() |
| 58 | + .orElse(NO_CANCELATION); |
| 59 | + }; |
| 60 | + |
| 61 | + private static ExceptionalCancellation cancelInterruptibleMethodOf(Class<?> clazz) { |
| 62 | + 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 | + } |
| 74 | + } |
| 75 | + return (p, b) -> (Boolean)m.invoke(p, b); |
| 76 | + } catch (ReflectiveOperationException | SecurityException ex) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static ExceptionalCancellation cancelMethodOf(Class<?> clazz) { |
| 82 | + 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 | + } |
| 94 | + } |
| 95 | + return (p, b) -> (Boolean)m.invoke(p); |
| 96 | + } catch (ReflectiveOperationException | SecurityException ex) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static ExceptionalCancellation completeExceptionallyMethodOf(Class<?> clazz) { |
| 102 | + 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 | + } |
| 113 | + } |
| 114 | + return (p, b) -> (Boolean)m.invoke(p, new CancellationException()); |
| 115 | + } catch (ReflectiveOperationException | SecurityException ex) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static Method firstUnreflectableMethod(Method m) { |
| 121 | + return firstUnreflectableMethod(m.getDeclaringClass(), m, new HashSet<>()); |
| 122 | + } |
| 123 | + |
| 124 | + private static Method firstUnreflectableMethod(Class<?> clazz, Method m, Set<Class<?>> visited) { |
| 125 | + if (visited.contains(clazz)) { |
| 126 | + return null; |
| 127 | + } |
| 128 | + visited.add(clazz); |
| 129 | + if ((clazz.getModifiers() & Modifier.PUBLIC) != 0) { |
| 130 | + try { |
| 131 | + Method parent = clazz.getDeclaredMethod(m.getName(), m.getParameterTypes()); |
| 132 | + if ((parent.getModifiers() & Modifier.PUBLIC) != 0) { |
| 133 | + return parent; |
| 134 | + } else { |
| 135 | + return null; |
| 136 | + } |
| 137 | + } catch (NoSuchMethodException ex) { |
| 138 | + // Ok, will check parents |
| 139 | + } |
| 140 | + } |
| 141 | + return |
| 142 | + Stream.concat(Stream.of(clazz.getSuperclass()), Stream.of(clazz.getInterfaces())) |
| 143 | + .filter(Objects::nonNull) |
| 144 | + .map(superClazz -> firstUnreflectableMethod(superClazz, m, visited)) |
| 145 | + .filter(Objects::nonNull) |
| 146 | + .findFirst() |
| 147 | + .orElse(null) |
| 148 | + ; |
| 149 | + } |
| 150 | + |
| 151 | + @FunctionalInterface |
| 152 | + static interface ExceptionalCancellation { |
| 153 | + boolean apply(CompletionStage<?> p, boolean b) throws Throwable; |
| 154 | + default Cancellation unchecked() { |
| 155 | + return (a, b) -> { |
| 156 | + try { |
| 157 | + return this.apply(a, b); |
| 158 | + } catch (Error | RuntimeException ex) { |
| 159 | + throw ex; |
| 160 | + } catch (Throwable ex) { |
| 161 | + throw new RuntimeException(ex); |
| 162 | + } |
| 163 | + }; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments