in scrooge-generator/src/main/scala/com/twitter/scrooge/backend/AnnotationValidator.scala [82:122]
private def validateDefaultAnnotation(
annotationKey: String,
annotationValue: String,
fieldClazz: Set[Class[_]]
): Set[String] =
DefaultAnnotations.metadata.get(annotationKey) match {
case Some(AnnotationMetaData(annotationClazz, allowedClazz, _)) =>
val result = mutable.Set.empty[String]
if (annotationClazz.nonEmpty) {
val clazz = annotationClazz.get
// validate if the given annotation value matches the annotation type
try {
// the annotation class is defined with the Java class as a private
// field in `DefaultAnnotations.AnnotationMetaData`, so we only need
// to compare with Java class.
// we only need Java type here because the underlying Hibernator
// validator defines the annotation value in Java.
// we only need to check for `Integer` and `Long` since they are the
// only allowed annotation classes for default annotations.
if (clazz == classOf[java.lang.Integer]) annotationValue.toInt
if (clazz == classOf[java.lang.Long]) annotationValue.toLong
} catch {
case _: java.lang.NumberFormatException =>
val errorMessage =
s"The annotation $annotationKey requires a value of type ${clazz.getSimpleName}, " +
s"the annotation value ${truncate(annotationValue)} " +
s"is not of type ${clazz.getSimpleName}"
result += errorMessage
}
}
// validate the given annotation can be applied to the given field type
if (fieldClazz.nonEmpty && allowedClazz.intersect(fieldClazz).isEmpty) {
val errorMessage =
s"The annotation $annotationKey can not be applied to the field with type ${fieldClazz
.map(_.getSimpleName)
.mkString(", ")}, the allowed Classes are: ${allowedClazz.map(_.getSimpleName).mkString(", ")}"
result += errorMessage
}
result.toSet
case _ => Set.empty[String]
}