JAVA反射小案例

1 篇文章 0 订阅

最近看框架的时候发现自己的反射的知识不是特别清楚,随即就抽了点时间在网上把反射的知识补了一下,下面是我自己敲的一个小案例,见笑见笑大笑

package com.yc.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import com.yc.entity.Employee;

public class MyReflection {
	
	public static void main(String[] args) {
//		Employee e=new Employee();
//		System.out.println(e);
//		e=new MyReflection().invokeTest(Employee.class);
//		System.out.println(e);
		
//		new MyReflection().testForName("com.yc.entity.Employee");
		
		List<String> methodStrs=new MyReflection().showMethod(String.class);
		for (String s : methodStrs) {
			System.out.println(s);
		}
	}
	
	public void testForName(String path){
		try {
			Class<?> c=Class.forName(path);
			System.out.println(c.newInstance() instanceof Employee);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public <T> T invokeTest(Class c){
		T t = null;
		try {
			t=(T) c.newInstance();
			//类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。当然也包括它所实现接口的方法。
			Method[] ms=t.getClass().getDeclaredMethods();
			for (Method m : ms) {
				/*if(m.getName().equals("toString")){
					m.invoke(t);
				}*/
				if(m.getName().startsWith("set")){
					Class<?>[] types = m.getParameterTypes();
					if((types[0].getSimpleName()).equals("String")){
						m.invoke(t,"66666");
					} else if((types[0].getSimpleName()).equals("int")){
						m.invoke(t,2);
					}
				}
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return t;
	}
	
	//显示所有的方法
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public <T> List<String> showMethod(Class c){
		List<String> methodStrs=new ArrayList<String>();
		T t;
		try {
			//实例化所给的类
			t=(T) c.newInstance();
			
			//返回某个类的所有公用(public)方法包括其继承类的公用方法,当然也包括它所实现接口的方法。
			//Method[] ms=t.getClass().getMethods();
			
			//对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。当然也包括它所实现接口的方法。
			Method[] ms=t.getClass().getDeclaredMethods();
			
			String str;
			for (Method m : ms) {
				//获取方法的修饰符
				str=Modifier.toString(m.getModifiers())+" ";
				
				//获取返回类型
				Class<?> returnType=m.getReturnType();
				str+=returnType.getSimpleName()+" "+m.getName()+"(";
				
				//获取形参类型
				Class<?>[] types=m.getParameterTypes();
				if(types.length>0){
					for (int i=0;i<types.length;i++) {
						//获取类型名,不是全限定名
						str+=types[i].getSimpleName()+" "+"arg"+i+",";
					}
					str=str.substring(0, str.lastIndexOf(","));
				}
				str+=")";
				
				//获取抛出的异常
				Class<?>[] exceptions=m.getExceptionTypes();
				if(exceptions.length>0){
					str+=" throws ";
					for (Class<?> e : exceptions) {
						str+=e.getSimpleName()+",";
					}
					str=str.substring(0, str.lastIndexOf(","));
				}
				str+="{}";
				methodStrs.add(str);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return methodStrs;
	}
}


贴上showMethod方法的测试结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值