java反射简介

 

想要了解反射,最基本的类就是Class类,因为Class能够保存类的信息。在类中含有构造方法、函数、属性。这些都能够通过某种手段得到。

1.获得字节码的方法:

(1)Class.forName(String)

(2)p.getClass();

(3)P.class;

Class c1 =...;

c1.getDeclaredConstructors();返回一个Constructor数组。    包含一个类中的构造函数。

2.通过构造方法进行反射:newInstance

getConstructor(Class)进行对某个类的构造函数进行声明。

newInstance(Object);能够创建一个实例。

以下为实例:

  1. Constructor c = String.class.getConstructor(StringBuffer.class);  
  2.             String str = (String) c.newInstance(new StringBuffer("123"));  
  3.             System.out.println(str);  
这样就能够创建一个String的实例。

3.对于域的反射:get

能够反映出当前对象的域的值是多少.

私有变量的反射需要f.setAccessible(true);

  1. import java.lang.reflect.Constructor;  
  2. import java.lang.reflect.Field;  
  3.   
  4. public class ReflectField {  
  5.   
  6.     public static void main(String[] args) {  
  7.         ReflectPoint o = new ReflectPoint(12);  
  8.         Field fy = null;  
  9.         try {  
  10.             fy = o.getClass().getDeclaredField("y");  
  11.             fy.setAccessible(true);  
  12.             Integer x = (Integer) fy.get(o);  
  13.   
  14.             System.out.println(x);  
  15.               
  16.         } catch (Exception e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  
  20. }  
  21.   
  22. class ReflectPoint {  
  23.     private int x;  
  24.   
  25.     private int y;  
  26.   
  27.     public ReflectPoint(int x, int y) {  
  28.         super();  
  29.         this.x = x;  
  30.         this.y = y;  
  31.     }  
  32.   
  33. }  

示例:有一个类,其中包含很多字符串的属性,想要把每个字符串属性中的‘b’变成‘a’,并显示结果

  1. import java.lang.reflect.AccessibleObject;  
  2. import java.lang.reflect.Constructor;  
  3. import java.lang.reflect.Field;  
  4.   
  5. public class ReflectField {  
  6.   
  7.     public static void main(String[] args) {  
  8.         ReflectPoint o = new ReflectPoint(12);  
  9.         Field[] fs ;  
  10.         try {  
  11.             fs = o.getClass().getDeclaredFields();  
  12.             AccessibleObject.setAccessible(fs, true);  
  13.             for(Field f:fs)  
  14.             {  
  15.                 if(f.getType() ==String.class)  
  16.                 {  
  17.                     String str = (String)f.get(o);  
  18.                     String result = str.replace("b""a");  
  19.                     f.set(o, result);  
  20.                     System.out.println(result);  
  21.                 }  
  22.             }  
  23.               
  24.               
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29. }  
  30.   
  31. class ReflectPoint {  
  32.     private int x;  
  33.   
  34.     private int y;  
  35.       
  36.       
  37.     private String str1 = "basketball";  
  38.     private String str2 = "ball";  
  39.     private String str3 = "it";  
  40.     public ReflectPoint(int x, int y) {  
  41.         super();  
  42.         this.x = x;  
  43.         this.y = y;  
  44.     }  
  45.   
  46. }  
4.方法反射:

(1)getMethod()声明

(2)invoke()方法调用

  1. import java.lang.reflect.AccessibleObject;  
  2. import java.lang.reflect.Constructor;  
  3. import java.lang.reflect.Field;  
  4. import java.lang.reflect.Method;  
  5.   
  6. public class ReflectField {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ReflectPoint o = new ReflectPoint();  
  10.         try {  
  11.   
  12.             Method m = o.getClass().getDeclaredMethod("getStr1"null);  
  13.             String name = (String) m.invoke(o, null);  
  14.             System.out.println(name);  
  15.               
  16.         } catch (Exception e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  
  20. }  
  21.   
  22. class ReflectPoint {  
  23.    
  24.     private String str1 = "busketBall";  
  25.     public ReflectPoint() {  
  26.         super();  
  27.     }  
  28.     public String getStr1()  
  29.     {  
  30.         return str1;  
  31.     }  
  32. }  




Constructor:

(1)getModifiers();返回访问权限。能够由Modifier.toString(i)转换成字符串。

(2)getName();返回构造函数的函数名。

(3)getParameterTypes()返回参数的Class,需要对每个参数Class调用getName()获得参数类型。

c1.getDeclaredFields();返回一个Field数组。            包含一个类中的属性。

Field:

(1)getType()返回属性类型

(2)f.get(Object obj);Obj是一个类,包含f属性,返回当前f属性的当前值。

(3)int i=getModifiers()返回访问权限。能够由Modifier.toString(i)转换成字符串。

(4)getName()返回属性名称。

(5)f.set(Object obj,Object val);把Object类中的f属性设置为val值。

c1.getDeclaredMethods()返回一个Method数组。     包含一个类中的一般方法。

Method:

(1)getModifiers()返回访问权限。能够由Modifier.toString(i)转换成字符串。

(2)getName();返回函数的函数名。

(3)getParameterTypes()返回参数的Class,需要对每个参数Class调用getName()获得参数类型。

(4)getReturnType()返回值的类型


下面是一个示例代码,输入一个类,然后把输入类的构造方法,域,函数都打印出来:

  1. import java.lang.reflect.Constructor;  
  2. import java.lang.reflect.Field;  
  3. import java.lang.reflect.Method;  
  4. import java.lang.reflect.Modifier;  
  5. import java.util.Scanner;  
  6.   
  7.   
  8. public class ReflectTest {  
  9.     public static void main(String args[])  
  10.     {  
  11.         Scanner in = new Scanner(System.in);  
  12.         String str = in.nextLine();  
  13.         Class c = null;  
  14.         try {  
  15.             c = Class.forName(str);  
  16.         } catch (ClassNotFoundException e) {  
  17.             System.out.println("********出错!********");  
  18.             e.printStackTrace();  
  19.         }  
  20.         String tmp = Modifier.toString(c.getModifiers());  
  21.         System.out.println(tmp+" class "+c.getName()+"{");  
  22.         printField(c);  
  23.         printConstructor(c);  
  24.         printMethod(c);  
  25.         System.out.println("}");  
  26.           
  27.     }  
  28.     public static void printField(Class c)  
  29.     {  
  30.         Field[] f = c.getDeclaredFields();  
  31.         for(int i=0;i<f.length;i++)  
  32.         {  
  33.             int p = f[i].getModifiers();  
  34.             String tmp = Modifier.toString(p);  
  35.             String type = f[i].getType().getName();  
  36.             String name = f[i].getName();  
  37.             System.out.println("  "+tmp+" "+type+" "+name+";");  
  38.         }  
  39.     }  
  40.     public static void printConstructor(Class c)  
  41.     {  
  42.         Constructor[] con = c.getDeclaredConstructors();  
  43.         for(int i=0;i<con.length;i++)  
  44.         {  
  45.             int p = con[i].getModifiers();  
  46.             String tmp = Modifier.toString(p);  
  47.             String name = con[i].getName();  
  48.             Class[] param = con[i].getParameterTypes();  
  49.             System.out.print("  "+tmp+" "+name+"(");  
  50.             for(int j=0;j<param.length;j++)  
  51.             {  
  52.                 System.out.print(param[j].getName());  
  53.             }  
  54.             System.out.println(");");  
  55.   
  56.         }  
  57.     }  
  58.     public static void printMethod(Class c)  
  59.     {  
  60.         Method[] m = c.getDeclaredMethods();  
  61.         for(int i=0;i<m.length;i++)  
  62.         {  
  63.             int p = m[i].getModifiers();  
  64.             String tmp = Modifier.toString(p);  
  65.             String type = m[i].getReturnType().getName();  
  66.             String name = m[i].getName();  
  67.             Class[] param = m[i].getParameterTypes();  
  68.             System.out.print("  "+tmp+" "+type+" "+name+"(");  
  69.             for(int j=0;j<param.length;j++)  
  70.             {  
  71.                 System.out.print(param[j].getName());  
  72.             }  
  73.             System.out.println(");");  
  74.   
  75.         }  
  76.     }  
  77. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值