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:com.sunchenbin.store.feilong.core.lang.reflect.FieldUtil.java

/**
 * .//from w  w w.  ja  v  a 2  s. c om
 *
 * @param owner
 *            the owner
 * @param fieldName
 *            
 * @param value
 *            
 * @see java.lang.Object#getClass()
 * @see java.lang.Class#getField(String)
 * @see java.lang.reflect.Field#set(Object, Object)
 * 
 * @see org.apache.commons.lang3.reflect.FieldUtils#writeField(Field, Object, Object, boolean)
 * @since 1.4.0
 */
public static void setFieldValue(Object owner, String fieldName, Object value) {
    try {
        Class<?> ownerClass = owner.getClass();
        Field field = ownerClass.getField(fieldName);
        field.set(ownerClass, value);
    } catch (Exception e) {
        LOGGER.error(e.getClass().getName(), e);
        throw new ReflectException(e);
    }
}

From source file:com.github.hibatis.ReflectionUtils.java

/**
 * , private/protected, ??setter./* w  w  w . j av 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 field [" + fieldName + "] on target [" + obj + "]");
    }

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

From source file:me.bird.util.Reflections.java

/**
 * , private/protected, ??setter.//  w w w .jav a2s .co m
 */
public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
    //      Field field = FieldUtils.getDeclaredField(obj.getClass(), fieldName, true);
    Field field = FieldUtils.getField(obj.getClass(), fieldName, true);

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

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

From source file:com.bt.download.android.gui.UniversalScanner.java

private static Uri nativeScanFile(Context context, String path) {
    try {/*www  .  j  a v  a2s.com*/
        File f = new File(path);

        Class<?> clazz = Class.forName("android.media.MediaScanner");

        Constructor<?> mediaScannerC = clazz.getDeclaredConstructor(Context.class);
        Object scanner = mediaScannerC.newInstance(context);

        Field mClientF = clazz.getDeclaredField("mClient");
        mClientF.setAccessible(true);
        Object mClient = mClientF.get(scanner);

        Method scanSingleFileM = clazz.getDeclaredMethod("scanSingleFile", String.class, String.class,
                String.class);
        Uri fileUri = (Uri) scanSingleFileM.invoke(scanner, f.getAbsolutePath(), "external", "data/raw");
        int n = context.getContentResolver().delete(fileUri, null, null);
        if (n > 0) {
            LOG.debug("Deleted from Files provider: " + fileUri);
        }

        Field mNoMediaF = mClient.getClass().getDeclaredField("mNoMedia");
        mNoMediaF.setAccessible(true);
        mNoMediaF.setBoolean(mClient, false);

        // This is only for HTC (tested only on HTC One M8)
        try {
            Field mFileCacheF = clazz.getDeclaredField("mFileCache");
            mFileCacheF.setAccessible(true);
            mFileCacheF.set(scanner, new HashMap<String, Object>());
        } catch (Throwable e) {
            // no an HTC, I need some time to refactor this hack
        }

        Method doScanFileM = mClient.getClass().getDeclaredMethod("doScanFile", String.class, String.class,
                long.class, long.class, boolean.class, boolean.class, boolean.class);
        Uri mediaUri = (Uri) doScanFileM.invoke(mClient, f.getAbsolutePath(), null, f.lastModified(),
                f.length(), false, true, false);

        Method releaseM = clazz.getDeclaredMethod("release");
        releaseM.invoke(scanner);

        return mediaUri;

    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}

From source file:lt.emasina.resthub.server.factory.MetadataFactory.java

public static void injectPrivateField(Object o, Class<?> fieldHolderClass, String fieldName, Object value)
        throws Exception {
    Field fResourceMd = fieldHolderClass.getDeclaredField(fieldName);
    fResourceMd.setAccessible(true);/*from   w ww.jav  a  2  s. c  om*/
    fResourceMd.set(o, value);
}

From source file:gov.abrs.etms.common.util.ReflectionUtils.java

/**
 * , private/protected, ??setter./*from  w  ww . j a va2 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 field [" + fieldName + "] on target [" + obj + "]");
    }

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

From source file:com.yize.broadcast.core.util.ReflectionUtils.java

/**
 * , private/protected, ??setter./*w ww .  j  a v a  2s.c  o  m*/
 */
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 field [" + fieldName + "] on target [" + obj + "]");
    }

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

From source file:lite.flow.runtime.kiss.ComponentUtil.java

public static void injectOutput(String outputName, Output<?> output, Object componentInstance)
        throws IllegalArgumentException, IllegalAccessException {

    Class<?> componentClazz = componentInstance.getClass();

    // find activity all Output type fields
    for (Field field : FieldUtils.getAllFields(componentClazz)) {
        Class<?> decl = field.getType();
        if (Output.class.isAssignableFrom(decl)) {

            String name = field.getName();
            if (name != null && name.equals(outputName)) {
                field.setAccessible(true);
                field.set(componentInstance, output);
                return;
            }//from w  w  w  .j av a2  s .c o m
        }
    }

    throw new IllegalArgumentException(
            format("Class '%s' do not contain output '%s'", componentClazz.getName(), outputName));
}

From source file:ml.shifu.shifu.util.ClassUtils.java

private static void setFieldValue(Field field, Object instance, Object value)
        throws IllegalArgumentException, IllegalAccessException {
    field.setAccessible(true);/*from   w w  w  .  jav  a 2s . c om*/
    field.set(instance, value);
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static void setValueToBean(Object bean, String field, Object value) {
    Class<?> clazz = bean.getClass();
    try {/*from www . j a  va2 s  . c  o  m*/
        Field f = getDeclaredField(clazz, NamingUtil.dbNameToJavaName(field));
        f.setAccessible(true);
        try {
            f.set(bean, TypeConvertUtil.convert(f.getType(), value));
        } catch (IllegalArgumentException e) {
            throw new UnknowException(e);
        } catch (IllegalAccessException e) {
            throw new UnknowException(e);
        }
    } catch (SecurityException e) {
        throw new UnknowException(e);
    } catch (NoSuchFieldException e) {
        throw new JavaFieldNotFoundException(clazz, field);
    }
}