Take what you've learned about Java reflection

motivation

Java has a feature called reflection to get the type information of an object. I knew the name and outline so far, but I wanted to know it properly, so I will summarize it.

What is reflection?

Java allows you to dynamically retrieve and manipulate type information at runtime. In information engineering, the technology of reading or rewriting the structure of the program itself in the process of executing the program is called reflection. Java reflection mainly provides the following functions.

Freywork and others use this reflection to access fields and call methods.

How Java reflection works

The Java reflection mechanism is made possible by utilizing the following two classes and functions.

java.lang.Class Java provides a class called java.lang.Class that represents a class. Each class or interface handled in the source code has a corresponding Class object. This is expressed as Class <String> for String, for example. This class is special and cannot be instantiated using new etc. Instead, it is generated by the JVM when loading the class, or built automatically by calling the class loader's defineClass method. In Java, it is possible to get type information from the generated Class object.

Class loader

A class loader is an object that literally has the role of loading a class. When you get the Class object from the source code, you get it internally from this class loader.

Try to access the class information by using reflection.

It's so easy, but I'd like to use reflection. First, prepare the following simple class.

public class Foo {
	
	Foo(String name){
		this.name = name;
	}
	
	private String name;
	
	public void greet(){
		System.out.println("Hello, " + name);
	}
}

Try calling the method.

Let's call Foo # greet () using reflection on the above Foo.class class.

import java.lang.reflect.Method;

public class ReflectionSample {

	public static void main(String[] args) {

		Class<Foo> fooClazz1 = Foo.class;
		Foo foo = new Foo("taro");

		try {
			Method greetMethod = fooClazz1.getMethod("greet");
			greetMethod.invoke(foo);
		} catch (ReflectiveOperationException e) {
			e.printStackTrace();
		}
	}
}

A method call for a class is made using java.lang.reflect.Method # invoke. The Method class can be obtained from Class # getMethod. In that case, specify the method name you want to call. This time Foo # greet () has no argument, but you can pass the argument of the method to be called after the second argument.

Execution result

Hello, taro

Impressions

I don't usually use it when writing business applications, but I thought that I might have some knowledge about this when reading framework code.

Reference books & sites

Recommended Posts

Take what you've learned about Java reflection
What I researched about Java 8
What I researched about Java 6
What you learned about hashes
What I researched about Java 9
What you learned about symbols
What I researched about Java 7
[Android / Java] Learned about DataBinding
What I learned about Kotlin
What I researched about Java 5
What I learned with Java Gold
What I learned with Java Silver
What I researched about Java learning
What I learned from Java monetary calculation
[Java] About Java 12 features
Summary of what I learned about Spring Boot
What is java
[Java] About arrays
Something about java
Where about java
About Java features
What is Java <>?
About Java threads
[Java] About interface
What I learned in Java (Part 2) What are variables?
What is Java
What i learned
About Java class
About Java arrays
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
I will write what I learned about docker anyway (second)
What I learned in Java (Part 3) Instruction execution statement
What I learned when building a server in Java
I will write what I learned about docker anyway (first)
About Java log output
About Java functional interface
About class division (Java)
About [Java] [StreamAPI] allMatch ()
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
What is Java Encapsulation?
[Java Silver] About initialization
What I learned ② ~ Mock ~
About Java Array List
About Java Polymorphism super ()
What I learned ① ~ DJUnit ~
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
What is Java technology?
About Java entry points
About Java 10 Docker support
Personal summary about Java
What is Java API-java