This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: What is reflection and why is it so useful?

I’m interested in Java, but I think the principles are the same in any language.

Answer a

Reflection is used to describe code that can check other code in the same system (or itself).

Let’s say you’re writing Java and you come across an object of unknown type and you want to call its doSomething() method (if it exists). Java’s static typing system does not support this requirement unless the object has a known interface. But with reflection, your code can find out if the object has a method called doSomething(), which you can call if you want.

Here is a test case, assuming that the object is named foo

Method method = foo.getClass().getMethod("doSomething".null);
method.invoke(foo, null);
Copy the code

A very common use case in Java is annotations. For example, Junit4 uses reflection to look for @test annotated methods in a class and then calls them during unit tests.

Here are some good use cases to learn from

http://docs.oracle.com/javase/tutorial/reflect/index.html
Copy the code

Finally, these concepts are very similar in other statically typed languages that support reflection, such as C#. In dynamically typed languages, these use cases are not necessary. Because the compiler allows any method to be called on any object, if the method does not exist, it fails at run time.

The ability to examine code in the system and see object types is not reflection, but type introspection. Reflection is the ability to make modifications at run time through introspection. This distinction is necessary here because some languages support reflection but not reflection, such as C++.

Answer two

Reflection allows you to dynamically instantiate new objects, invoke methods, and perform GET /set operations on class variables at run time without prior knowledge of their implementation.

Class myObjectClass = MyObject.class;
Method[] method = myObjectClass.getMethods();

//Here the method takes a string parameter if there is no param, put null.
Method method = aClass.getMethod("method_name", String.class); 

Object returnValue = method.invoke(null."parameter-value1");
Copy the code

In the above code, the null argument is the object of the method you want to call. If the method is static, null is supplied. If the method is not static, then you need to provide a valid MyObject instance instead of NULL at call time.

Reflection also allows you to access private members/methods of a class:

public class A{

  private String str= null;

  public A(String str) {
  this.str= str; }}Copy the code
A obj= new A("Some value");

Field privateStringField = A.class.getDeclaredField("privateString");

//Turn off access check for this field
privateStringField.setAccessible(true);

String fieldValue = (String) privateStringField.get(obj);
System.out.println("fieldValue = " + fieldValue);
Copy the code

To examine classes (also known as introspection), you don’t need to import the reflection package (java.lang.Reflect). Class metadata can be accessed through java.lang.class.

Reflection is a very powerful API, but if used excessively, it can slow down an application because it resolves all types at run time.

The article translated from Stack Overflow:stackoverflow.com/questions/3…