当前位置: 首页>>代码示例>>Scala>>正文


Scala ClassFinder类代码示例

本文整理汇总了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
  }
} 
开发者ID:ruippeixotog,项目名称:scalafbp,代码行数:44,代码来源:ComponentLoader.scala

示例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[_]]
        })
    }
  }

} 
开发者ID:gpulse,项目名称:eventgenerator,代码行数:26,代码来源:ClassLoaderImplicits.scala

示例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
  }
} 
开发者ID:macgemini,项目名称:tytanis,代码行数:39,代码来源:HelpActor.scala

示例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)
  }

} 
开发者ID:blstream,项目名称:akka-viz,代码行数:31,代码来源:SerializerFinder.scala


注:本文中的org.clapper.classutil.ClassFinder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。