Advanced topics Reflection

# Reflection

Reflection in Klyn starts with `type(...)` and expands into metadata about members, inheritance, annotations, generics, and more. This is especially useful for tooling, tests, and framework-style code.

Acquiring a `Type`

    import klyn.math

    r = Rational(1, 3)
    metadata = type(r)
    print(metadata.name)
    print(metadata.fullName)

Kind Flags

    interface Worker:
        public run() as Void

    metadata = type(Worker)
    assert metadata.isInterface
    assert metadata.isAbstract

Reflection exposes whether a type is public, abstract, final, native, an interface, an enum, or an annotation.

Inspecting Members

    import klyn.math

    metadata = type(Rational(1, 3))
    attrs = metadata.getAttributes()
    props = metadata.getProperties()
    methods = metadata.getMethods()

You can inspect declared members, retrieve a member by name, and choose whether inherited members should be included.

Generic Reflection

    import klyn.collections

    metadata = type(List<Int>)
    print(metadata.isGenericInstantiation)
    print(metadata.genericParameterCount)
    print(metadata.genericParameterNames())

Annotations Through Reflection

    annotation Audited:
        pass

    metadata = type(Audited)
    print(metadata.isAnnotation)

Reflection can also expose annotations attached to types and members, which is how test frameworks and metadata-driven tooling discover annotated declarations.