Java Reflection Mechanism: A Comprehensive Guide
Overview
Java Reflection is a powerful mechanism that allows developers to inspect and dynamically access the properties of a class, object, or interface at runtime. In this article, we will delve into the Java java.lang.reflect.Field class, which represents a class, object, or interface attribute, and explore its methods and usage.
Java.lang.reflect.Field Class
The java.lang.reflect.Field class is used to represent a class, object, or interface attribute. It provides a way to access and manipulate the attributes of a class, including private and public attributes.
Field Object Overview
A Field object represents a single attribute of a class, object, or interface. It provides several methods to access and manipulate the attribute, including:
getName(): Returns the name of the attribute.getType(): Returns the type of the attribute.get(Object obj): Returns the value of the attribute for the specified object.set(Object obj, Object value): Sets the value of the attribute for the specified object.setAccessible(boolean): Enables or disables access control permissions for the attribute.
Common Field Methods
Here are some common methods of the Field class:
| Method Name | Description |
|---|---|
getName() |
Returns the name of the attribute. |
getType() |
Returns the type of the attribute. |
get(Object obj) |
Returns the value of the attribute for the specified object. |
set(Object obj, Object value) |
Sets the value of the attribute for the specified object. |
setAccessible(boolean) |
Enables or disables access control permissions for the attribute. |
Example Code
Here is an example code snippet that demonstrates the usage of the Field class:
package com.rainmer.main;
import java.lang.reflect.Field;
import com.rainmer.bean.Book;
public class FieldTest {
// Method to get all attributes (including private) of a class
public void show(Class c1) {
Field[] fi = c1.getDeclaredFields(); // Get all attributes, whether public or private
System.out.println("~~~~~~~~~~~~ Get all have attributes (including private) ~~~~~~~~~~~~~~");
for (Field ff : fi) {
System.out.println(ff.getName()); // Property name
System.out.println(ff.getType()); // Attribute type
}
System.out.println("~~~~~~~~~~~~ can only get common attributes ~~~~~~~~~~~~~~");
Field[] fi1 = c1.getFields(); // Only get common attributes
for (Field ff : fi1) {
System.out.println(ff.getName()); // Property name
System.out.println(ff.getType()); // Attribute type
}
}
// Method to get attribute value of an object by its class
public void show(Object ob) {
System.out.println("~~~~~~~~~~~~~~~ acquired attribute value of the attribute and by the entity class object ~~~~~~~~~~~~~~~~~~ ~ ");
Class cl = ob.getClass();
Field[] fi = cl.getDeclaredFields();
try {
for (Field ff : fi) {
ff.setAccessible(true); // Enable access, not the private property became common attributes
System.out.println(ff.getName() + "value" + ff.get(ob));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Book bo = new Book();
bo.setId(1);
bo.setName("broken sky");
bo.setType("Fantasy");
FieldTest ft = new FieldTest();
ft.show(Book.class);
ft.show(bo);
}
}
Output
SetName method called ~~~ ~~~
~~~~~~~~~~~~ Get all have attributes (including private) ~~~~~~~~~~~~~~
id int
name class java.lang.String
type class java.lang.String
~~~~~~~~~~~~ can only get common attributes ~~~~~~~~~~~~~~
type class java.lang.String
~~~~~~~~~~~~~~~ acquired attribute value of the attribute and by the entity class object ~~~~~~~~~~~~~~~~~~ ~
id value 1
name value broken sky
type value Fantasy
This code snippet demonstrates how to use the Field class to get all attributes (including private) of a class, and how to get attribute value of an object by its class.