Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: What scenarios call for the use of reflection?

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.

like image 513
KJW Avatar asked Feb 10 '12 04:02

KJW


People also ask

When should we use reflection in Java?

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.

Is it good practice to use reflection in Java?

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.

How can you use reflection?

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.

Where does Spring use reflection?

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.


2 Answers

Any time you're dealing with a string at runtime and want to treat part of that string as an identifier in the language.

  1. Remote procedure calling -- treat part of a message received over the network as a method name.
  2. Serialization and deserialization -- convert field names to string so you can write the object's fields to a stream and later convert it back into an object.
  3. Object-relational mappings -- maintain a relationship between fields in an object and columns in a database.
  4. Interfaces with dynamically typed scripting languages -- turn a string value produced by a scripting language into a reference to a field or method on an object.

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));
  }
}
like image 87
Mike Samuel Avatar answered Sep 18 '22 07:09

Mike Samuel


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.

like image 38
Sumit Singh Avatar answered Sep 21 '22 07:09

Sumit Singh