So from reading some of the articles, the message i got out of it was being able to modify fields and set values to classes in real time without recompiling.
so is it possible to do this to 3rd party java library created classes which no source code is available / is it possible to use reflection to modify class instances on run time?
in what other scenarios is reflection commonly used?
I am trying to understand how reflection can be applicable.
Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.
It is generally a bad idea to use reflection is application code, because you lose the strict type checking of the language. Reflection is generally for use by framework code, where it is essential. Good vs bad are not absolutes, but must be assessed in context.
You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them.
Spring framework uses dependency injection (DI) to populate the dependencies into beans defined in config files. DI framework actually heavily uses reflection for injecting these bean dependencies.
Any time you're dealing with a string at runtime and want to treat part of that string as an identifier in the language.
It can also be used to allow language features to be emulated in the language.
Consider the command line java com.example.MyClass
which turns a string into a class name. This doesn't require reflection, because the java
executable can turn a .class
file into code, but without reflection it would not be able to write java com.example.Wrapper com.example.MyClass
where Wrapper
delegates to its argument as in:
class Wrapper {
public static void main(String... argv) throws Exception {
// Do some initialization or other work.
Class<?> delegate = Class.forName(argv[0]);
Method main = delegate.getMethod("main", String[].class);
main.apply(null, Arrays.asList(argv).subList(1, argv.length).toArray(argv));
}
}
Reflection is used when it is needed to get into the other classes in deeper level
. So in most of the cases, these implementors have the container-behavior. For instance, dependency injection is mostly done with the use of reflection. If you need a framework as an example for that, Spring does its dependency injection jobs with the help of reflection API
.
You will also find reflections used behind the scenes in a large number of areas. For example, if you used JAXB, then a lot of the marshalling / unmarshalling of the XML will be done using reflections. Using Annotations in your code often results in reflections being used behind the scenes. When performing unit testing, particularly when mocking classes and/or methods, often has lots of reflections code being used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With