Java之于反射

1. getMethods()与getDeclaredMethods()的区别

     public Method[] getMethods()返回某个类的所有公用(public)方法包括其继承类的公用方法,当     然也包括它所实现接口的方法

     public Method[] getDeclaredMethods()对象表示的类或接口声明的所有方法,包括公共、保护、默     认(包)访问和私有方法,但不包括继承的方法。当然也包括它所实现接口的方法


2.getFields()与getDeclaredFields()的区别

   getDeclaredFields()返回所有字段包括私有字段

    getFields()只返回公有字段即public修饰的字段



学习反射获取传入JavaBean的参数名与参数值并转为Map

方法一:

public static Map<String, Object> convertBean(Object bean) 
            throws IntrospectionException, IllegalAccessException, InvocationTargetException { 
        Class type = bean.getClass(); 
        Map<String, Object> returnMap = new HashMap<String, Object>(); 
        BeanInfo beanInfo = Introspector.getBeanInfo(type); 
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors(); 
        for (int i = 0; i< propertyDescriptors.length; i++) { 
            PropertyDescriptor descriptor = propertyDescriptors[i]; 
            String propertyName = descriptor.getName(); 
            System.out.println(propertyName);
            if (!propertyName.equals("class")) { 
                Method readMethod = descriptor.getReadMethod(); 
                Object result = readMethod.invoke(bean, new Object[0]); 
                if (result != null) { 
                    returnMap.put(propertyName, result); 
                } else { 
                    returnMap.put(propertyName, ""); 
                } 
            } 
        } 
        return returnMap; 
    } 
	
	
	
	
	public static void main(String[] args) {
		Student s = new Student();
		s.setAge(12);
		s.setSex("nan");
		s.setUserName("张^^^");
		Course course = new Course();
		course.setCourseName("高数");
		course.setCourseCode("BH675FG");
		s.setCourse(course);
		try {
			Map<String, Object> gh = convertBean(s);
			Iterator<Entry<String, Object>> iterator = gh.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<java.lang.String, java.lang.Object> entry = (Map.Entry<java.lang.String, java.lang.Object>) iterator
						.next();
				System.out.println(entry);
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



方法二:

<pre name="code" class="java">package com.orgJson;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.model.Course;
import com.model.Student;

public class JsonUtil {
	public static Map<Object, Object> toMap(Object javaBean) {
		Map<Object, Object> result = new HashMap<Object, Object>();
		Method[] methods = javaBean.getClass().getDeclaredMethods();
		for (Method method : methods) {
			try {
				if (method.getName().startsWith("get")) {
					String field = method.getName();
					System.out.println("刚拿到方法名称" + field);
					field = field.substring(field.indexOf("get") + 3);
					System.out.println("get截取掉" + field);
					field = field.toLowerCase().charAt(0) + field.substring(1);
					System.out.println("put map前" + field);
					Object value = method.invoke(javaBean, (Object[]) null);
					result.put(field, null == value ? "" : value.toString());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static void main(String[] args) {
		Student s = new Student();
		s.setAge(12);
		s.setSex("nan");
		Course course = new Course();
		course.setCourseName("高数");
		s.setCourse(course);
		Map<Object, Object> g = toMap(s);
		Iterator<Entry<Object, Object>> d = g.entrySet().iterator();
		while (d.hasNext()) {
			Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) d
					.next();
			System.out.println(entry);
		}
	}
}

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值