Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

import java.util.Map;

public class Main {
    /**
     * Set the fields' value.
     * 
     * @param bean
     * @param valMap
     */
    public static void setFieldValues(Object bean, Map<String, Object> valMap) {
        Class<?> cls = bean.getClass();
        //Get all fields.
        Field[] fields = cls.getDeclaredFields();

        for (Field field : fields) {
            if (valMap.containsKey(field.getName())) {
                field.setAccessible(true);
                try {
                    field.set(bean, valMap.get(field.getName()));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}