Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Method;

import java.util.Map;

public class Main {
    public static void setProperties(Object object, Map<String, ? extends Object> properties,
            boolean includeSuperClasses) {
        if (object == null || properties == null) {
            return;
        }

        Class<?> objectClass = object.getClass();

        for (Map.Entry<String, ? extends Object> entry : properties.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key != null && key.length() > 0) {
                String setterName = "set" + Character.toUpperCase(key.charAt(0)) + key.substring(1);
                Method setter = null;

                // Try to use the exact setter
                if (value != null) {
                    try {
                        if (includeSuperClasses) {
                            setter = objectClass.getMethod(setterName, value.getClass());
                        } else {
                            setter = objectClass.getDeclaredMethod(setterName, value.getClass());
                        }
                    } catch (Exception ex) {
                    }
                }

                // Find a more generic setter
                if (setter == null) {
                    Method[] methods = includeSuperClasses ? objectClass.getMethods()
                            : objectClass.getDeclaredMethods();
                    for (Method method : methods) {
                        if (method.getName().equals(setterName)) {
                            Class<?>[] parameterTypes = method.getParameterTypes();
                            if (parameterTypes.length == 1 && isAssignableFrom(parameterTypes[0], value)) {
                                setter = method;
                                break;
                            }
                        }
                    }
                }

                // Invoke
                if (setter != null) {
                    try {
                        setter.invoke(object, value);
                    } catch (Exception e) {
                    }
                }
            }
        }
    }

    private static boolean isAssignableFrom(Class<?> parameterType, Object value) {
        if (parameterType.isPrimitive()) {
            if (value == null) {
                return false;
            }
            Class<?> valueClass = value.getClass();

            if (parameterType == Boolean.TYPE) {
                return valueClass == Boolean.class;
            } else if (parameterType == Byte.TYPE) {
                return valueClass == Byte.class;
            } else if (parameterType == Character.TYPE) {
                return valueClass == Character.class;
            } else if (parameterType == Short.TYPE) {
                return valueClass == Short.class || valueClass == Byte.class;
            } else if (parameterType == Integer.TYPE) {
                return valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                        || valueClass == Byte.class;
            } else if (parameterType == Long.TYPE) {
                return valueClass == Long.class || valueClass == Integer.class || valueClass == Character.class
                        || valueClass == Short.class || valueClass == Byte.class;
            } else if (parameterType == Float.TYPE) {
                return valueClass == Float.class || valueClass == Long.class || valueClass == Integer.class
                        || valueClass == Character.class || valueClass == Short.class || valueClass == Byte.class;
            } else if (parameterType == Double.TYPE) {
                return valueClass == Double.class || valueClass == Float.class || valueClass == Long.class
                        || valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                        || valueClass == Byte.class;
            } else {
                return false;
            }
        } else {
            return value == null || parameterType.isAssignableFrom(value.getClass());
        }
    }
}