Created
May 10, 2019 04:53
-
-
Save hitme/69355b56d27ed73ea87e5b3e512116e5 to your computer and use it in GitHub Desktop.
scala-annotations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from https://medium.com/@giposse/scala-reflection-f5bde5fce630: | |
import javaAnnotations.{ SomethingIzable, VaporElement} | |
import scala.reflect.runtime.{universe => ru} | |
import ru._ | |
@SomethingIzable(value = “Teletransport”, level = 35) | |
case class Annotated ( | |
field1 : Integer, | |
@VaporElement(1.002) | |
field2 : String | |
) | |
object GetAnnot extends App { | |
def getAnnotationProperties(annotation: Annotation) = { | |
val returnValue = annotation.tree.children | |
.filter(a => a.isInstanceOf[AssignOrNamedArg]) | |
.map(_.asInstanceOf[AssignOrNamedArg]) | |
.map(ap => (ap.lhs, ap.rhs)) | |
returnValue | |
} | |
def annotationTypeName(annot: Annotation) = annot.tree.tpe.typeSymbol.name.toString() | |
val objSymbol = typeOf[Annotated].typeSymbol; | |
val classAnnot = getAnnotationProperties(objSymbol.annotations.head); | |
val annotatedProps = objSymbol.info.decls.filter(_.annotations.nonEmpty) | |
val mainCtor = objSymbol.info.decls.find(d => d.isMethod && d.asMethod.isPrimaryConstructor).get | |
val params = mainCtor.typeSignature.paramLists.head | |
val annotatedParams = params.filter(p => p.annotations.nonEmpty) | |
val annotData = annotatedParams.map(ap => (ap.name.toString(), ap.annotations | |
.map(sa => (annotationTypeName(sa), getAnnotationProperties(sa))))) | |
println(“Done!”) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment