Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Field;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class Main {
    public static Map<String, String> convertBean(Object bean) {
        if (bean == null)
            return null;
        try {
            Class<? extends Object> clazz = bean.getClass();
            Map<String, String> returnMap = new HashMap<String, String>();
            List<Field> fleids = new ArrayList<Field>();
            for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
                fleids.addAll(Arrays.asList(c.getDeclaredFields()));
            }

            for (Field field : fleids) {
                String value = "";
                field.setAccessible(true);
                try {
                    Object result = field.get(bean);
                    if (result == null)
                        continue;
                    value = result.toString();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                //            MLogUtil.e("field.getName() "+field.getName());
                //            MLogUtil.e("value "+value);
                returnMap.put(field.getName(), value);
                field.setAccessible(false);
            }
            return returnMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}