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.vivekpanyam.evolve.Utils.java

/**
 * Set the ClassLoader for the running application
 *
 * @param a An activity in the currently running application
 * @param classLoader The ClassLoader used to load the new DEX file
 *//*ww w . j a va 2 s  .co m*/

public static void setClassLoader(Activity a, ClassLoader classLoader) {
    try {
        Field mMainThread = getField(Activity.class, "mMainThread");
        Object mainThread = mMainThread.get(a);
        Class<?> threadClass = mainThread.getClass();
        Field mPackages = getField(threadClass, "mPackages");

        HashMap<String, ?> map = (HashMap<String, ?>) mPackages.get(mainThread);
        WeakReference<?> ref = (WeakReference<?>) map.get(a.getPackageName());
        Object apk = ref.get();
        Class<?> apkClass = apk.getClass();
        Field mClassLoader = getField(apkClass, "mClassLoader");

        mClassLoader.set(apk, classLoader);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.apache.hyracks.http.test.HttpServerTest.java

public static void setPrivateField(Object obj, String filedName, Object value) throws Exception {
    Field f = obj.getClass().getDeclaredField(filedName);
    f.setAccessible(true);//  w  w  w . j  a va  2s .co m
    f.set(obj, value);
}

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteEsInputFormat.java

private static void enableInputSplitDebugMode(List<InputSplit> list) {
    for (InputSplit is : list) {
        try {//w  w  w  .  j a  v  a2s  .c  o m
            Class<?> c = is.getClass();

            Field nodeIp = c.getDeclaredField("nodeIp");
            nodeIp.setAccessible(true);
            nodeIp.set(is, "127.0.0.1");
        } catch (Exception e) {
            //DEBUG
            //e.printStackTrace();
        }
    }
}

From source file:com.userhook.model.UHMessageMetaButton.java

public static UHMessageMetaButton fromJSON(JSONObject json) {

    UHMessageMetaButton button = new UHMessageMetaButton();

    try {/*from   w ww  .  j a va 2  s  .  c  om*/

        String[] fields = { "title", "click", "uri", "survey", "survey_title", "payload" };
        for (String field : fields) {
            if (json.has(field)) {
                Field f = UHMessageMetaButton.class.getDeclaredField(field);
                f.set(button, json.getString(field));
            }
        }

        if (json.has("image")) {
            UHMessageMetaImage image = UHMessageMetaImage.fromJSON(json.getJSONObject("image"));
            button.image = image;
        }

    } catch (Exception e) {
        Log.e(UserHook.TAG, "error parsing message meta button json", e);
    }

    return button;
}

From source file:com.seleniumtests.GenericTest.java

/**
 * Generate a ITestResult from scratch/*  w  w w.ja va 2s . co m*/
 * @param testNGCtx
 * @return
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static ITestResult generateResult(final ITestContext testNGCtx, final Class<?> clazz)
        throws NoSuchMethodException, SecurityException, NoSuchFieldException, IllegalArgumentException,
        IllegalAccessException {
    ITestResult testResult = new TestResult();
    testResult.setParameters(new String[] { "foo", "bar" });

    XmlSuite suite = new XmlSuite();
    suite.setName("TmpSuite");
    XmlTest test = new XmlTest(suite);
    test.setName("myTestNg");

    ITestNGMethod testMethod = new TestNGMethod(clazz.getMethod("myTest"),
            new JDK15AnnotationFinder(new DefaultAnnotationTransformer()), test, null);
    Field methodField = TestResult.class.getDeclaredField("m_method");
    methodField.setAccessible(true);
    methodField.set(testResult, testMethod);
    Field contextField = TestResult.class.getDeclaredField("m_context");
    contextField.setAccessible(true);
    contextField.set(testResult, testNGCtx);

    return testResult;
}

From source file:me.j360.dubbo.modules.util.reflect.ReflectionUtil.java

/**
 * ?Field, ?, ??setter./*w w  w  .j  ava  2  s.c  o  m*/
 */
public static void setField(final Object obj, Field field, final Object value) {
    try {
        field.set(obj, value);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.feilong.core.lang.reflect.FieldUtilTemp.java

/**
 * ./*w  w w  .j a v  a  2  s.co m*/
 * 
 * <p>
 *  <code>owner</code> null, {@link NullPointerException}<br>
 *  <code>fieldName</code> null, {@link NullPointerException}<br>
 *  <code>fieldName</code> blank, {@link IllegalArgumentException}<br>
 * </p>
 *
 * @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) {
    Validate.notNull(owner, "owner can't be null!");
    Validate.notBlank(fieldName, "fieldName can't be blank!");
    try {
        Class<?> ownerClass = owner.getClass();
        Field field = ownerClass.getField(fieldName);
        field.set(ownerClass, value);
    } catch (Exception e) {
        throw new ReflectException(e);
    }
}

From source file:com.thoughtworks.go.config.MagicalGoConfigXmlLoader.java

public static void setMd5(CruiseConfig configForEdit, String md5)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = BasicCruiseConfig.class.getDeclaredField("md5");
    field.setAccessible(true);/* ww  w.j a  v a2  s.  c o m*/
    field.set(configForEdit, md5);
}

From source file:com.relicum.titleapi.Reflection.ReflectionUtil.java

public static void setPrivateField(Class<EntityArmorStand> clazz, Object handle, String fieldName, Object value)
        throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);/*from   ww w .  j av  a 2s  . co  m*/
    field.set(handle, value);
}

From source file:com.infinira.aerospike.dataaccess.util.Utils.java

public static boolean set(Object object, String fieldName, Object fieldValue) {
    Assert.notNull(object, "Object cannot be null.");
    Assert.notNull(fieldName, "Fieldname cannot be null.");
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {/*from  w ww  . jav  a  2  s.c  o  m*/
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(object, fieldValue);
            return true;
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return false;
}