Java Reflection Field Set setField(Object obj, String fieldName, Object value, Class valueClass)

Here you can find the source of setField(Object obj, String fieldName, Object value, Class valueClass)

Description

set Field

License

Open Source License

Declaration

public final static void setField(Object obj, String fieldName, Object value, Class valueClass)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException 

Method Source Code


//package com.java2s;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class Main {
    public final static void setField(Object obj, String fieldName, Object value, Class valueClass)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (obj == null || fieldName == null || fieldName.length() < 1) {
            return;
        }// w  w w . j  av  a2 s  .  c o  m

        String methodName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
        Method m = obj.getClass().getMethod(methodName, new Class[] { valueClass });
        m.invoke(obj, new Object[] { value });
    }
}

Related

  1. setField(Object obj, String fieldName, Object fieldValue)
  2. setField(Object obj, String fieldName, Object val)
  3. setField(Object obj, String fieldname, Object value)
  4. setField(Object obj, String fieldName, Object value)
  5. setField(Object obj, String fieldName, Object value)
  6. setField(Object obj, String name, Object value)
  7. setField(Object obj, String name, Object value)
  8. setField(Object object, Class clazz, String fieldName, Object value)
  9. setField(Object object, Class clazz, String fieldName, Object value)