Java Method Call invokeSet(Object o, String fieldName, Object value)

Here you can find the source of invokeSet(Object o, String fieldName, Object value)

Description

invoke Set

License

Open Source License

Declaration

public static void invokeSet(Object o, String fieldName, Object value) 

Method Source Code

//package com.java2s;

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

public class Main {

    public static void invokeSet(Object o, String fieldName, Object value) {
        Method method = getSetMethod(o.getClass(), fieldName);
        try {/*from  ww w.  j  ava2  s .c  o m*/
            method.invoke(o, new Object[] { value });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Method getSetMethod(Class objectClass, String fieldName) {
        try {
            Class[] parameterTypes = new Class[1];
            Field field = objectClass.getDeclaredField(fieldName);
            parameterTypes[0] = field.getType();
            StringBuffer sb = new StringBuffer();
            sb.append("set");
            sb.append(fieldName.substring(0, 1).toUpperCase());
            sb.append(fieldName.substring(1));
            Method method = objectClass.getMethod(sb.toString(),
                    parameterTypes);
            return method;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}

Related

  1. invokeReadResolveOn(Object object, Class clz)
  2. invokeReal(Object real, Method m, Object[] args)
  3. invokeRemoteMBeanOperation(String remoteURL, String jmxName, Class klass, Function function)
  4. invokeRestrictedMethod(Object obj, Class theClass, String methodName)
  5. invokeServiceClass(JsonReader jsonReader, Object service, Method operation, Class[] paramClasses, int paramCount)
  6. invokeSetFieldValue(Object bean, Field field, Object value)
  7. invokeSilent(Object obj, String methodName, Class[] parameterTypes, Object[] args)
  8. invokeSimple(Object target, String name)
  9. invokeTarget(Method method, Object obj, Object... args)