Git Product home page Git Product logo

scala-reflections's Introduction

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

scala-reflections's People

Contributors

anjunar avatar

Stargazers

Damian Reeves avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.