Java反射介紹
在運行狀態(tài)中,對于任意一個類,都能夠知道這個類的所有屬性和方法;對于任意一個對象,都能夠調用它的任意一個方法和屬性;這種動態(tài)獲取的信息以及動態(tài)調用對象的方法的功能稱為java語言的反射機制。
- 對象是表示或封裝一些數(shù)據(jù),一個類被加載后,JVM會創(chuàng)建一個對應的Class對象,類的整個結構信息會放到對應的Class對象中。這個Class對象就像一面鏡子一樣,通過這面鏡子可以看到對應類的全部信息。
- 加載完類之后,在堆內存中,就產(chǎn)生了一個Class類型的對象(一個類只有一個Class對象),這個對象就包含了完整的類的結構信息。
獲取Class實例的三種方式
class的實例也稱為類的類類型
TextView textView = findViewById(R.id.tv_info);
// 方式1
Class cls1 = textView.getClass();
// 方式2
Class cls2 = Class.forName("android.widget.TextView");
// 方式3
Class cls3 = TextView.class;
Java反射獲取類信息實例
該實例可以打印出整個類結構
package com.example;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
/**
* Created by nan on 17-8-31.
*/
public class ReflectionTest {
private static String name;
public static void main(String[] args){
if(args.length>0){
name=args[0];
}else{
Scanner scanner = new Scanner(System.in);
name = scanner.next();
}
try {
Class cls = Class.forName(name);
//類的修飾符
String clsModifiers = Modifier.toString(cls.getModifiers());
//類名稱
String clsName = cls.getName();
System.out.print(clsModifiers+" class "+clsName);
//父類名稱
Class superCls = cls.getSuperclass();
if(superCls!=null&&superCls!=Object.class){
System.out.print(" extends "+superCls.getName());
}
//接口名稱
Class[] impInterfaceClses = cls.getInterfaces();
if(impInterfaceClses.length>0){
System.out.print(" implements");
for(int i=0;i<impInterfaceClses.length;i++){
System.out.print(impInterfaceClses[i].getName());
if(i!=impInterfaceClses.length-1){
System.out.print(",");
}
}
}
System.out.println(" {");
//域
printFields(cls);
System.out.println();
//構造方法
printContructor(cls);
System.out.println();
//類方法
printMethods(cls);
System.out.println("}");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 域(成員和靜態(tài)成員)
* @param cls
*/
public static void printFields(Class cls) {
//Field[] fields = cls.getFields();//獲取到所有public類型的域
Field[] fields = cls.getDeclaredFields();//獲取到當前類的所有域包括私有域
for(Field field:fields){
System.out.print("\t");
System.out.print(Modifier.toString(field.getModifiers()));
System.out.print(" "+field.getType().getName());
System.out.print(" "+field.getName());
System.out.println(";");
}
}
/**
* 構造方法
*/
public static void printContructor(Class cls){
//Constructor[] constructors = cls.getConstructors();
Constructor[] constructors = cls.getDeclaredConstructors();
for(Constructor constructor:constructors){
System.out.print("\t");
System.out.print(Modifier.toString(constructor.getModifiers()));
System.out.print(" "+constructor.getName());
System.out.print("(");
Class[] clses = constructor.getParameterTypes();
if(clses.length>0){
for(int i=0;i<clses.length;i++){
System.out.print(clses[i].getName());
if(i!=clses.length-1){
System.out.print(",");
}
}
}
System.out.print(");");
System.out.println();
}
}
/**
* 成員方法和靜態(tài)方法
* @param cls
*/
public static void printMethods(Class cls) {
Method[] methods = cls.getDeclaredMethods();
for(Method method:methods){
System.out.print("\t");
System.out.print(Modifier.toString(method.getModifiers()));
System.out.print(" "+method.getReturnType().getName());
System.out.print(" "+method.getName());
System.out.print("(");
Class[] clses = method.getParameterTypes();
if(clses.length>0){
for(int i=0;i<clses.length;i++){
System.out.print(clses[i].getName());
if(i!=clses.length-1){
System.out.print(",");
}
}
}
System.out.print(");");
System.out.println();
}
}
}
Java反射數(shù)組
數(shù)組相關API
// java.lang.reflect.Array
static int getLength(Object array);//獲取數(shù)組的長度
static Object newInstance(Class componentType,int length);//創(chuàng)建數(shù)組元素為componentType類類型,數(shù)組長度為length的數(shù)組
// java.lang.Class
cls.isArray()//判斷該class類型是否是數(shù)組類型
cls.getComponentType()//獲取數(shù)組元素類型
- 動態(tài)拷貝數(shù)組
public static Object copyOf(Object o){
if(o==null){
return null;
}
Class cls = o.getClass();
if(!cls.isArray()){
return null;
}
Class componentType = cls.getComponentType();
int length = Array.getLength(o);
Object newArray = Array.newInstance(componentType,length);//根據(jù)元素類型和長度創(chuàng)建出相應元素類型的數(shù)組
System.arraycopy(o,0,newArray,0,length);
return newArray;
}