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
metadata = type(Runnable)
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
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
metadata = type(List<Int>)
print(metadata.isGenericInstantiation)
print(metadata.genericParameterCount)
print(metadata.genericParameterNames())
Annotations Through Reflection
metadata = type(klyn.annotations.Deprecated)
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.