Java Reflection Method Setter Invoke invokeSetter(Object entity, String propertyName, Object propertyValue)

Here you can find the source of invokeSetter(Object entity, String propertyName, Object propertyValue)

Description

Sets a property on an entity based on its name.

License

Open Source License

Parameter

Parameter Description
entity The entity to update.
propertyName The property name.
propertyValue The property value.

Exception

Parameter Description
Exception an exception

Declaration

public static void invokeSetter(Object entity, String propertyName, Object propertyValue) throws Exception 

Method Source Code

//package com.java2s;
/**/* ww w . ja v  a2  s.c  o  m*/
 * Copyright 2005-2014 Restlet
 * 
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
 * select the license that you prefer but you may not use this file except in
 * compliance with one of these Licenses.
 * 
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 * 
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 * 
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 * 
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * http://restlet.com/products/restlet-framework
 * 
 * Restlet is a registered trademark of Restlet S.A.S.
 */

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    /**
     * Sets a property on an entity based on its name.
     * 
     * @param entity
     *            The entity to update.
     * @param propertyName
     *            The property name.
     * @param propertyValue
     *            The property value.
     * @throws Exception
     */
    public static void invokeSetter(Object entity, String propertyName, Object propertyValue) throws Exception {
        if (propertyName != null && entity != null) {
            propertyName = propertyName.replaceAll("/", ".");
            Object o = entity;
            String pty = propertyName;
            String[] strings = propertyName.split("\\.");
            if (strings.length > 1) {
                for (int i = 0; i < strings.length - 1; i++) {
                    String string = strings[i];
                    Object p = invokeGetter(o, string);
                    if (p == null) {
                        // Try to instantiate it
                        Field[] fields = o.getClass().getDeclaredFields();
                        for (Field field : fields) {
                            if (field.getName().equalsIgnoreCase(string)) {
                                p = field.getType().newInstance();
                                break;
                            }
                        }
                    }
                    o = p;
                }
                pty = strings[strings.length - 1];
            }

            String setterName = null;
            char firstLetter = pty.charAt(0);
            if (Character.isLowerCase(firstLetter)) {
                setterName = "set" + Character.toUpperCase(firstLetter) + pty.substring(1);
            } else {
                setterName = "set" + pty;
            }

            Method setter = null;
            Method method;
            for (int i = 0; (setter == null) && (i < o.getClass().getDeclaredMethods().length); i++) {
                method = o.getClass().getDeclaredMethods()[i];

                if (method.getName().equals(setterName)) {
                    if ((method.getParameterTypes() != null) && (method.getParameterTypes().length == 1)) {
                        setter = method;
                    }
                }
            }

            if (setter != null) {
                setter.invoke(o, propertyValue);
            }
        }
    }

    /**
     * Sets a property on an entity based on its name.
     * 
     * @param entity
     *            The entity to update.
     * @param propertyName
     *            The property name.
     * @param propertyValue
     *            The property value.
     * @param propertyType
     *            The property data type.
     * @throws Exception
     */
    public static void invokeSetter(Object entity, String propertyName, String propertyValue, String propertyType)
            throws Exception {

        if (propertyName != null) {
            propertyName = propertyName.replaceAll("/", ".");
            Object o = entity;
            String pty = propertyName;

            String[] strings = propertyName.split("\\.");
            if (strings.length > 1) {
                for (int i = 0; i < strings.length - 1; i++) {
                    String string = strings[i];
                    Object p = invokeGetter(o, string);
                    if (p == null) {
                        // Try to instantiate it
                        Field[] fields = o.getClass().getDeclaredFields();
                        for (Field field : fields) {
                            if (field.getName().equalsIgnoreCase(string)) {
                                p = field.getType().newInstance();
                                break;
                            }
                        }
                    }
                    o = p;
                }
                pty = strings[strings.length - 1];
            }

            String setterName = null;
            char firstLetter = propertyName.charAt(0);
            if (Character.isLowerCase(firstLetter)) {
                setterName = "set" + Character.toUpperCase(firstLetter) + pty.substring(1);
            } else {
                setterName = "set" + pty;
            }

            Method setter = null;
            Object setterParameter = null;
            Method method;
            for (int i = 0; (setter == null) && (i < o.getClass().getDeclaredMethods().length); i++) {
                method = o.getClass().getDeclaredMethods()[i];

                if (method.getName().equals(setterName)) {
                    if ((method.getParameterTypes() != null) && (method.getParameterTypes().length == 1)) {
                        Class<?> parameterType = method.getParameterTypes()[0];

                        if (String.class.equals(parameterType)) {
                            setterParameter = propertyValue;
                            setter = method;
                        } else if (Integer.class.equals(parameterType)) {
                            setterParameter = Integer.valueOf(propertyValue);
                            setter = method;
                        } else if (int.class.equals(parameterType)) {
                            setterParameter = Integer.valueOf(propertyValue);
                            setter = method;
                        }
                    }
                }
            }

            if (setter != null) {
                setter.invoke(o, setterParameter);
            }
        }
    }

    /**
     * Returns the value of a property on an entity based on its name.
     * 
     * @param entity
     *            The entity.
     * @param propertyName
     *            The property name.
     * @return The value of a property for an entity.
     * @throws Exception
     */
    public static Object invokeGetter(Object entity, String propertyName) throws Exception {
        Object result = null;

        if (propertyName != null && entity != null) {
            propertyName = propertyName.replaceAll("/", ".");
            Object o = entity;
            String pty = propertyName;
            int index = propertyName.indexOf(".");
            if (index != -1) {
                o = invokeGetter(entity, propertyName.substring(0, index));
                pty = propertyName.substring(index + 1);

                result = invokeGetter(o, pty);
            } else {
                String getterName = null;
                char firstLetter = propertyName.charAt(0);
                if (Character.isLowerCase(firstLetter)) {
                    getterName = "get" + Character.toUpperCase(firstLetter) + pty.substring(1);
                } else {
                    getterName = "get" + pty;
                }

                Method getter = null;
                Method method;
                for (int i = 0; (getter == null) && (i < entity.getClass().getDeclaredMethods().length); i++) {
                    method = entity.getClass().getDeclaredMethods()[i];

                    if (method.getName().equals(getterName)) {
                        getter = method;
                    }
                }

                if (getter != null) {
                    result = getter.invoke(o);
                }
            }
        }

        return result;
    }
}

Related

  1. invokeSetter(final Method setter, final Object target, Object value)
  2. invokeSetter(Method setter, Object obj, String fieldName, Object value)
  3. invokeSetter(Method setter, Object obj, String value)
  4. invokeSetter(Method setter, Object object, Object propValue)
  5. invokeSetter(Object o, String name, Class value1clazz, Object value)
  6. invokeSetter(Object o, String name, Object value, Class clazz)
  7. invokeSetterMethod(Method method, Object oBean, Object oValue)
  8. invokeSetters(final T instance, final Map vars)