fixing up final misnamers (createClassFor -> getClassFor)

This commit is contained in:
Roland 2012-02-10 14:36:35 +01:00
parent 09897459d6
commit 4b71872aef
6 changed files with 37 additions and 45 deletions

View file

@ -16,36 +16,6 @@ import java.lang.reflect.InvocationTargetException
*/ */
trait DynamicAccess { trait DynamicAccess {
/**
* Obtain a `Class[_]` object loaded with the right class loader (i.e. the one
* returned by `classLoader`).
*/
def createClassFor[T: ClassManifest](fqcn: String): Either[Throwable, Class[_ <: T]]
/**
* Obtain an object conforming to the type T, which is expected to be
* instantiated from a class designated by the fully-qualified class name
* given, where the constructor is selected and invoked according to the
* `args` argument. The exact usage of args depends on which type is requested,
* see the relevant requesting code for details.
*/
def createInstanceFor[T: ClassManifest](fqcn: String, args: Seq[(Class[_], AnyRef)]): Either[Throwable, T]
/**
* Obtain the Scala object instance for the given fully-qualified class name, if there is one.
*/
def getObjectFor[T: ClassManifest](fqcn: String): Either[Throwable, T]
/**
* This is the class loader to be used in those special cases where the
* other factory method are not applicable (e.g. when constructing a ClassLoaderBinaryInputStream).
*/
def classLoader: ClassLoader
}
object DynamicAccess {
/** /**
* Convenience method which given a `Class[_]` object and a constructor description * Convenience method which given a `Class[_]` object and a constructor description
* will create a new instance of that class. * will create a new instance of that class.
@ -66,6 +36,32 @@ object DynamicAccess {
} }
} }
/**
* Obtain a `Class[_]` object loaded with the right class loader (i.e. the one
* returned by `classLoader`).
*/
def getClassFor[T: ClassManifest](fqcn: String): Either[Throwable, Class[_ <: T]]
/**
* Obtain an object conforming to the type T, which is expected to be
* instantiated from a class designated by the fully-qualified class name
* given, where the constructor is selected and invoked according to the
* `args` argument. The exact usage of args depends on which type is requested,
* see the relevant requesting code for details.
*/
def createInstanceFor[T: ClassManifest](fqcn: String, args: Seq[(Class[_], AnyRef)]): Either[Throwable, T]
/**
* Obtain the Scala object instance for the given fully-qualified class name, if there is one.
*/
def getObjectFor[T: ClassManifest](fqcn: String): Either[Throwable, T]
/**
* This is the class loader to be used in those special cases where the
* other factory method are not applicable (e.g. when constructing a ClassLoaderBinaryInputStream).
*/
def classLoader: ClassLoader
/** /**
* Caught exception is returned as Left(exception). * Caught exception is returned as Left(exception).
* Unwraps `InvocationTargetException` if its getTargetException is an `Exception`. * Unwraps `InvocationTargetException` if its getTargetException is an `Exception`.
@ -93,9 +89,7 @@ object DynamicAccess {
*/ */
class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAccess { class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAccess {
import DynamicAccess.withErrorHandling override def getClassFor[T: ClassManifest](fqcn: String): Either[Throwable, Class[_ <: T]] =
override def createClassFor[T: ClassManifest](fqcn: String): Either[Throwable, Class[_ <: T]] =
try { try {
val c = classLoader.loadClass(fqcn).asInstanceOf[Class[_ <: T]] val c = classLoader.loadClass(fqcn).asInstanceOf[Class[_ <: T]]
val t = classManifest[T].erasure val t = classManifest[T].erasure
@ -105,7 +99,7 @@ class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAcces
} }
override def createInstanceFor[T: ClassManifest](fqcn: String, args: Seq[(Class[_], AnyRef)]): Either[Throwable, T] = override def createInstanceFor[T: ClassManifest](fqcn: String, args: Seq[(Class[_], AnyRef)]): Either[Throwable, T] =
createClassFor(fqcn).fold(Left(_), { c getClassFor(fqcn).fold(Left(_), { c
val types = args.map(_._1).toArray val types = args.map(_._1).toArray
val values = args.map(_._2).toArray val values = args.map(_._2).toArray
withErrorHandling { withErrorHandling {
@ -118,7 +112,7 @@ class ReflectiveDynamicAccess(val classLoader: ClassLoader) extends DynamicAcces
}) })
override def getObjectFor[T: ClassManifest](fqcn: String): Either[Throwable, T] = { override def getObjectFor[T: ClassManifest](fqcn: String): Either[Throwable, T] = {
createClassFor(fqcn).fold(Left(_), { c getClassFor(fqcn).fold(Left(_), { c
withErrorHandling { withErrorHandling {
val module = c.getDeclaredField("MODULE$") val module = c.getDeclaredField("MODULE$")
module.setAccessible(true) module.setAccessible(true)

View file

@ -97,7 +97,7 @@ abstract class ExtensionKey[T <: Extension](implicit m: ClassManifest[T]) extend
override def lookup(): ExtensionId[T] = this override def lookup(): ExtensionId[T] = this
def createExtension(system: ExtendedActorSystem): T = def createExtension(system: ExtendedActorSystem): T =
DynamicAccess.createInstanceFor[T](m.erasure, Seq(classOf[ExtendedActorSystem] -> system)) match { system.dynamicAccess.createInstanceFor[T](m.erasure, Seq(classOf[ExtendedActorSystem] -> system)) match {
case Left(ex) throw ex case Left(ex) throw ex
case Right(r) r case Right(r) r
} }

View file

@ -100,7 +100,7 @@ trait LoggingBus extends ActorEventBus {
if loggerName != StandardOutLoggerName if loggerName != StandardOutLoggerName
} yield { } yield {
try { try {
system.dynamicAccess.createClassFor[Actor](loggerName) match { system.dynamicAccess.getClassFor[Actor](loggerName) match {
case Right(actorClass) addLogger(system, actorClass, level, logName) case Right(actorClass) addLogger(system, actorClass, level, logName)
case Left(exception) throw exception case Left(exception) throw exception
} }

View file

@ -132,11 +132,9 @@ class Serialization(val system: ExtendedActorSystem) extends Extension {
* Tries to load the specified Serializer by the fully-qualified name; the actual * Tries to load the specified Serializer by the fully-qualified name; the actual
* loading is performed by the systems [[akka.actor.DynamicAccess]]. * loading is performed by the systems [[akka.actor.DynamicAccess]].
*/ */
def serializerOf(serializerFQN: String): Either[Throwable, Serializer] = { def serializerOf(serializerFQN: String): Either[Throwable, Serializer] =
val dynamicAccess = system.dynamicAccess system.dynamicAccess.createInstanceFor[Serializer](serializerFQN, Seq(classOf[ExtendedActorSystem] -> system)).fold(_
dynamicAccess.createInstanceFor[Serializer](serializerFQN, Seq(classOf[ExtendedActorSystem] -> system)) system.dynamicAccess.createInstanceFor[Serializer](serializerFQN, Seq()), Right(_))
.fold(_ dynamicAccess.createInstanceFor[Serializer](serializerFQN, Seq()), Right(_))
}
/** /**
* A Map of serializer from alias to implementation (class implementing akka.serialization.Serializer) * A Map of serializer from alias to implementation (class implementing akka.serialization.Serializer)
@ -153,7 +151,7 @@ class Serialization(val system: ExtendedActorSystem) extends Extension {
*/ */
private[akka] val bindings: Seq[ClassSerializer] = { private[akka] val bindings: Seq[ClassSerializer] = {
val configuredBindings = for ((k: String, v: String) settings.SerializationBindings if v != "none") yield { val configuredBindings = for ((k: String, v: String) settings.SerializationBindings if v != "none") yield {
val c = system.dynamicAccess.createClassFor(k).fold(throw _, identity[Class[_]]) val c = system.dynamicAccess.getClassFor(k).fold(throw _, identity[Class[_]])
(c, serializers(v)) (c, serializers(v))
} }
sort(configuredBindings) sort(configuredBindings)

View file

@ -14,7 +14,7 @@ object MessageSerializer {
def deserialize(system: ExtendedActorSystem, messageProtocol: MessageProtocol): AnyRef = { def deserialize(system: ExtendedActorSystem, messageProtocol: MessageProtocol): AnyRef = {
val clazz = val clazz =
if (messageProtocol.hasMessageManifest) { if (messageProtocol.hasMessageManifest) {
system.dynamicAccess.createClassFor[AnyRef](messageProtocol.getMessageManifest.toStringUtf8) system.dynamicAccess.getClassFor[AnyRef](messageProtocol.getMessageManifest.toStringUtf8)
.fold(throw _, Some(_)) .fold(throw _, Some(_))
} else None } else None
SerializationExtension(system) SerializationExtension(system)

View file

@ -121,7 +121,7 @@ object TestActorRef {
def apply[T <: Actor](implicit m: Manifest[T], system: ActorSystem): TestActorRef[T] = apply[T](randomName) def apply[T <: Actor](implicit m: Manifest[T], system: ActorSystem): TestActorRef[T] = apply[T](randomName)
def apply[T <: Actor](name: String)(implicit m: Manifest[T], system: ActorSystem): TestActorRef[T] = apply[T](Props({ def apply[T <: Actor](name: String)(implicit m: Manifest[T], system: ActorSystem): TestActorRef[T] = apply[T](Props({
DynamicAccess.createInstanceFor[T](m.erasure, Seq()) match { system.asInstanceOf[ExtendedActorSystem].dynamicAccess.createInstanceFor[T](m.erasure, Seq()) match {
case Right(value) value case Right(value) value
case Left(exception) throw new ActorInitializationException(null, case Left(exception) throw new ActorInitializationException(null,
"Could not instantiate Actor" + "Could not instantiate Actor" +