本文整理汇总了Scala中org.clapper.classutil.ClassFinder类的典型用法代码示例。如果您正苦于以下问题:Scala ClassFinder类的具体用法?Scala ClassFinder怎么用?Scala ClassFinder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ClassFinder类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ComponentLoader
//设置package包名称以及导入依赖的类
package net.ruippeixotog.scalafbp.runtime
import scala.reflect.runtime.universe._
import scala.util.Try
import akka.event.slf4j.SLF4JLogging
import org.clapper.classutil.ClassFinder
import net.ruippeixotog.scalafbp.component.Component
object ComponentLoader extends SLF4JLogging {
def allInClasspath: Iterable[Component] = {
val m = runtimeMirror(getClass.getClassLoader)
val finder = ClassFinder()
def newInstance(className: String): Option[Component] = Try {
if (className.endsWith("$")) {
m.reflectModule(m.staticModule(className.init)).instance.asInstanceOf[Component]
} else {
Class.forName(className).newInstance().asInstanceOf[Component]
}
}.toOption
def findConcreteSubclasses(supers: Set[String], found: Set[String] = Set.empty): Set[String] = {
if (supers.isEmpty) found
else {
val candidates = finder.getClasses.filter { info =>
supers.contains(info.superClassName) || info.interfaces.exists(supers.contains)
}
val newFound = candidates.filter { info => info.isConcrete && info.isPublic }.map(_.name)
val newSupers = candidates.filterNot(_.isFinal).map(_.name)
findConcreteSubclasses(newSupers.toSet, found ++ newFound)
}
}
log.info("Finding component implementations...")
val components = findConcreteSubclasses(Set(classOf[Component].getName)).flatMap(newInstance)
log.info(s"Found ${components.size} component types")
components
}
}
示例2: ClassLoaderImplicits
//设置package包名称以及导入依赖的类
package eventgen.launcher
import java.io.File
import eventgen.launcher.core.ExternalGenerator
import org.clapper.classutil.ClassFinder
object ClassLoaderImplicits {
implicit val generatorLoader = new ExternalClassLoader[ExternalGenerator[_]] {
override def getAllInheritors(classPath: String): Seq[ExternalGenerator[_]] = {
val currentLoader = Thread.currentThread().getContextClassLoader
ClassFinder(new File(classPath) :: Nil).getClasses()
.filter(c => c.interfaces.exists(i => i == "eventgen.launcher.core.ExternalGenerator"))
.map(classInfo => {
val classToLoad = Class.forName(classInfo.name, false, currentLoader)
val instance = classToLoad.newInstance
instance.asInstanceOf[ExternalGenerator[_]]
})
}
}
}
示例3: HelpActor
//设置package包名称以及导入依赖的类
package actors
import actors.commands.GetHelp
import akka.actor.Actor.Receive
import akka.actor.{Actor, ActorLogging}
import org.clapper.classutil.ClassFinder
import java.io.{File => Fl}
class HelpActor extends Actor with ActorLogging {
override def receive: Receive = {
case x: GetHelp => sender() ! OutputMessage(commands.mkString("\n"))
}
val commands: List[String] = {
val classloader = Thread.currentThread.getContextClassLoader // some classloader
val classpath = classloader.getResource(".").toURI
val finder = ClassFinder(Seq(new Fl(classpath)))
val classes = finder.getClasses.toIterator
val commands = ClassFinder.concreteSubclasses("actors.commands.BaseCommand", classes)
commands.map(cinfo => {
{
val name = toCamel(cinfo.name)
if (cinfo.fields.size > 0) {
val members = cinfo.fields.map(x => x.name).reduce((a, b) => a + "" + b)
s"CommandName: ${name}, parameters: ${members}"
}
else s"CommandName: ${name}"
}
}).toList
}
def toCamel(s: String): String = {
val split = s.split("_")
val tail = split.tail.map { x => x.head.toUpper + x.tail }
split.head + tail.mkString
}
}
示例4: findSerializers
//设置package包名称以及导入依赖的类
package akkaviz.serialization
import org.clapper.classutil.ClassFinder
import scala.collection.breakOut
trait SerializerFinder {
private[this] val rm = scala.reflect.runtime.currentMirror
def findSerializers: List[AkkaVizSerializer] = {
val finder = ClassFinder()
val classes = finder.getClasses.filter(_.isConcrete).filter(_.implements(classOf[AkkaVizSerializer].getName))
classes.flatMap {
cls =>
val clazz = Class.forName(cls.name)
val classSymbol = rm.classSymbol(clazz)
if (classSymbol.isModule) {
Some(rm.reflectModule(classSymbol.asModule).instance.asInstanceOf[AkkaVizSerializer])
} else {
val constructors = classSymbol.toType.members.filter(_.isConstructor).map(_.asMethod)
val constructorMaybe = constructors.filter(_.isPublic).filter(_.paramLists.exists(_.size == 0)).headOption
constructorMaybe.map {
constructor =>
rm.reflectClass(classSymbol).reflectConstructor(constructor).apply().asInstanceOf[AkkaVizSerializer]
}
}
}(breakOut)
}
}