本文整理汇总了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)
}
示例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
}
}
示例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
}
}
示例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
}
}
示例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))
}
}