Using reflection
If you know what type an object is, you can use reflection to get information about the type or
to perform actions on the object. Reflection means using type introspection to
query or modify objects at run time. You can use reflection to retrieve metadata about
classes, interfaces, fields, and methods at run time without knowing their names at compile
time. You can also instantiate new objects and call methods with this information. For
example, instead of calling an object method, you can get a list of methods from its type, or
use a String runtime value to call a method by name.
Only use reflection if there is no other way to do what you need. Avoid using reflection to get properties or call methods. In almost all cases, you can write Gosu code to avoid reflection. Using reflection dramatically limits how Gosu and Guidewire Studio can alert you to problems at compile time. Detecting errors at compile time is better than encountering unexpected behavior at run time.
- gw.lang.reflect
- Base reflection utilities for inspecting types, methods, annotations, and builders. The primary class is ReflectUtil, which provides object creation and method calls.
- gw.lang.reflect.features.*
- Reference packages for inspecting properties, methods, constructors, and blocks.
- gw.lang.reflect.gs
- Inspecting Gosu classes, methods, and properties.
- gw.lang.reflect.interval
- Reflection iterators, intervals, and sequences.
- gw.lang.reflect.java
- Inspecting Java classes, methods, and properties.
- gw.lang.reflect.json
- Inspecting JSON objects.
- gw.lang.reflect.module
- Inspecting modules and files.
To use reflection from Java or Gosu, use the utility class gw.lang.reflect.ReflectUtil, which contains methods to retrieve information about a class, including its properties and methods, and other methods to invoke object methods.
