本文整理汇总了Scala中java.lang.annotation.Annotation类的典型用法代码示例。如果您正苦于以下问题:Scala Annotation类的具体用法?Scala Annotation怎么用?Scala Annotation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Annotation类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ClassOps
//设置package包名称以及导入依赖的类
package com.github.stonexx.scala.util
import java.lang.annotation.Annotation
import java.net.URI
import java.util.{Date, Locale}
import org.apache.commons.lang3.ClassUtils
import scala.reflect.ClassTag
final class ClassOps[T](val self: Class[T]) extends AnyVal {
import cls._
def isSimpleType: Boolean = ClassUtils.isPrimitiveOrWrapper(self) ||
classOf[CharSequence].isAssignableFrom(self) ||
classOf[Number].isAssignableFrom(self) ||
classOf[Date].isAssignableFrom(self) ||
classOf[URI] == self ||
classOf[Locale] == self ||
classOf[Class[_]] == self ||
self.isEnum
def findAnnotation[A <: Annotation : ClassTag](annotationClass: Class[A]): Option[A] =
Option(self getAnnotation annotationClass).orElse {
self.getInterfaces.find(_ isAnnotationPresent annotationClass).flatMap(_.findAnnotation[A])
}.orElse {
if (classOf[Annotation] isAssignableFrom self) None
else self.getAnnotations.map(_.annotationType).find(_ isAnnotationPresent annotationClass).flatMap(_.findAnnotation[A])
}.orElse {
Option(self.getSuperclass).filter(_ != classOf[Object]).flatMap(_.findAnnotation[A])
}
@inline
def findAnnotation[A <: Annotation](implicit atag: ClassTag[A]): Option[A] =
findAnnotation(atag.runtimeClass.asInstanceOf[Class[A]])
}
trait ToClassOps {
implicit def toClassOps[T](x: Class[T]): ClassOps[T] = new ClassOps(x)
}
示例2: get
//设置package包名称以及导入依赖的类
package de.khamrakulov.play.metrics.annotation.matcher
import java.lang.annotation.Annotation
import java.lang.reflect.Method
trait AnnotationMatcher {
def get[T <: Annotation: Manifest](method: Method): Option[T]
}
object ClassAnnotationMatcher {
def apply() = new ClassAnnotationMatcher()
}
class ClassAnnotationMatcher() extends AnnotationMatcher {
override def get[T <: Annotation](method: Method)(implicit ev: Manifest[T]): Option[T] = Option {
method.getDeclaringClass.getAnnotation(ev.runtimeClass.asInstanceOf[Class[T]])
}
}
object MethodAnnotationMatcher {
def apply() = new MethodAnnotationMatcher()
}
class MethodAnnotationMatcher() extends AnnotationMatcher {
override def get[T <: Annotation](method: Method)(implicit ev: Manifest[T]): Option[T] = Option {
method.getAnnotation(ev.runtimeClass.asInstanceOf[Class[T]])
}
}
示例3: JsonSubTypesUtil
//设置package包名称以及导入依赖的类
package com.sk.app.proxmock.toolset.serialization.betterpolymorphism
import java.lang.annotation.Annotation
import com.fasterxml.jackson.annotation.JsonSubTypes
private[betterpolymorphism] object JsonSubTypesUtil {
def asMap(clazz: Class[_]): Map[String, Class[_]] = {
findAnnotation(clazz, classOf[JsonSubTypes]) match {
case Some(subTypes) => subTypes
.value()
.map(typeInfo => typeInfo.name() -> typeInfo.value())
.toMap
case None => Map()
}
}
def asMapInverted(clazz: Class[_]): Map[Class[_], String] = {
findAnnotation(clazz, classOf[JsonSubTypes]) match {
case Some(subTypes) => subTypes
.value()
.map(typeInfo => typeInfo.value() -> typeInfo.name())
.toMap
case None => Map()
}
}
private def findAnnotation[T <: Annotation](clazz: Class[_], annoationClass: Class[T]): Option[T] = {
var found = clazz
while (found.getSuperclass != null) {
val annotation = found.getAnnotation(annoationClass)
if (annotation != null) return Option(annotation)
found = found.getSuperclass
}
None
}
}