|
| 1 | +package scala.compat.java8.runtime |
| 2 | + |
| 3 | +import java.lang.invoke._ |
| 4 | +import java.lang.ref.WeakReference |
| 5 | + |
| 6 | +/** |
| 7 | + * This class is only intended to be called by synthetic `$deserializeLambda$` method that the Scala 2.12 |
| 8 | + * compiler will add to classes hosting lambdas. |
| 9 | + * |
| 10 | + * It is intended to be consumed directly. |
| 11 | + */ |
| 12 | +object LambdaDeserializer { |
| 13 | + private final case class CacheKey(implClass: Class[_], implMethodName: String, implMethodSignature: String) |
| 14 | + private val cache = new java.util.WeakHashMap[CacheKey, WeakReference[CallSite]]() |
| 15 | + |
| 16 | + /** |
| 17 | + * Deserialize a lambda by calling `LambdaMetafactory.altMetafactory` to spin up a lambda class |
| 18 | + * and instantiating this class with the captured arguments. |
| 19 | + * |
| 20 | + * A cache is employed to ensure that subsequent deserialization of the same lambda expression |
| 21 | + * is cheap, it amounts to a reflective call to the constructor of the previously created class. |
| 22 | + * However, deserialization of the same lambda expression is not guaranteed to use the same class, |
| 23 | + * concurrent deserialization of the same lambda expression may spin up more than one class. |
| 24 | + * |
| 25 | + * This cache is weak in keys and values to avoid retention of the enclosing class (and its classloader) |
| 26 | + * of deserialized lambdas. |
| 27 | + * |
| 28 | + * Assumptions: |
| 29 | + * - No additional marker interfaces are required beyond `{java.io,scala.}Serializable`. These are |
| 30 | + * not stored in `SerializedLambda`, so we can't reconstitute them. |
| 31 | + * - No additional bridge methods are passed to `altMetafactory`. Again, these are not stored. |
| 32 | + * |
| 33 | + * Note: The Java compiler |
| 34 | + * |
| 35 | + * @param lookup The factory for method handles. Must have access to the implementation method, the |
| 36 | + * functional interface class, and `java.io.Serializable` or `scala.Serializable` as |
| 37 | + * required. |
| 38 | + * @param serialized The lambda to deserialize. Note that this is typically created by the `readResolve` |
| 39 | + * member of the anonymous class created by `LambdaMetaFactory`. |
| 40 | + * @return An instance of the functional interface |
| 41 | + */ |
| 42 | + def deserializeLambda(lookup: MethodHandles.Lookup, serialized: SerializedLambda): AnyRef = { |
| 43 | + def slashDot(name: String) = name.replaceAll("/", ".") |
| 44 | + val loader = lookup.lookupClass().getClassLoader |
| 45 | + val implClass = loader.loadClass(slashDot(serialized.getImplClass)) |
| 46 | + |
| 47 | + def makeCallSite: CallSite = { |
| 48 | + import serialized._ |
| 49 | + def parseDescriptor(s: String) = |
| 50 | + MethodType.fromMethodDescriptorString(s, loader) |
| 51 | + |
| 52 | + val funcInterfacesSignature = parseDescriptor(getFunctionalInterfaceMethodSignature) |
| 53 | + val methodType: MethodType = funcInterfacesSignature |
| 54 | + val instantiated = parseDescriptor(getInstantiatedMethodType) |
| 55 | + val implMethodSig = parseDescriptor(getImplMethodSignature) |
| 56 | + |
| 57 | + val from = implMethodSig.parameterCount() - funcInterfacesSignature.parameterCount() |
| 58 | + val to = implMethodSig.parameterCount() |
| 59 | + val functionalInterfaceClass = loader.loadClass(slashDot(getFunctionalInterfaceClass)) |
| 60 | + var invokedType: MethodType = |
| 61 | + implMethodSig.dropParameterTypes(from, to) |
| 62 | + .changeReturnType(functionalInterfaceClass) |
| 63 | + |
| 64 | + val implMethod: MethodHandle = try { |
| 65 | + getImplMethodKind match { |
| 66 | + case MethodHandleInfo.REF_invokeStatic => |
| 67 | + lookup.findStatic(implClass, getImplMethodName, implMethodSig) |
| 68 | + case MethodHandleInfo.REF_invokeVirtual => |
| 69 | + invokedType = invokedType.insertParameterTypes(0, implClass) |
| 70 | + lookup.findVirtual(implClass, getImplMethodName, implMethodSig) |
| 71 | + case MethodHandleInfo.REF_invokeSpecial => |
| 72 | + invokedType = invokedType.insertParameterTypes(0, implClass) |
| 73 | + lookup.findSpecial(implClass, getImplMethodName, implMethodSig, implClass) |
| 74 | + } |
| 75 | + } catch { |
| 76 | + case e: ReflectiveOperationException => |
| 77 | + throw new IllegalArgumentException("Illegal lambda deserialization", e) |
| 78 | + } |
| 79 | + val FLAG_SERIALIZABLE = 1 |
| 80 | + val FLAG_MARKERS = 2 |
| 81 | + val flags: Int = FLAG_SERIALIZABLE | FLAG_MARKERS |
| 82 | + val markerInterface: Class[_] = if (functionalInterfaceClass.getName.startsWith("scala.Function")) |
| 83 | + loader.loadClass("scala.Serializable") |
| 84 | + else |
| 85 | + loader.loadClass("java.io.Serializable") |
| 86 | + |
| 87 | + LambdaMetafactory.altMetafactory( |
| 88 | + lookup, getFunctionalInterfaceMethodName, invokedType, |
| 89 | + |
| 90 | + /* samMethodType = */ funcInterfacesSignature, |
| 91 | + /* implMethod = */ implMethod, |
| 92 | + /* instantiatedMethodType = */ instantiated, |
| 93 | + /* flags = */ flags.asInstanceOf[AnyRef], |
| 94 | + /* markerInterfaceCount = */ 1.asInstanceOf[AnyRef], |
| 95 | + /* markerInterfaces[0] = */ markerInterface, |
| 96 | + /* bridgeCount = */ 0.asInstanceOf[AnyRef] |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + val key = new CacheKey(implClass, serialized.getImplMethodName, serialized.getImplMethodSignature) |
| 101 | + val callSiteRef: WeakReference[CallSite] = cache.get(key) |
| 102 | + var site = if (callSiteRef == null) null else callSiteRef.get() |
| 103 | + if (site == null) { |
| 104 | + site = makeCallSite |
| 105 | + cache.put(key, new WeakReference(site)) |
| 106 | + } |
| 107 | + |
| 108 | + val factory = site.getTarget |
| 109 | + val captures = Array.tabulate(serialized.getCapturedArgCount)(n => serialized.getCapturedArg(n)) |
| 110 | + factory.invokeWithArguments(captures: _*) |
| 111 | + } |
| 112 | +} |
0 commit comments