Loading…
Loading…
Topic
Articles and problems tagged Reflection.
Module services for the OCP 21 exam: the service provider interface, service locator, consumer and service provider, the uses and provides directives, ServiceLoader load, findFirst and stream with Provider, the rules for provider classes, and which directives each part needs.
Learn what reflection can reveal about Java generics despite erasure: the generic Class<T> class, matching Class<T> parameters, the Type interface with TypeVariable, WildcardType, ParameterizedType and GenericArrayType, and type literals (super type tokens) for generic types.
Build pluggable Java applications with java.util.ServiceLoader: define a service interface, write providers, register them in META-INF/services or with provides/uses in module-info, iterate lazily, select providers with stream() and Provider.type(), and use findFirst.
Finish Java reflection: create arrays of any type with java.lang.reflect.Array, grow arrays generically like Arrays.copyOf, call methods with Method.invoke, invoke constructors with arguments, handle InvocationTargetException, and compare reflection with method handles and lambdas.
Use Java reflection to inspect any class at runtime: Field, Method, Constructor and RecordComponent, getDeclaredFields vs getFields, Modifier and AccessFlag, sealed and record metadata, reading and writing fields with setAccessible, module restrictions, and a generic toString.
Start with Java reflection: what it is for, getting Class objects with getClass, Class.forName and .class literals, odd array type names, comparing Class objects, creating instances by name, a primer on checked exceptions, and loading resources with getResource and getResourceAsStream.
Read Java annotations at runtime with the AnnotatedElement API: getAnnotation, getAnnotationsByType, declared versus inherited annotations, and three hands-on tools built with reflection, an annotation-driven toString, a mini JUnit-style test runner and a record validator.