Skip to content

anjunar/scala-reflections

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scala Reflections

"scala-reflections" is a robust and versatile reflection framework built exclusively for Scala 3, designed to empower developers with advanced metaprogramming capabilities while maintaining the principles of type-safety, expressiveness, and ease of use. This paper introduces "scala-reflections" and explores its rich set of features, emphasizing its potential to revolutionize Scala 3 development by providing seamless access to program structures, types, and values at runtime.

Key features of "scala-reflections" include:

Type-Driven Metaprogramming: The framework leverages Scala 3's enhanced type system, enabling developers to perform sophisticated type-level computations and type-safe pattern matching

Runtime Reflection: A reliable runtime reflection mechanism is provided, enabling dynamic inspection and manipulation of program structures and data. Developers can utilize runtime reflection to implement dynamic behaviors and adapt to changing requirements.

Meta-Annotations: "scala-reflections" introduces a novel approach to metaprogramming with meta-annotations. Developers can define custom annotations that execute at compile-time, enabling the creation of domain-specific language extensions and advanced compile-time checks.

Extensibility and Customization: The framework is designed for extensibility, allowing developers to define custom reflection strategies and adapt the framework to meet the unique needs of their projects.

This paper presents an in-depth analysis of "scala-reflections," showcasing its architecture, design principles, and usage examples. We illustrate how this framework can improve code quality, reduce boilerplate code, and enhance developer productivity. Real-world use cases are explored to demonstrate the practical benefits of integrating "scala-reflections" into existing projects.

In conclusion, "scala-reflections" stands as a significant advancement in metaprogramming for Scala 3. It offers a comprehensive set of features that empower developers to write more expressive, safe, and efficient code. By harnessing the power of Scala 3's type system and reflection capabilities, "scala-reflections" opens new horizons for metaprogramming in the Scala ecosystem, promising to accelerate innovation and code quality across a wide range of applications.

object Environment {

  val packages: List[String] = List("com.anjunar", "scala", "samples")

  val classPath: ClassPath = ClassPath.from(ClassLoader.getSystemClassLoader)
  
  val allClasses: java.util.Set[Class[_]] = classPath
    .getAllClasses
    .stream()
    .filter(clazz => packages.exists(p => clazz.getPackageName.startsWith(p)))
    .map(_.load())
    .collect(Collectors.toSet)

  val resolver: TypeResolver = Reflections.init(allClasses)
  
  val introSpector: Introspector = new Introspector(resolver)


}
class Person extends Identity {

  @Size(min = 3, max = 80)
  @BeanProperty
  var firstName: String = _

  @Size(min = 3, max = 80)
  @BeanProperty
  var lastName: String = _

}
val personClass = findStaticClass(classOf[Person])

val firstNameField = personClass
  .declaredField("firstName")
  .get

val sizeAnnotation = firstNameField
  .declaredAnnotation(classOf[Size])
  .get

val min = sizeAnnotation
  .fields("min")
  .asInstanceOf[Int]
assert(min == 3)

val max = sizeAnnotation
  .fields("max")
  .asInstanceOf[Int]
assert(max == 80)
val personBean = introSpector.resolve(classOf[Person])

val person = new Person()
person.id = "1"
person.firstName = "Max"
person.lastName = "Mustermann"

val property = personBean
  .properties
  .find(_.name == "firstName")
  .get

val value = property.get(person)

println(value) // => Max

...for more examples look a the tests...

--add-exports java.base/jdk.internal.vm.annotation=ALL-UNNAMED
--add-exports java.base/jdk.internal.util.random=ALL-UNNAMED

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published