All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ing.baker.recipe.javadsl.ReflectionHelpers.scala Maven / Gradle / Ivy

There is a newer version: 1.1.18
Show newest version
package com.ing.baker.recipe.javadsl

import java.lang.annotation.Annotation
import java.lang.reflect.{Method, Type}

import com.ing.baker.recipe.{annotations, common}
import com.thoughtworks.paranamer.AnnotationParanamer

object ReflectionHelpers {

  implicit class MethodReflectionAdditions(method: Method) {
    lazy val paramamer         = new RequiresAnnotationParanamer()
    lazy val getParameterNames = paramamer.lookupParameterNames(method)

    def parameterTypeForName(name: String): Option[Type] =
      getParameterNames.indexWhere(_ == name) match {
        case -1 => None
        case n  => Some(method.getGenericParameterTypes.apply(n))
      }
  }

  class RequiresAnnotationParanamer extends AnnotationParanamer {
    override def getNamedValue(annotation: Annotation): String = {

      val annotationType = annotation.annotationType()

      if (annotationType.equals(classOf[annotations.RequiresIngredient]))
        annotation.asInstanceOf[annotations.RequiresIngredient].value()
      else if (annotationType.equals(classOf[annotations.ProcessId]))
        common.ProcessIdName
      else annotationType.getSimpleName
    }

    override def isNamed(annotation: Annotation): Boolean = true
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy