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

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

Description

set Field

License

Open Source License

Declaration

public static void setField(Object obj, String fieldName, Object value) 

Method Source Code


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

import java.lang.reflect.*;

public class Main {
    public static void setField(Object obj, String fieldName, Object value) {
        try {//w w w  .ja v a  2  s  . c om
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.set(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static void setField(Object obj, String fieldName, double value) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.setDouble(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static void setField(Object obj, String fieldName, int value) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.setInt(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static void setField(Object obj, String fieldName, long value) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.setLong(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static void setField(Object obj, String fieldName, short value) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.setShort(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static void setField(Object obj, String fieldName, byte value) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.setByte(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static void setField(Object obj, String fieldName, char value) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.setChar(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static void setField(Object obj, String fieldName, boolean value) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            f.setBoolean(obj, value);
        } catch (Exception e) {
        }
        ;
    }

    public static Object getField(Object obj, String fieldName) {
        try {
            Class c = obj.getClass();
            Field f = c.getField(fieldName);
            return f.get(obj);
        } catch (Exception e) {
        }
        ;
        return null;
    }

    public static final Object GetField(Object obj, String field) {
        Field fld = null;
        try {
            Class c = obj.getClass();
            fld = c.getField(field);
        } catch (Exception e) {
            System.err.println(e);
            e.printStackTrace();
        }
        if (fld != null) {
            try {
                return fld.get(obj);
            } catch (Exception e) {
                System.err.println(e);
                e.printStackTrace();
            }
        } else {
            System.err.println("ERROR: NO FIELD " + field + " FOUND IN CLASS " + obj.getClass().getName());
        }
        return null;
    }
}

Related

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