Java Reflection Field Value Set setFieldValue(String name, Object instance, int value, Class cl)

Here you can find the source of setFieldValue(String name, Object instance, int value, Class cl)

Description

set Field Value

License

Apache License

Declaration

public static void setFieldValue(String name, Object instance,
            int value, Class cl) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {
    public static boolean isThrowable = true;

    public static void setFieldValue(String name, Object instance,
            int value, Class cl) {
        try {//from w  w  w. j a v  a  2s  . c om
            getReflectField(name, instance, cl).setInt(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue int error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }

    }

    public static void setFieldValue(String name, Object instance,
            boolean value, Class cl) {
        try {
            getReflectField(name, instance, cl).setBoolean(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue boolean error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    public static void setFieldValue(String name, Object instance,
            byte value, Class cl) {
        try {
            getReflectField(name, instance, cl).setByte(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue byte error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    public static void setFieldValue(String name, Object instance,
            char value, Class cl) {
        try {
            getReflectField(name, instance, cl).setChar(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue char error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    public static void setFieldValue(String name, Object instance,
            double value, Class cl) {
        try {
            getReflectField(name, instance, cl).setDouble(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue double error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    public static void setFieldValue(String name, Object instance,
            float value, Class cl) {
        try {
            getReflectField(name, instance, cl).setFloat(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue float error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    public static void setFieldValue(String name, Object instance,
            long value, Class cl) {
        try {
            getReflectField(name, instance, cl).setLong(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue long error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    public static void setFieldValue(String name, Object instance,
            Object value, Class cl) {
        try {
            getReflectField(name, instance, cl).set(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue Object error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    public static void setFieldValue(String name, Object instance,
            short value, Class cl) {
        try {
            getReflectField(name, instance, cl).setShort(instance, value);
        } catch (Exception e) {
            e.printStackTrace();
            if (isThrowable) {
                throw new RuntimeException("setFieldValue short error "
                        + name + "   target   " + instance + "  value  "
                        + value);
            }
        }
    }

    private static Field getReflectField(String name, Object instance,
            Class cl) throws NoSuchFieldException {
        if (cl == null) {
            if (isThrowable)
                throw new RuntimeException("Field " + name
                        + " declaring class is null ");
            else
                return null;
        }
        Field field;
        if (!cl.isInterface()) {
            try {
                field = cl.getDeclaredField(name);
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                return field;
            } catch (NoSuchFieldException e) {
                // ignore and search next
            }
        } else {
            return cl.getDeclaredField(name);
        }
        throw new NoSuchFieldException("Field " + name + " not found in "
                + instance.getClass());
    }
}

Related

  1. setFieldValue(Object target, String fieldName, Object value)
  2. setFieldValue(Object target, String fieldName, String fieldValue)
  3. setFieldValue(Object target, String fname, Class ftype, Object fvalue)
  4. setFieldValue(String fieldName, int fieldValue, Object board)
  5. setFieldValue(String inField, Object inObject, Object inValue)
  6. setFieldValue(String name, String obfuscatedName, Class clazz, Object object, Object value)
  7. setFieldvalue(T instance, E value, Field field)
  8. setFieldValue_internal(Object bean, Field field, Object value)
  9. setFieldValueForObject(Object object, String fieldName, Object value)