Example usage for java.lang.reflect Field set

List of usage examples for java.lang.reflect Field set

Introduction

In this page you can find the example usage for java.lang.reflect Field set.

Prototype

@CallerSensitive
@ForceInline 
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the field represented by this Field object on the specified object argument to the specified new value.

Usage

From source file:io.github.benas.randombeans.util.ReflectionUtils.java

/**
 * Set a value (accessible or not accessible) in a field of a target object.
 *
 * @param object instance to set the property on
 * @param field  field to set the property on
 * @param value  value to set/* ww  w  .j  a v  a 2  s . c  o  m*/
 * @throws IllegalAccessException if the property cannot be set
 */
public static void setProperty(final Object object, final Field field, final Object value)
        throws IllegalAccessException {
    boolean access = field.isAccessible();
    field.setAccessible(true);
    field.set(object, value);
    field.setAccessible(access);
}

From source file:com.aptana.editor.css.validator.CSSValidator.java

/**
 * Loads our CSS profile./*from   w w  w  . j a v  a 2 s  .c  o  m*/
 * 
 * @throws IOException
 *             if profile loading fails
 */
private static void loadAptanaCSSProfile() {
    InputStream configStream = CSSValidator.class.getResourceAsStream(CONFIG_FILE);
    InputStream profilesStream = CSSValidator.class.getResourceAsStream(PROFILES_CONFIG_FILE);

    try {
        // loads our config
        PropertiesLoader.config.load(configStream);

        // loads our profile
        Utf8Properties profiles = new Utf8Properties();
        profiles.load(profilesStream);
        // a hack, but no other way since PropertiesLoader provides no public access to stored profiles
        Field field = PropertiesLoader.class.getDeclaredField("profiles"); //$NON-NLS-1$
        field.setAccessible(true);
        field.set(null, profiles);
    } catch (Exception e) {
        IdeLog.logError(CSSPlugin.getDefault(), Messages.CSSValidator_ERR_FailToLoadProfile, e);
    } finally {
        try {
            configStream.close();
        } catch (IOException e) {
        }
        try {
            profilesStream.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.abssh.util.ReflectionUtils.java

/**
 * , private/protected, ??setter.//from w w  w . j a v  a2  s. co m
 */
public static void setFieldValue(final Object object, final String fieldName, final Object value) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null) {
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");
    }

    makeAccessible(field);

    try {
        field.set(object, value);
    } catch (IllegalAccessException e) {
        logger.error("??:{}", e.getMessage());
    }
}

From source file:com.aptana.css.core.internal.build.CSSValidator.java

/**
 * Loads our CSS profile./*w w w . j  a  va 2  s.  co  m*/
 * 
 * @throws IOException
 *             if profile loading fails
 */
private static void loadAptanaCSSProfile() {
    InputStream configStream = CSSValidator.class.getResourceAsStream(CONFIG_FILE);
    InputStream profilesStream = CSSValidator.class.getResourceAsStream(PROFILES_CONFIG_FILE);

    try {
        // loads our config
        PropertiesLoader.config.load(configStream);

        // loads our profile
        Utf8Properties profiles = new Utf8Properties();
        profiles.load(profilesStream);
        // a hack, but no other way since PropertiesLoader provides no public access to stored profiles
        Field field = PropertiesLoader.class.getDeclaredField("profiles"); //$NON-NLS-1$
        field.setAccessible(true);
        field.set(null, profiles);
    } catch (Exception e) {
        IdeLog.logError(CSSCorePlugin.getDefault(), Messages.CSSValidator_ERR_FailToLoadProfile, e);
    } finally {
        try {
            configStream.close();
        } catch (IOException e) {
        }
        try {
            profilesStream.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.project.framework.util.ReflectionUtils.java

/**
 * , private/protected, ??setter./*from w  w  w . j  a v a  2s.  c o m*/
 */
public static void setFieldValue(final Object object, final String fieldName, final Object value) {

    Field field = getDeclaredField(object, fieldName);

    Assert.notNull(field, "Could not find field [" + fieldName + "] on target [" + object + "]");

    makeAccessible(field);

    try {
        field.set(object, value);
    } catch (IllegalAccessException e) {
        logger.error("??:{}", e.getMessage());
    }
}

From source file:net.umpay.mailbill.util.reflect.ReflectionUtils.java

/**
 * , private/protected, ??setter.//w  w  w  .  j  av  a 2s  .  c  o  m
 */
public static void setFieldValue(final Object object, final String fieldName, final Object value) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null) {
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");
    }
    makeAccessible(field);
    try {
        field.set(object, value);
    } catch (IllegalAccessException e) {
        logger.error("??:{}", e);
    }
}

From source file:com.rapid.develop.core.utils.ReflectionUtils.java

/**
 * , private/protected, ??setter.//from  w  w w  .  ja v  a2  s  .  c  om
 */
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
    Field field = getAccessibleField(obj, fieldName);

    if (field == null) {
        throw new IllegalArgumentException("Could not find fields [" + fieldName + "] on target [" + obj + "]");
    }

    try {
        field.set(obj, value);
    } catch (IllegalAccessException e) {
        logger.error("??:{}", e.getMessage());
    }
}

From source file:com.smf.platform.util.ReflectionUtils.java

/**
 * , private/protected, ??setter./* ww w .j  av  a2s .com*/
 */
public static void setFieldValue(final Object object, final String fieldName, final Object value) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null) {
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");
    }

    makeAccessible(field);

    try {
        field.set(object, value);
    } catch (IllegalAccessException e) {
        //         logger.error("??:{}", e.getMessage());
    }
}

From source file:com.austin.base.commons.util.ReflectUtil.java

/**
 * @param object//from w  ww.  jav  a2s.c  o m
 * @param propertyName
 * @param newValue
 * @throws NoSuchFieldException
 */
public static void forceSetProperty(Object object, String propertyName, Object newValue)
        throws NoSuchFieldException {
    Assert.notNull(object);
    Assert.hasText(propertyName);

    Field field = getDeclaredField(object, propertyName);
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    try {
        field.set(object, newValue);
    } catch (IllegalAccessException e) {
        log.info("Error won't happen");
    }
    field.setAccessible(accessible);
}

From source file:org.comicwiki.Repository.java

private static Object mergeObjects(Object source, Object target) {
    Field[] fields = source.getClass().getFields();
    for (Field field : fields) {
        field.setAccessible(true);//from ww  w .  j  a  va2 s .c  o  m
        try {
            Object sourceValue = field.get(source);
            if (sourceValue == null) {
                continue;
            }
            Object targetValue = field.get(target);
            if (targetValue == null) {
                field.set(target, sourceValue);
            } else if (targetValue instanceof Thing) {// CreativeWork
                mergeObjects(sourceValue, targetValue);
            } else if (targetValue instanceof IRI[]) {
                IRI[] tV = (IRI[]) targetValue;
                IRI[] sV = (IRI[]) sourceValue;
                field.set(target, Add.both(tV, sV, IRI.class));
            } else if (targetValue instanceof String[]) {
                String[] tV = (String[]) targetValue;
                String[] sV = (String[]) sourceValue;
                field.set(target, Add.both(sV, tV, String.class));
            } else if (targetValue instanceof Number[]) {
                Number[] tV = (Number[]) targetValue;
                Number[] sV = (Number[]) sourceValue;
                field.set(target, Add.both(tV, sV, Number.class));
            } else if (targetValue instanceof URL[]) {
                URL[] tV = (URL[]) targetValue;
                URL[] sV = (URL[]) sourceValue;
                field.set(target, Add.both(tV, sV, URL.class));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return target;
}