Home  >  Article  >  Java  >  Java Reflection Mechanism Analysis Guide

Java Reflection Mechanism Analysis Guide

黄舟
黄舟Original
2017-02-06 16:17:421254browse

1. Is JAVA a dynamic language?

Generally speaking, when it comes to dynamic languages, it means that the program structure or variable type is allowed to be changed while the program is running. From this point of view, JAVA, like C++, is not a dynamic language.

But JAVA has a very prominent dynamic related mechanism: reflection. Through reflection, Java can load, detect and use classes that were fully summed during compilation at runtime, generate their object entities, call their methods or set values ​​for properties. So Java is considered a semi-dynamic language.


The concept of reflection:


The reflection mechanism in Java means that in the running state, for any Class, you can know all the properties and methods of this class;


#For any object, you can call any of its methods;


This function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language


2. Dynamic nature


2.1. Dynamic nature


●Generate object instance during runtime;

●Call method during runtime;

●Change properties during runtime


##2.2. Functions that the Java reflection mechanism can achieve


●In Determine the class to which any object belongs at runtime

●Construct an object of any class at runtime

●Determine the methods and attributes of any class at runtime

●Call the method of any object at runtime

●Generate dynamic proxy

2.3. Java reflection application scenarios

Many objects in Java programs will There are two types: compile-time type and run-time type

The compile-time type is determined by the type used when declaring the object, and the run-time type is determined by the type actually assigned to the object

For example:

Person p =new Student();

The type is Person when compiling and Student

In addition, the program may also receive an object passed in from the outside during runtime. The compilation of the object The type is Object, but the program needs to call the method of the runtime type of the object. To solve these problems, programs need to discover real information about objects and classes at runtime. However, if it is impossible to predict which classes the object and class may belong to at compile time, and the program only relies on runtime information to discover the real information of the object and class, reflection must be used at this time

3. Java Reflection API


#The reflection API is used to generate information about classes, interfaces or objects in the current JAVA virtual machine.

●Class class: The core class of reflection, which can obtain the attributes, methods and other content information of the class

●Field class: Java.lang.reflect. Represents the attributes of the class, which can be obtained and set The attribute value of the class.

●Method class: Java.lang.reflect. Represents a method of a class, which can be used to obtain information about methods in a class or execute methods

Construcor Class: Java.lang.reflect. Represents the constructor method of the class.

4. Get all methods and properties


Person class

package com.pb.Reflect.classinfo;

public class Person {
private String name;
private String gender;
private int age;

private Person() {
//
}
public Person(String name, String gender, int age) {
super();
this.name = name;
this.gender = gender;
this.age = age;
}
//getter、和setter方法
private String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public String toString(){
return "姓名:"+name+"年龄: "+age;
}

}

Use reflection:

package com.pb.Reflect.classinfo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.swing.JOptionPane;

/*
* 通过用户输入类的全路径,来获取该类的成员方法和属性
* Declared获取全部不管是私有和公有
* 1.获取访问类的Class对象
* 2.调用Class对象的方法返回访问类的方法和属性信息
*/
public class ReflectDemo {

/*
* 构造方法
*/
public ReflectDemo(){
//用户输入类的全路径径
//使用String组件
String classpsth=JOptionPane.showInputDialog(null,"输入类的全路径");
//使用Class.forName方法根据输入的类的全路径 返回该类的Class对象
try {
Class cla = Class.forName(classpsth);
//利用Class对象的cla的自审,返回方法对象集合
Method [] method=cla.getDeclaredMethods(); //返回所有的方法
System.out.println("========获取方法信息============");
for (Method meth : method) {
//遍历method数组,并输出方法信息
System.out.println(meth.toString());
}
System.out.println("========获取出方法信息结束============");
//获取属性利用Class对象的cla的自审,返回成员属性对象集合
Field [] field=cla.getDeclaredFields();
System.out.println("========获取成员属性信息============");
for (Field f : field) {
System.out.println(f.toString());
}
System.out.println("========获取成员属性信息结束============");
//获取属性利用Class对象的cla的自审,返回构造方法集合
Constructor [] constructor=cla.getDeclaredConstructors();
System.out.println("========获取成员构造方法信息============");
for (Constructor constru : constructor) {
System.out.println(constru.toString());
}
System.out.println("========获取成员构造方法信息结束============");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("路径输入错误!");
}
}

}
package com.pb.Reflect.classinfo;

public class TestReflection {

public static void main(String[] args) {
ReflectDemo rd=new ReflectDemo();

}

}

Enter com. pb.Reflect.classinfo.Person

Result:

========获取方法信息============
public java.lang.String com.pb.Reflect.classinfo.Person.getGender()
public void com.pb.Reflect.classinfo.Person.setGender(java.lang.String)
public int com.pb.Reflect.classinfo.Person.getAge()
public void com.pb.Reflect.classinfo.Person.setAge(int)
public java.lang.String com.pb.Reflect.classinfo.Person.toString()
private java.lang.String com.pb.Reflect.classinfo.Person.getName()
private void com.pb.Reflect.classinfo.Person.setName(java.lang.String)
========获取出方法信息结束============
========获取成员属性信息============
private java.lang.String com.pb.Reflect.classinfo.Person.name
private java.lang.String com.pb.Reflect.classinfo.Person.gender
private int com.pb.Reflect.classinfo.Person.age
========获取成员属性信息结束============
========获取构造方法信息============
private com.pb.Reflect.classinfo.Person()
public com.pb.Reflect.classinfo.Person(java.lang.String,java.lang.String,int)
========获取构造方法信息结束============

The above is the content of the Java reflection mechanism analysis guide. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn