Java Reflection Field Set setField(java.lang.Object toObj, String tcFieldName, java.lang.Object toNewValue)

Here you can find the source of setField(java.lang.Object toObj, String tcFieldName, java.lang.Object toNewValue)

Description

set Field

License

Open Source License

Declaration

public static void setField(java.lang.Object toObj, String tcFieldName, java.lang.Object toNewValue) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

public class Main {
    public static void setField(java.lang.Object toObj, String tcFieldName, java.lang.Object toNewValue) {
        try {/*from  w w  w.j  av a2  s  .c o  m*/
            Field loField = toObj.getClass().getDeclaredField(tcFieldName);
            loField.setAccessible(true);
            loField.set(toObj, toNewValue);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchFieldException ex) {
            throw new RuntimeException(ex);
        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        }
    }
}

Related

  1. setField(final Object obj, final String fieldName, final Object value)
  2. setField(final Object obj, final String name, final Object value)
  3. setField(final Object obj, final String variableName, final Object variableValue)
  4. setField(final Object object, final String name, final Object value)
  5. setField(final Object parent, final String name, final Object obj)
  6. setField(Object instance, String fieldName, Object value)
  7. setField(Object instance, String name, Object obj)
  8. setField(Object instance, String sField, Object value)
  9. setField(Object item, Field f, Object value)