Java Reflection Field Set setField(Object sourceObj, Object targetObj, Field valueField, List targetFields)

Here you can find the source of setField(Object sourceObj, Object targetObj, Field valueField, List targetFields)

Description

set Field

License

Open Source License

Declaration

private static void setField(Object sourceObj, Object targetObj,
        Field valueField, List<Field> targetFields) 

Method Source Code

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

import java.lang.reflect.Field;
import java.util.*;

public class Main {

    private static void setField(Object sourceObj, Object targetObj,
            Field valueField, List<Field> targetFields) {
        valueField.setAccessible(true);/*from   w  w w  .j  av  a  2s  .c om*/

        for (Field target : targetFields) {
            if (target.getName().equals(valueField.getName())) {
                target.setAccessible(true);

                //System.out.println("setting field -> dto:" + valueField.getName() + ", entity:" + target.getName());

                try {
                    target.set(targetObj, valueField.get(sourceObj));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}

Related

  1. setField(Object object, String name, Object value)
  2. setField(Object owner, String name, Object newValue, Class definedIn)
  3. setField(Object owner, String targetClass, String fieldName, Object value)
  4. setField(Object service, String name, Object obj)
  5. setField(Object source, String fieldName, Object value)
  6. setField(Object target, Field field, Object value)
  7. setField(Object target, Field field, Object value)
  8. setField(Object target, String fieldName, Class fieldType, T value)
  9. setField(Object target, String fieldname, Object value)