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


Scala Constructor类代码示例

本文整理汇总了Scala中java.lang.reflect.Constructor的典型用法代码示例。如果您正苦于以下问题:Scala Constructor类的具体用法?Scala Constructor怎么用?Scala Constructor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Constructor类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: newInstance

//设置package包名称以及导入依赖的类
package com.programmaticallyspeaking.ncd.ioc

import java.lang.reflect.Constructor


  def newInstance[R](clazz: Class[_], creator: (Class[_], Seq[Any]) => R): R = {
    val ca = constructionFor(clazz)
    creator(clazz, ca._2)
  }

  private def constructionFor(clazz: Class[_]): (Constructor[_], Seq[AnyRef]) = {
    clazz.getConstructors.toList match {
      case c :: Nil =>
        val args = c.getParameterTypes.map(findReference)
        (c, args)

      case _ :: _ => throw new IllegalArgumentException(s"Class $clazz has multiple public constructors")
      case Nil => throw new IllegalArgumentException(s"Class $clazz has no public constructor")
    }
  }
}

object Container {
  val empty: Container = new Container(Seq.empty)
} 
开发者ID:provegard,项目名称:ncdbg,代码行数:26,代码来源:Container.scala

示例2: SharkExecuteStatementOperation

//设置package包名称以及导入依赖的类
package shark.server

import java.lang.reflect.Constructor
import java.util.{Map => JMap}
import org.apache.hive.service.cli.session.HiveSession

object SharkExecuteStatementOperation {
  def newExecuteStatementOperation(parentSession: HiveSession,
                                   statement: String,
                                   confOverlay: JMap[String, String])
                                   : Any = {
    val tokens = statement.trim().split("\\s+")
    val command = tokens{0}.toLowerCase
    command match {
      case "set" => {
        val ctor = accessCtor("org.apache.hive.service.cli.operation.SetOperation")
        ctor.newInstance(parentSession, statement, confOverlay)
      }
      case "dfs" => {
        val ctor = accessCtor("org.apache.hive.service.cli.operation.DfsOperation")
        ctor.newInstance(parentSession, statement, confOverlay)
      }
      case "add" => {
        val ctor = accessCtor("org.apache.hive.service.cli.operation.AddResourceOperation")
        ctor.newInstance(parentSession, statement, confOverlay)
      }
      case "delete" => {
        val ctor = accessCtor("org.apache.hive.service.cli.operation.DeleteResourceOperation")
        ctor.newInstance(parentSession, statement, confOverlay)
      }
      case _ => {
        new SharkSQLOperation(parentSession, statement, confOverlay)
      }
    }
  }

  def accessCtor(className : String) : Constructor[_] =  {
    val setClass =  Class.forName(className)
    val setConst =
      setClass.getDeclaredConstructor(
        classOf[HiveSession],
        classOf[String],
        classOf[JMap[String, String]])
    setConst.setAccessible(true)
    setConst
  }
} 
开发者ID:palmer0914,项目名称:shark,代码行数:48,代码来源:SharkExecuteStatementOperation.scala

示例3: ConstructorParameter

//设置package包名称以及导入依赖的类
package com.kakao.shaded.jackson.module.scala
package introspect

import util.Implicits._
import java.lang.reflect.{AccessibleObject, Constructor, Field, Method}

import scala.language.existentials

case class ConstructorParameter(constructor: Constructor[_], index: Int, defaultValueMethod: Option[Method])

case class PropertyDescriptor(name: String,
                              param: Option[ConstructorParameter],
                              field: Option[Field],
                              getter: Option[Method],
                              setter: Option[Method],
                              beanGetter: Option[Method],
                              beanSetter: Option[Method])
{
  if (List(field, getter).flatten.isEmpty) throw new IllegalArgumentException("One of field or getter must be defined.")

  def findAnnotation[A <: java.lang.annotation.Annotation](implicit mf: Manifest[A]): Option[A] = {
    val cls = mf.runtimeClass.asInstanceOf[Class[A]]
    lazy val paramAnnotation = (param flatMap { cp =>
      val paramAnnos = cp.constructor.getParameterAnnotations
      paramAnnos(cp.index).find(cls.isInstance)
    }).asInstanceOf[Option[A]]
    val getAnno = (o: AccessibleObject) => o.getAnnotation(cls)
    lazy val fieldAnnotation = field optMap getAnno
    lazy val getterAnnotation = getter optMap getAnno
    lazy val beanGetterAnnotation = beanGetter optMap getAnno

    paramAnnotation orElse fieldAnnotation orElse getterAnnotation orElse beanGetterAnnotation
  }

} 
开发者ID:kakao,项目名称:mango,代码行数:36,代码来源:PropertyDescriptor.scala

示例4: Accessible

//设置package包名称以及导入依赖的类
package com.kakao.mango.reflect

import java.lang.reflect.{Constructor, Method, Field}
import java.lang.reflect.Modifier._

import scala.reflect._


object Accessible {

  val modifiers: Field = {
    val field = classOf[Field].getDeclaredField("modifiers")
    field.setAccessible(true)
    field
  }

  def field[T: ClassTag](name: String): Field = {
    field(classTag[T].runtimeClass, name)
  }

  def field(clazz: Class[_], name: String): Field = {
    val field = clazz.getDeclaredField(name)
    field.setAccessible(true)
    modifiers.setInt(field, field.getModifiers & ~FINAL)
    field
  }

  def method[T: ClassTag](name: String, parameterTypes: Class[_]*): Method = {
    method(classTag[T].runtimeClass, name, parameterTypes: _*)
  }

  def method(clazz: Class[_], name: String, parameterTypes: Class[_]*): Method = {
    val method = clazz.getDeclaredMethod(name, parameterTypes: _*)
    method.setAccessible(true)
    method
  }

  def constructor[T](clazz: Class[T], parameterTypes: Class[_]*): Constructor[T] = {
    val constructor = clazz.getDeclaredConstructor(parameterTypes: _*)
    constructor.setAccessible(true)
    constructor
  }

  def firstConstructor[T: ClassTag]: Constructor[T] = {
    firstConstructor(classTag[T].runtimeClass.asInstanceOf[Class[T]])
  }

  def firstConstructor[T](clazz: Class[T]): Constructor[T] = {
    val constructor = clazz.getDeclaredConstructors()(0).asInstanceOf[Constructor[T]]
    constructor.setAccessible(true)
    constructor
  }

} 
开发者ID:kakao,项目名称:mango,代码行数:55,代码来源:Accessible.scala

示例5: Global

//设置package包名称以及导入依赖的类
package io.spass

import java.lang.reflect.Constructor

import io.spass.users.controllers.{AngularMailTemplates, JsonViewTemplates}
import io.spass.users.models.BasicUser
import io.spass.users.service.slick.{SlickAuthenticatorStore, SlickUserService}
import securesocial.controllers.{MailTemplates, ViewTemplates}
import securesocial.core.RuntimeEnvironment
import securesocial.core.authenticator.{CookieAuthenticatorBuilder, HttpHeaderAuthenticatorBuilder}
import securesocial.core.services.{AuthenticatorService, UserService}


object Global extends play.api.GlobalSettings {
  object MyRuntimeEnvironment extends RuntimeEnvironment.Default[BasicUser] {
    override val userService: UserService[BasicUser] = new SlickUserService
    override lazy val authenticatorService: AuthenticatorService[BasicUser] = new AuthenticatorService[BasicUser](
      new CookieAuthenticatorBuilder[BasicUser](new SlickAuthenticatorStore, idGenerator),
      new HttpHeaderAuthenticatorBuilder[BasicUser](new SlickAuthenticatorStore, idGenerator)
    )
    override lazy val viewTemplates: ViewTemplates = JsonViewTemplates
    override lazy val mailTemplates: MailTemplates = AngularMailTemplates
  }

  override def getControllerInstance[A](controllerClass: Class[A]): A = {
    val instance  = controllerClass.getConstructors.find { c =>
      val params = c.getParameterTypes
      params.length == 1 && params(0) == classOf[RuntimeEnvironment[BasicUser]]
    }.map {
      _.asInstanceOf[Constructor[A]].newInstance(MyRuntimeEnvironment)
    }
    instance.getOrElse(super.getControllerInstance(controllerClass))
  }
} 
开发者ID:camilosampedro,项目名称:atonwebapp,代码行数:35,代码来源:Global.scala


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