1. JAVA reflection allows you to know all the properties and methods of any class in the running state. For any object, you can call any of its methods and properties; This ability to dynamically retrieve information and invoke methods on objects is called the Reflection mechanism of the Java language. (See Baidu Encyclopedia)
2. Main functions: Java reflection mechanism mainly provides the following functions: at runtime to determine the class of any object; Construct an object of any class at runtime; Determine which member variables and methods any class has at run time. Call a method of any object at runtime; Generate dynamic proxies.
1. Use the class name to get the methods and attributes that the class has
First, define a class for subsequent operations
package ObjectDemo;
public class Coordinate {
private float x;
private float y;
private float z;
public float threshold=0.001 f; / / threshold
public float getX(a) {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getZ(a) {
return z;
}
public void setZ(float z) {
this.z = z;
}
public float getY(a) {
return y;
}
public void setY(float y) {
this.y = y;
}
// No argument constructor
public Coordinate(a)
{
this.x = 0;
this.y = 0;
this.z = 0;
}
public Coordinate(float x,float y,float z)
{
this.x = x;
this.y = y;
this.z = z;
}
// Check whether two coordinates are equal
private boolean isEqual(Coordinate other)
{
if(Math.abs(other.getX()-this.getX())>threshold)return false;
if(Math.abs(other.getY()-this.getY())>threshold)return false;
if(Math.abs(other.getZ()-this.getZ())>threshold)return false;
return true;
}
// Output attributes
public void showThings(a)
{
System.out.println("X:"+x+" Y:"+y+" Z:"+z);
}
public void showThings(String name)
{
System.out.println(name+" X:"+x+" Y:"+y+" Z:"+z); }}Copy the code
Get information about the class
package ReflectDemo2;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
public class ClassDetail {
private Class aClass;
public ClassDetail(String className)
{
try {
aClass = Class.forName(className);
} catch(ClassNotFoundException e) { e.printStackTrace(); }}// Get method details
public void getMethodsDetail(a)
{
Method methods[]=aClass.getDeclaredMethods();
for(Method method:methods)
{
// We can use the Method class's numerous get methods to get information about it
System.out.print(Modifier.toString(method.getModifiers()));
System.out.print("" + method.getReturnType());
System.out.print("" + method.getName()+'(');
Parameter parameters[] = method.getParameters();
int length = parameters.length;
if(length>0)
{
for (int i = 0; i < length - 1; ++i) {
System.out.print(parameters[i].getType() + "" + parameters[i].getName() + ",");
}
System.out.print(parameters[length - 1].getType() + "" + parameters[length - 1].getName());
}
System.out.print(")" + '\n'); }}// The detailed method to get the attribute
public void getFieldDetail(a)
{
//getDeclaredFields gets all attributes, getFields gets non-private
Field fields[] = aClass.getDeclaredFields();
for(Field field:fields)
{
System.out.println(Modifier.toString(field.getModifiers())+""+field.getType()+""+field.getName()); }}}Copy the code
call
public class RfMain {
public static void main(String[] args) {
ClassDetail detail = new ClassDetail("ObjectDemo.Coordinate"); detail.getMethodsDetail(); detail.getFieldDetail(); }}Copy the code
The output
private boolean isEqual(class ObjectDemo.Coordinate arg0)
public float getX()
public void setX(float arg0)
public float getY()
public void setY(float arg0)
public float getZ()
public void setZ(float arg0)
private float x
private float y
private float z
public float threshold
2. Use reflection mechanism to construct class objects and call class methods
No argument construction, no argument method call
public static void main(String[] args) {
try {
Class cls = Class.forName("ObjectDemo.Coordinate");
Object object = cls.newInstance();
Method method = cls.getMethod("showThings");
method.invoke(object);
} catch(Exception e) { e.printStackTrace(); }}Copy the code
Results:
Z: 0.0 X: Y 0.0:0.0
Calls to parameterless methods with argument constructs
public static void main(String[] args) { try { Class cls = Class.forName("ObjectDemo.Coordinate"); Constructor cons = cls.getConstructor(float.class,float.class,float.class); Object the Object = cons. NewInstance (2 and 4); Method method = cls.getMethod("showThings"); method.invoke(object); } catch (Exception e) { e.printStackTrace(); }}Copy the code
Results:
Z: 3.0 X: Y 2.0:4.0
There is no parameter construction, there is a parameter method call
public static void main(String[] args) { try { Class cls = Class.forName("ObjectDemo.Coordinate"); Object object = cls.newInstance(); Method method = cls.getMethod("showThings",String.class); method.invoke(object, "Quincy say:"); } catch (Exception e) { e.printStackTrace(); }}Copy the code
Results:
Quincy say: X:0.0 Y:0.0 Z:0.0
So much for reflection, what does it really do? Hehe!! Now let’s talk about one of its applications, such as database operations, we define some classes and the same table structure of the database, we can use the reflection mechanism to generate SQL statements, and as long as write once, is not very convenient?