Skip to content
This repository has been archived by the owner on Dec 8, 2017. It is now read-only.

wvlet/object-schema

Repository files navigation

object-schema Gitter Chat CircleCI Coverage Status

A library for inspecting Object schema (e.g., parameter names and its types, constructor, etc.)

Usage

Maven Central

libraryDependencies += "org.wvlet" %% "object-schema" % "(version)"
import wvlet.obj.ObjectSchema

case class Person(id:Int, name:String)
val scheam = ObjectSchema.of[Person]

The retrieved ObjectSchema has detailed information of your class, including:

  • Constructor and its argument types and names.
  • case class parameters
  • Methods

Extracting parameter names is impossible in Java programs, since JVM drops parameters names used in the source code at compile-time. While Scaal retains parameter names within the byte code of the class as ScalaSig. wvlet-obj reads ScalaSig and extract method parameter names and detailed types (e.g., type parameters used in Generics: e.g., String in List[String]).

Applications

Dependency injection

wvlet-obj is highly utilized in Airframe, wvlet-jmx to build programs by inspecting the shape of objects.

For example, in the following code wvlet-inject finds the constructor of MyApp class, then list dependencies (Module1, Module2, ...) that are necessary to build an MyApp instance:

class MyApp(m1:Module1, m2:Module2)

trait AppMain {
  val p = inject[MyApp]
}

Runtime code generation

In wvlet-jmx, @JMX annotation is used to register JMX parameter to JMX registry server so that we can monitor the behavior of the application outside JVM. You only need to aad @JMX annotation to start collecting JMX metrics:

case class Metrics(@JMX loadAvg:Double, @JMX memoryUsage:Double)

class MonitoringApp {
  @JMX def reportMetrics : Metrics = ...
}

wvlet-jmx checks the object schema of Metrics and registers its parameters to JMX registry.

Object mapping

wvlet-config reads Yaml files and binds the data into objects with wvlet-obj.

config.yml

default:
   timeout-millis: 100
   max-connection: 250

You can read this yaml file as follows:

case class ConnectionConfig(timeoutMillis:Long, maxConnection:Int)
val config = YamlReader.load[ConnectionConfig]("config.yml", "default")

Internally wvlet-obj inspects ConnectionConfig parameterss, and builds the config object by mapping property values written in the Yaml file.