scala.ref.WeakReference Scala Examples

The following examples show how to use scala.ref.WeakReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: ResetServiceIT.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.sandbox.services.reset

import com.daml.platform.sandbox.services.SandboxFixture

import scala.concurrent.Await
import scala.concurrent.duration.DurationInt
import scala.ref.WeakReference

final class ResetServiceIT extends ResetServiceITBase with SandboxFixture {
  "ResetService" when {
    "state is reset" should {
      "clear out all garbage" in {
        val state = new WeakReference(Await.result(server.sandboxState, 5.seconds))
        for {
          lid <- fetchLedgerId()
          _ <- reset(lid)
        } yield {
          System.gc()
          state.get should be(None)
        }
      }
    }
  }
} 
Example 2
Source File: ScalaParameterNameDiscoverer.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package spring

import java.lang.reflect.{Constructor, Executable, Method, Modifier}

import com.github.ghik.silencer.silent
import org.springframework.core.{JdkVersion, ParameterNameDiscoverer}

import scala.annotation.tailrec
import scala.ref.WeakReference
import scala.reflect.api.JavaUniverse
import scala.reflect.{ScalaLongSignature, ScalaSignature}

object ScalaParameterNameDiscoverer {
  final val ScalaSignatureClasses =
    List(classOf[ScalaSignature], classOf[ScalaLongSignature])

  @silent("deprecated")
  final val JdkAtLeast8 =
    JdkVersion.getMajorJavaVersion >= JdkVersion.JAVA_18

  // we don't want to keep the universe in memory forever, so we don't use scala.reflect.runtime.universe
  private var universeRef: WeakReference[JavaUniverse] = _

  private def universe: JavaUniverse = {
    universeRef.option.flatMap(_.get) match {
      case Some(result) => result
      case None =>
        val result = new scala.reflect.runtime.JavaUniverse
        universeRef = new WeakReference[JavaUniverse](result)
        result
    }
  }
}

class ScalaParameterNameDiscoverer extends ParameterNameDiscoverer {

  import ScalaParameterNameDiscoverer._

  @tailrec private def isScala(cls: Class[_]): Boolean = cls.getEnclosingClass match {
    case null => ScalaSignatureClasses.exists(ac => cls.getAnnotation(ac) != null)
    case encls => isScala(encls)
  }

  private def discoverNames(u: JavaUniverse)(executable: Executable, symbolPredicate: u.Symbol => Boolean): Array[String] = {
    import u._

    val declaringClass = executable.getDeclaringClass
    val mirror = runtimeMirror(declaringClass.getClassLoader)
    val ownerSymbol =
      if (Modifier.isStatic(executable.getModifiers)) mirror.moduleSymbol(declaringClass).moduleClass.asType
      else mirror.classSymbol(declaringClass)

    def argErasuresMatch(ms: MethodSymbol) =
      ms.paramLists.flatten.map(s => mirror.runtimeClass(s.typeSignature)) == executable.getParameterTypes.toList

    def paramNames(ms: MethodSymbol) =
      ms.paramLists.flatten.map(_.name.toString).toArray

    ownerSymbol.toType.members
      .find(s => symbolPredicate(s) && argErasuresMatch(s.asMethod))
      .map(s => paramNames(s.asMethod))
      .orNull
  }

  def getParameterNames(ctor: Constructor[_]): Array[String] =
    if (JdkAtLeast8 && ctor.getParameters.forall(_.isNamePresent))
      ctor.getParameters.map(_.getName)
    else if (isScala(ctor.getDeclaringClass))
      discoverNames(universe)(ctor, s => s.isConstructor)
    else null

  def getParameterNames(method: Method): Array[String] = {
    val declaringCls = method.getDeclaringClass
    if (JdkAtLeast8 && method.getParameters.forall(_.isNamePresent))
      method.getParameters.map(_.getName)
    else if (isScala(declaringCls)) {
      // https://github.com/scala/bug/issues/10650
      val forStaticForwarder =
        if (Modifier.isStatic(method.getModifiers))
          Class.forName(declaringCls.getName + "$", false, declaringCls.getClassLoader)
            .recoverToOpt[ClassNotFoundException]
            .flatMap(_.getMethod(method.getName, method.getParameterTypes: _*).recoverToOpt[NoSuchMethodException])
            .map(getParameterNames)
        else
          Opt.Empty
      forStaticForwarder.getOrElse(
        discoverNames(universe)(method, s => s.isMethod && s.name.toString == method.getName))
    }
    else null
  }
} 
Example 3
Source File: ActorSystems.scala    From akka-viz   with MIT License 5 votes vote down vote up
package akkaviz.events

import akka.actor.{ActorPath, ActorRef, ActorSystem}
import akka.viz.ActorCellInstrumentation

import scala.collection.breakOut
import scala.ref.WeakReference
import scala.util.Try

object ActorSystems {

  private[this] val systemReferences = scala.collection.mutable.Map[String, WeakReference[ActorSystem]]()

  def systems: scala.collection.immutable.Map[String, ActorSystem] = systemReferences.flatMap {
    case (name, ref) => ref.get.map {
      system => name -> system
    }
  }(breakOut)

  def registerSystem(system: ActorSystem): Unit = {
    systemReferences.update(system.name, WeakReference(system))
  }

  def tell(path: String, message: Any): Unit = {
    Try {
      val actorPath = ActorPath.fromString(path)
      systems.get(actorPath.address.system).foreach {
        system =>
          system.actorSelection(actorPath).tell(message, ActorRef.noSender)
      }
    }
  }

  def refreshActorState(path: String): Unit = {
    tell(path, ActorCellInstrumentation.RefreshInternalStateMsg)
  }

}