关闭

java的反射

标签: 反射
109人阅读 评论(0) 收藏 举报
分类:
package com.manager;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;

public class TestReflection01 {

	public static void main(String[] args) {
      System.out.println("input the type of the class");
      Scanner scan = new Scanner(System.in);
      String className= scan.next();
      System.out.println("current class name : " + className);
      try {
    	  //获取CLASS对象
    	  Class c = Class.forName(className);
    	  
    	  //解析属性信息
    	  Field[] fs = c.getFields();
    	  for(Field f : fs){
    		  System.out.println("-------------------------------");
    		  System.out.println("属性:" + f.toString());
    		  System.out.println("数据类型:" + f.getType());
    		  System.out.println("属性名:" + f.getName());
    		  int mod = f.getModifiers();
    		  System.out.println("属性修饰符:" + Modifier.toString(mod));
    		  System.out.println("-------------------------------");    		  
    	  }
    	  
    	  //解析方法信息t
    	  Method[] ms = c.getDeclaredMethods();
    	  for(Method m : ms){
    		  System.out.println("--------------------------------");
    		  System.out.println("方法:" + m.toString());
    		  System.out.println("方法名:" + m.getName());
    		  int mod = m.getModifiers();
    		  System.out.println("方法修饰符:" + Modifier.toString(mod));
    		  System.out.println("方法参数列表");
    		  Class pts[] = m.getParameterTypes();
    		  for(int i = 0; i < pts.length; i++){
    			  Class clazz = pts[i];
    			  if(i != 0){
    				  System.out.println(clazz);
    			  }
    		  }
    		  System.out.println();
    		  System.out.println("返回值类型:" + m.getReturnType());
    	  }
    	  
    	  //解析构造方法信息
    	  Constructor[] cs = c.getConstructors();
    	  for(Constructor con : cs){
    		  System.out.println("----------------------------------");
    		  System.out.println("构造方法:" + con.toString());
    		  System.out.println("方法名:" + con.getName());
    		  int mod = con.getModifiers();
    		  System.out.println("方法修饰符:" + Modifier.toString(mod));
    		  System.out.println("方法参数列表");
    		  Class pts[] = con.getParameterTypes();
    		  for(int i = 0; i < pts.length; i++){
    			  Class clazz = pts[i];
    			  if(i != 0){
    				  System.out.println(clazz);
    			  }
    		  }
    		  System.out.println();
    		  System.out.println("-----------------------------------");
    	  }
    	  
    	  //解析当前类类型的父类
    	  
    	   Class superClass = c.getSuperclass();
    	   System.out.println("当前类的父类:" + superClass.toString());
    	   System.out.println("-------------------");
    	  //解析当前类实现的接口
    	   Class[] interfaces = c.getInterfaces();
    	   System.out.println("当前类所实现的接口");
    	   for(Class class1 : interfaces){
    		   System.out.println(class1.toString());
    	   }
    	  //解析当前是类型所在包信息
    	   Package p = c.getPackage();
    	   System.out.println("当前类所在包:" + p.toString());
      } catch (ClassNotFoundException e) {
    	  e.printStackTrace();
      }
      //解析方法信息
      
	}

}

package com.manager;

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

class User{
	public String name;
	public int age;
	public static int total = 100;
	User(){
		super();
	}
	User(String name, int age){
		super();
		this.name = name;
		this.age = age;
	}
	public void SetAll(String name, int age){
		this.name = name;
		this.age = age;
	}
	public static void showAll(){
		System.out.println("int static method showTotal , total = " + total);
	}
	public String toString(){
		return "userName = " + name + "/t age = " + age;
	}
	
}
public class TestReflection02 {

	public static void main(String[] argss) {
		try{
			User p1 = new User("张三", 18); 
			TestReflection02 t = new TestReflection02();
			System.out.println("------------------------------------------");
			t.mf1(p1, "name" ,"李四");
			t.mf1(p1, "age", 30);
			System.out.println(p1);
			System.out.println("------------------------------------------");
			t.mf2("com.manager.User", "total", 88);
			System.out.println("------------------------------------------");
			Class[]argTypes = new Class[]{String.class, int.class};
			Object[] args = new Object[]{"王五", 99};
			t.mf3(p1, "SetAll", argTypes, args);
			System.out.println(p1);
			System.out.println("------------------------------------------");
			t.mf4("com.manager.User", "showAll", new Class[0], new Object[0]);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public void mf1(Object  o, String fieldName, Object newValue) 
				throws SecurityException, NoSuchFieldException, 
				IllegalArgumentException, IllegalAccessException {
		Class c = o.getClass();
		Field f = c.getField(fieldName);
		Object fv = f.get(o);
		System.out.println("修改前:" + fieldName + "=" + fv);
		f.set(o, newValue);
		System.out.println("修改前:" + fieldName + "=" + f.get(o));
	}
	
	public void mf2(String className, String fieldName, Object newValue) 
				throws ClassNotFoundException, SecurityException, 
				NoSuchFieldException,
                                IllegalArgumentException,IllegalAccessException {
		Class c = Class.forName(className);
		Field f = c.getField(fieldName);
		Object fv = f.get(c);
		System.out.println("修改前:" + fieldName + "=" + fv);
		f.set(c, newValue);
		System.out.println("修改前:" + fieldName + "=" + f.get(c));
	}
	
	public void mf3(Object o, String MethodName, Class[] argsType, Object[] args) 
			throws ClassNotFoundException, NoSuchMethodException, 
			IllegalArgumentException, IllegalAccessException, 
                        InvocationTargetException {
		Class c = o.getClass();
		Method m = c.getMethod(MethodName, argsType);
		Object result = m.invoke(o, args);
		System.out.println("result : " + result);
	}
	
     public void mf4(String className, String MethodName, Class[] argsType, Object[] args) 
			throws ClassNotFoundException, NoSuchMethodException, 
			IllegalArgumentException, IllegalAccessException, 
                        InvocationTargetException {
		Class c = Class.forName(className);
		Method m = c.getMethod(MethodName, argsType);
		Object result = m.invoke(c, args);
		System.out.println("result : " + result);
}

}

0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    博客专栏
    个人资料
    • 访问:524836次
    • 积分:10506
    • 等级:
    • 排名:第1569名
    • 原创:454篇
    • 转载:541篇
    • 译文:3篇
    • 评论:30条
    文章分类
    最新评论