From the course: Advanced Java: Hands-on with Streams, Lambda Expressions, Collections, Generics and More
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Reflection API for dynamic code manipulation - Java Tutorial
From the course: Advanced Java: Hands-on with Streams, Lambda Expressions, Collections, Generics and More
Reflection API for dynamic code manipulation
- Now that we created custom annotations let's discuss the reflection, API in Java. The reflection API is a built-in Java functionality that allows us to inspect and manipulate our code at run time. The dynamic code manipulation can be used in combination with annotations to achieve interesting results. What do you think I mean by interesting? Reflection is a feature in Java that allows to inspect and interact with the metadata of classes, methods and fields at runtime. With reflection API we can do things like inspect the structure of a class, its methods, fields and annotations. Create new instances of a class, invoke methods and access fields. And even modify the behavior of a class, method or field at runtime. This definitely asks for some examples. So let's see how we can use the reflection API to inspect our custom annotation my custom annotation. So let's go over the steps. On line nine I'm grabbing info.class's…