score:0
You can write in a macro
import scala.annotation.experimental
import scala.quoted.*
inline def printAnnotations[A]: Unit = ${printAnnotationsImpl[A]}
@experimental // because .typeRef is @experimental
def printAnnotationsImpl[A](using Quotes, Type[A]): Expr[Unit] = {
import quotes.reflect.*
val symbol = TypeTree.of[A].symbol
println(symbol.caseFields.map(_.annotations)) // Case Class
val companion = symbol.companionModule
val methods = companion.declaredMethods
// Annotated method argument
println(methods.map(_.paramSymss.map(_.map(_.annotations))))
// Annotated Type
println(methods.collect(method => companion.typeRef.memberType(method) match {
case lt: LambdaType => lt.paramTypes.collect {
case at: AnnotatedType => at.annotation
}
}))
'{()}
}
Then for
case class A(x: Int, @Annot("z1") y: String)
object A:
def f1(x: Int, y: String @Annot("z2")): A = ???
def f2(x: Int, @Annot("z3") y: String): A = ???
printAnnotations[A]
will print
List(List(), List(Apply(Select(New(Ident(Annot)),<init>),List(Literal(Constant(z1))))))
List(List(List(List(), List())), List(List(List())), List(), List(List(List(), List())), List(List(List(), List(Apply(Select(New(Ident(Annot)),<init>),List(Literal(Constant(z3))))))), List(List()), List(List(List())))
List(List(), List(), List(Apply(Select(New(Ident(Annot)),<init>),List(Literal(Constant(z2))))), List(), List(), List())
Scala 3.1.3
Source: stackoverflow.com
Related Query
- How to get Scala annotations that are given to an argument of a method
- How to access a Java static method from Scala given a type alias for that class it resides in
- How do you get java method annotations to work in scala
- Scala macros: How can I get a list of the objects within a given package that inherit some trait?
- How can I make a scala method parameter type that is a collection of multiple types that can be converted to a given type?
- How does the Scala program compile, given that bindAndHandle method takes the first parameter of type Flow not Route?
- How do I get all the keys that are stored in the Cassandra 2.0.1 column family with scala using hector?
- How to get the annotations of a method in Scala 2.11
- How to get the properties values those that are defined in the shell (bash) script file using Scala 2.11 code?
- How to define a method in Scala that returns a Java object?
- How to compare two dataframe and print columns that are different in scala
- How to create annotations and get them in scala
- Scala 2.12.2 emits a ton of useless "Warning: parameter value ... in method ... is never used" warnings. How to get rid of them?
- How to define a scala method with type param that cannot be Any
- How to declare scala method so that it can be called from Java using varargs style
- How do I get a scala macro to replace a method call
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- How to get an java.lang.Class of a scala object for an annotation argument
- In Scala how do I apply a method to a variable before sorting a List on that variable?
- How are scala traits that extend classes compiled for a JVM target?
- Scala - calling a method with generic type parameter given a string value that determines the correct type
- How to get all columns names in spark in a nested structure that are of type string
- In Scala how do I define upper type bounds that are exclusive of the defined class?
- How to get return type string of a method using Scala reflection?
- How do I group records that are within a specific time interval using Spark Scala or sql?
- How to extract arguments from url that are separated with # in Scala Play
- How to get the annotations of a method
- how to get all instances of a given class/trait with scala reflect? all refs to a given instance?
- How to model operations in Scala that are only applicable to the same concrete implementations of a trait?
More Query from same tag
- java vs scala - reading in file on a separate thread
- Autowired not working in Scala Spring Boot project
- cats-effect: Unable to see decrease in execution time when using `parSequence`
- Scala function takes a map, modify the values and returns a map
- How to convert an anonymous function to a method value?
- Get reference to trait defined inside object
- Why does Scala not infer the type parameters when pattern matching with @
- How to include more than one item in my json object in scala?
- Running another notebook inside a notebook which is connected to git
- Scala (Breeze) + Intellij: "Cannot resolve symbol *"
- Scala Lagom development mode & Docker
- How should I handle Filter and Futures in play2 and Scala
- Swagger (spray) path parameters inside resource url
- final class vs non final compiler optimizations
- textbox validation in lift
- Unit testing Scala
- How to cast from Buffer[Map[String, String]] to Map[String, String]
- scala.reflect.internal.MissingRequirementError: object java.lang.Object in compiler mirror not found
- Scala/Spark - Create Dataset with one column from another Dataset
- representing arithmetic expressions as trees
- Error while importing SBT project
- Create a Seq of Elements based on Increment in Scala
- Scala Co-Variant Bounded Types
- Is there a library similar to PyCogent, but in Java (or Scala)?
- Int division in scala
- How to break each rows into multiple rows in Spark DataFrame using scala
- scala breeze, remove column or row from matrix
- Could not find proxy for ... in Macro
- Java library dependencies not found in my Play2 scala application
- Cannot figure out what =:=[A,B] stands for