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.HashMap;
import java.util.Map;

public class Main {
    static Map<String, Object> objectToMap(Object object) {
        Map<String, Object> map = new HashMap<>();

        Class cls = object.getClass();
        while (cls != null) {
            for (Field field : cls.getDeclaredFields()) {
                field.setAccessible(true);

                Object value = null;
                try {
                    value = field.get(object);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

                if (value != null)
                    map.put(field.getName(), value);
            }

            cls = cls.getSuperclass();
        }

        return map;
    }
}