本文整理汇总了Scala中java.lang.reflect.ParameterizedType类的典型用法代码示例。如果您正苦于以下问题:Scala ParameterizedType类的具体用法?Scala ParameterizedType怎么用?Scala ParameterizedType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ParameterizedType类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: JSONHTTP
//设置package包名称以及导入依赖的类
package works.weave.socks.aws.orders.dataaccess.web
import com.amazonaws.util.IOUtils
import com.fasterxml.jackson.core.`type`.TypeReference
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.net.URI
import javax.xml.ws.http.HTTPException
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.slf4j.LoggerFactory
import works.weave.socks.aws.orders.ProjectDefaultJacksonMapper
object JSONHTTP {
val Log = LoggerFactory.getLogger(getClass)
val objectMapper = ProjectDefaultJacksonMapper.build() // after (_.bla)
def get[T : Manifest : NotNothing](uri : URI) : T = {
val client = HttpClientBuilder.create.build
val get = new HttpGet(uri)
get.addHeader("Accept", "application/json")
val response = client.execute(get)
val responseString = IOUtils.toString(response.getEntity.getContent)
Log.info(s"Got status ${response.getStatusLine.getStatusCode}")
if (response.getStatusLine.getStatusCode == 200) {
Log.info(s"Got response from URI $uri: $responseString")
objectMapper.readValue(responseString, typeReference[T])
} else {
throw new HTTPException(response.getStatusLine.getStatusCode)
}
}
def typeReference[T : Manifest] = new TypeReference[T] {
override def getType = typeFromManifest(manifest[T])
}
def typeFromManifest(m : Manifest[_]) : Type = {
if (m.typeArguments.isEmpty) { m.runtimeClass }
else new ParameterizedType {
def getRawType = m.runtimeClass
def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
def getOwnerType = null
}
}
// Trick to disable nothign for type param
sealed trait NotNothing[-T]
object NotNothing {
implicit object notNothing extends NotNothing[Any]
implicit object `The error is because the missing type parameter was resolved to Nothing` extends NotNothing[Nothing]
}
}
示例2: JacksonSupport
//设置package包名称以及导入依赖的类
package com.github.andyglow.relaxed
import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind._
case class JacksonSupport(json: JsonNode) extends Reader with ReaderSupport[JsonNode] {
def isNull: (JsonNode) => Boolean = _.isNull
def ctor: (JsonNode) => JacksonSupport = JacksonSupport.apply
def get(field: String): Option[JsonNode] = {
val node = json.findPath(field)
if (node.isMissingNode) None else Some(node)
}
override def opt[T: Reads](field: String): Option[T] = {
val r = implicitly[Reads[T]].asInstanceOf[JacksonSupport.JacksonReads[T]]
get(field) map {x => r.mapper.readerFor(r.typeReference).readValue[T](x)}
}
override def optOpt[T: Reads](field: String): Option[Option[T]] = {
val r = implicitly[Reads[T]].asInstanceOf[JacksonSupport.JacksonReads[T]]
getOpt(field) map {_ map {x => r.mapper.readerFor(r.typeReference).readValue[T](x)}}
}
}
object JacksonSupport {
import java.lang.reflect.{Type, ParameterizedType}
sealed trait JacksonReads[T] extends Reads[T] {
def mapper: ObjectMapper
def typeReference: TypeReference[T]
}
implicit def readsImpl[T](implicit x: ObjectMapper, m: Manifest[T]): Reads[T] = new JacksonReads[T]() {
override def mapper: ObjectMapper = x
override lazy val typeReference: TypeReference[T] = {
def typeFromManifest(m: Manifest[_]): Type = {
if (m.typeArguments.isEmpty) m.runtimeClass
else new ParameterizedType {
def getRawType: Class[_] = m.runtimeClass
def getActualTypeArguments: Array[Type] = m.typeArguments.map(typeFromManifest).toArray
def getOwnerType = null
}
}
new TypeReference[T] {
override def getType: Type = typeFromManifest(manifest[T])
}
}
}
}
示例3: VPackScalaDeserializers
//设置package包名称以及导入依赖的类
package com.arangodb.velocypack.module.scala.internal
import com.arangodb.velocypack.VPackDeserializer
import com.arangodb.velocypack.VPackDeserializationContext
import com.arangodb.velocypack.VPackSlice
import com.arangodb.velocypack.VPackDeserializerParameterizedType
import java.lang.reflect.ParameterizedType
import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
object VPackScalaDeserializers {
val OPTION = new VPackDeserializerParameterizedType[Option[Any]] {
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Option[Any] =
throw new UnsupportedOperationException
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext, t: ParameterizedType): Option[Any] = {
val value = context.deserialize(vpack, t.getActualTypeArguments()(0).asInstanceOf[Class[Any]])
value match {
case null => None
case _ => Some(value)
}
}
}
val LIST = new VPackDeserializer[List[Any]] {
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): List[Any] =
context.deserialize(vpack, classOf[java.util.List[Any]]).toList
}
val MAP = new VPackDeserializer[Map[Any, Any]] {
def deserialize(parent: VPackSlice, vpack: VPackSlice, context: VPackDeserializationContext): Map[Any, Any] =
context.deserialize(vpack, classOf[java.util.Map[Any, Any]]).toMap
}
}
示例4: serialize
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.internal.javadsl.api
import java.lang.reflect.{ ParameterizedType, Type }
import java.util.Optional
import com.lightbend.lagom.javadsl.api.deser.{ PathParamSerializer, PathParamSerializers }
import org.pcollections.{ PSequence, TreePVector }
import scala.compat.java8.FunctionConverters._
trait UnresolvedPathParamSerializer[Param] extends PathParamSerializer[Param] {
def serialize(parameter: Param) = throw new NotImplementedError("Cannot use unresolved path param serializer to serialize path params")
def deserialize(parameters: PSequence[String]) = throw new NotImplementedError("Cannot use unresolved path param serializer to deserialize path params")
def resolve(resolver: ServiceCallResolver, typeInfo: Type): PathParamSerializer[Param]
}
class UnresolvedTypePathParamSerializer[Param] extends UnresolvedPathParamSerializer[Param] {
override def resolve(resolver: ServiceCallResolver, typeInfo: Type): PathParamSerializer[Param] = {
resolver.pathParamSerializerFor(typeInfo, typeInfo)
}
}
class UnresolvedOptionalPathParamSerializer[Param] extends UnresolvedPathParamSerializer[Optional[Param]] {
override def resolve(resolver: ServiceCallResolver, typeInfo: Type): PathParamSerializer[Optional[Param]] = {
typeInfo match {
case paramType: ParameterizedType if paramType.getRawType == classOf[Optional[_]] =>
val wrappedType = paramType.getActualTypeArguments.apply(0)
val subTypeSerializer = resolver.pathParamSerializerFor[Param](wrappedType, wrappedType)
PathParamSerializers.optional[Param](
wrappedType.getTypeName,
(subTypeSerializer.deserialize _).compose((p: String) => TreePVector.singleton(p)).asJava,
(subTypeSerializer.serialize _).andThen {
case single if single.size() == 1 => single.get(0)
case other => throw new IllegalStateException("Can only wrap an Optional serializer around a path param serializer that produces exactly one parameter")
}.asJava
)
case _ => throw new IllegalArgumentException("Unresolved optional path param serializer can only be resolved against ParamaterizedType descriptors for the Optional class. This serializer was resolved against: " + typeInfo)
}
}
}
示例5: EnumResolver
//设置package包名称以及导入依赖的类
package com.kakao.shaded.jackson.module.scala
package util
import java.lang.reflect.ParameterizedType
import com.kakao.shaded.jackson.databind.BeanProperty
import com.kakao.shaded.jackson.module.scala.introspect.ScalaAnnotationIntrospector
import com.kakao.shaded.jackson.module.scala.JsonScalaEnumeration
object EnumResolver {
def apply(property: BeanProperty): Option[EnumResolver] = {
Option(property)
.flatMap(p => ScalaAnnotationIntrospector.propertyFor(p.getMember))
.flatMap(_.findAnnotation[JsonScalaEnumeration])
.map(a => apply(a))
}
def apply(a: JsonScalaEnumeration): EnumResolver = {
val pt = a.value().getGenericSuperclass.asInstanceOf[ParameterizedType]
val args = pt.getActualTypeArguments
apply(args(0).asInstanceOf[Class[Enumeration]])
}
def apply[T <: Enumeration](cls: Class[T]): EnumResolver = {
val enum = cls.getField("MODULE$").get(null).asInstanceOf[T]
apply(enum)
}
def apply(e: Enumeration): EnumResolver = {
val valueSet = e.values
val map: Map[String, e.type#Value] = valueSet.map(v => (v.toString, v)).toMap
new EnumResolver(e.getClass, valueSet, map)
}
}
class EnumResolver(cls: Class[_], valueSet: Enumeration#ValueSet, enumsByName: Map[String, Enumeration#Value]) {
def getEnum(key: String): Enumeration#Value = enumsByName(key)
def getEnumClass = cls
}
示例6: SeqTypeAdapterFactory
//设置package包名称以及导入依赖的类
package cz.augi.gsonscala
import java.lang.reflect.ParameterizedType
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.{JsonReader, JsonToken, JsonWriter}
import com.google.gson.{Gson, TypeAdapter, TypeAdapterFactory}
object SeqTypeAdapterFactory extends TypeAdapterFactory {
override def create[T](gson: Gson, t: TypeToken[T]): TypeAdapter[T] =
if (classOf[Seq[_]].isAssignableFrom(t.getRawType)) new SeqTypeAdapter(gson, t) else null
}
class SeqTypeAdapter[T](gson: Gson, t: TypeToken[T]) extends TypeAdapter[T] {
val innerType = t.getType.asInstanceOf[ParameterizedType].getActualTypeArguments()(0)
override def write(out: JsonWriter, value: T): Unit = {
out.beginArray()
value.asInstanceOf[Seq[_]].foreach(i => gson.toJson(i, innerType, out))
out.endArray()
}
override def read(in: JsonReader): T = {
val builder = scala.collection.mutable.Seq.newBuilder
in.beginArray()
while(in.peek() != JsonToken.END_ARRAY) {
builder += gson.fromJson(in, innerType)
}
in.endArray()
val res = builder.result()
(if (res.isEmpty) Seq.empty else res).asInstanceOf[T]
}
}
示例7: OptionalTypeAdapterFactory
//设置package包名称以及导入依赖的类
package cz.augi.gsonscala
import java.lang.reflect.ParameterizedType
import java.util.Optional
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.{JsonReader, JsonWriter}
import com.google.gson.{Gson, TypeAdapter, TypeAdapterFactory}
object OptionalTypeAdapterFactory extends TypeAdapterFactory {
override def create[T](gson: Gson, t: TypeToken[T]): TypeAdapter[T] =
if (classOf[Optional[_]].isAssignableFrom(t.getRawType)) new OptionalTypeAdapter(gson, t) else null
}
class OptionalTypeAdapter[T](gson: Gson, t: TypeToken[T]) extends TypeAdapter[T] {
val innerType = t.getType.asInstanceOf[ParameterizedType].getActualTypeArguments()(0)
override def write(out: JsonWriter, value: T): Unit =
value match {
case o: Optional[_] =>
if (o.isPresent) {
gson.toJson(o.get, innerType, out)
} else {
// we must forcibly write null in order the read method to be called
val orig = out.getSerializeNulls
out.setSerializeNulls(true)
out.nullValue()
out.setSerializeNulls(orig)
}
}
override def read(in: JsonReader): T = Optional.ofNullable(gson.fromJson(in, innerType)).asInstanceOf[T]
}
示例8: OptionTypeAdapterFactory
//设置package包名称以及导入依赖的类
package cz.augi.gsonscala
import java.lang.reflect.ParameterizedType
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.{JsonReader, JsonWriter}
import com.google.gson.{Gson, TypeAdapter, TypeAdapterFactory}
object OptionTypeAdapterFactory extends TypeAdapterFactory {
override def create[T](gson: Gson, t: TypeToken[T]): TypeAdapter[T] =
if (classOf[Option[_]].isAssignableFrom(t.getRawType)) new OptionTypeAdapter(gson, t) else null
}
class OptionTypeAdapter[T](gson: Gson, t: TypeToken[T]) extends TypeAdapter[T] {
val innerType = t.getType.asInstanceOf[ParameterizedType].getActualTypeArguments()(0)
override def write(out: JsonWriter, value: T): Unit =
value match {
case o: Option[_] => o match {
case Some(v) => gson.toJson(v, innerType, out)
case None =>
// we must forcibly write null in order the read method to be called
val orig = out.getSerializeNulls
out.setSerializeNulls(true)
out.nullValue()
out.setSerializeNulls(orig)
}
}
override def read(in: JsonReader): T = Option(gson.fromJson(in, innerType)).asInstanceOf[T]
}