Sunday 6 August 2017

Java reflection

1. Giới thiệu về java reflection

Java reflection là một tính năng của java, cho phép chương trình có thể biết được thông tin của một lớp(các thuộc tính, phương thức của nó), hoặc gọi các hàm của một lớp trong thời điểm chương trình thực thi (runtime).
Tại sao lại cần thiết phải có tính năng này?
Bởi vì đơn giản là có một số chương trình, lúc viết code  chưa xác định được rõ ràng đối tượng nào sẽ được tạo ra mà chỉ đến khi lúc chương trình chạy mới có được đối tượng thực sự, trong trường hợp đó chương trình sẽ phải sử dụng java reflection để biết được rõ hơn về thông tin đối tượng đó.

2. Một số ví dụ đơn giản sử dụng java reflection

Chương trình 1: In ra tất cả các biến và hàm của một lớp
Giả sử chúng ta có một lớp Person như sau:

package test;

public class Person {
 private String name;
 private String dateOfBirth;
 private int weight;
 private int height;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public int getWeight() {
  return weight;
 }

 public void setWeight(int weight) {
  this.weight = weight;
 }

 public int getHeight() {
  return height;
 }

 public void setHeight(int height) {
  this.height = height;
 }

}

Đây là chương trình chính sử dụng java reflection:
package test;

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

public class JavaReflectionExample {
 public static void main(String[] args) {
  Class personClass = Person.class;
  
  System.out.println("Attributes: ");
  Field[] fields = personClass.getDeclaredFields();
  for(Field field : fields){
   System.out.println(field.getName());
  }
  
  System.out.println("\nMethods: ");
  Method[] methods = personClass.getDeclaredMethods();
  
  for(Method method : methods){
   System.out.println(method.getName());
  }
 }
}

Và kết quả:
Attributes:
name
dateOfBirth
weight
height

Methods:
getName
setName
getDateOfBirth
setDateOfBirth
getWeight
setWeight
getHeight
setHeight

Ngoài khả năng cho phép biết được thông tin của một lớp, sử dụng reflection chúng ta còn có thể gọi một hàm của một lớp ở thời điểm thực thi chương trình
Chương trình 2: Gọi hàm của một lớp
Chương trình này sẽ sử dụng java reflection để gọi hàm tên là add để thực hiện phép cộng "2 + 3".

package test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class JavaReflectionExample1 {
 public int add(int num0, int num1){
  return num0 + num1;
 }
 
 public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  Class javaReflectionExample1Class = JavaReflectionExample1.class;
  Class[] methodParameterTypes = new Class[2];
  methodParameterTypes[0] = Integer.TYPE; // for num0 parameter
  methodParameterTypes[1] = Integer.TYPE; // for num1 parameter
  
  Method addMethod = javaReflectionExample1Class.getMethod("add", methodParameterTypes);
  
  JavaReflectionExample1 javaReflectionExample1 = new JavaReflectionExample1();
  
  Object retObj = addMethod.invoke(javaReflectionExample1, Integer.valueOf(2), Integer.valueOf(3));
  Integer retInt = (Integer)retObj;
  
  System.out.println("2 + 3 = " + retInt);
  
 }
}

Kết quả:
2 + 3 = 5

No comments:

Post a Comment