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:Main.java

/**
 * Using reflection to override default typeface
 * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
 * @param context to work with assets/*from ww  w .ja  v  a 2s  . co  m*/
 * @param defaultFontNameToOverride for example "monospace"
 * @param customFontFileNameInAssets file name of the font from assets
 */
public static void overrideFont(Context context, String defaultFontNameToOverride,
        String customFontFileNameInAssets) {
    try {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(),
                customFontFileNameInAssets);

        final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
        defaultFontTypefaceField.setAccessible(true);
        defaultFontTypefaceField.set(null, customFontTypeface);
    } catch (Exception e) {
    }
}

From source file:Main.java

private static void injectClassField(Class clazz, String fieldStr, long newValue) {
    try {/*  ww w.ja  va2 s .com*/
        final Field field = clazz.getDeclaredField(fieldStr);
        if (field != null) {
            field.setAccessible(true);
            Object obj = null;
            field.set(obj, newValue);
        }
    } catch (Exception e) {
        Log.d("REFLECTION", clazz.getSimpleName() + " injection failed for field: " + fieldStr);
    }
}

From source file:Main.java

public static Service createService(Context context, ServiceInfo serviceInfo) throws Exception {
    IBinder token = new Binder();

    Class<?> createServiceDataClass = Class.forName("android.app.ActivityThread$CreateServiceData");
    Constructor<?> constructor = createServiceDataClass.getDeclaredConstructor();
    constructor.setAccessible(true);/*from ww w.j  av a2s .  co  m*/
    Object createServiceData = constructor.newInstance();

    Field tokenField = createServiceDataClass.getDeclaredField("token");
    tokenField.setAccessible(true);
    tokenField.set(createServiceData, token);

    serviceInfo.applicationInfo.packageName = context.getPackageName();
    Field infoField = createServiceDataClass.getDeclaredField("info");
    infoField.setAccessible(true);
    infoField.set(createServiceData, serviceInfo);

    Class<?> compatibilityClass = Class.forName("android.content.res.CompatibilityInfo");
    Field defaultCompatibilityField = compatibilityClass.getDeclaredField("DEFAULT_COMPATIBILITY_INFO");
    defaultCompatibilityField.setAccessible(true);
    Object defaultCompatibility = defaultCompatibilityField.get(null);
    defaultCompatibilityField.set(createServiceData, defaultCompatibility);

    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Method currentActivityThreadMethod = activityThreadClass.getDeclaredMethod("currentActivityThread");
    Object currentActivityThread = currentActivityThreadMethod.invoke(null);

    Method handleCreateServiceMethod = activityThreadClass.getDeclaredMethod("handleCreateService",
            createServiceDataClass);
    handleCreateServiceMethod.setAccessible(true);
    handleCreateServiceMethod.invoke(currentActivityThread, createServiceData);

    Field mServicesField = activityThreadClass.getDeclaredField("mServices");
    mServicesField.setAccessible(true);
    Map mServices = (Map) mServicesField.get(currentActivityThread);
    Service service = (Service) mServices.get(token);
    mServices.remove(token);

    return service;
}

From source file:Main.java

/**
 * Set the fields' value./* w w w .j  a  v  a2 s  . c o m*/
 * 
 * @param bean
 * @param valMap
 */
public static void setFieldValues(Object bean, Map<String, Object> valMap) {
    Class<?> cls = bean.getClass();
    //Get all fields.
    Field[] fields = cls.getDeclaredFields();

    for (Field field : fields) {
        if (valMap.containsKey(field.getName())) {
            field.setAccessible(true);
            try {
                field.set(bean, valMap.get(field.getName()));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        field.setAccessible(true);/*from  w  w  w  .  j av  a 2 s  .c o  m*/
        try {
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }

}

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        field.setAccessible(true);/*ww  w .  j a va  2  s.co m*/
        try {
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }
}

From source file:io.apiman.manager.api.migrator.DataMigratorTest.java

/**
 * @param version//from   ww  w. j a va2  s . c  o  m
 * @param versionString
 * @throws Exception
 */
private static void setVersion(Version version, String versionString) throws Exception {
    Field field = version.getClass().getDeclaredField("versionString");
    field.setAccessible(true);
    field.set(version, versionString);

    field = version.getClass().getDeclaredField("versionDate");
    field.setAccessible(true);
    field.set(version, new Date().toString());
}

From source file:Main.java

public static void keep_setLong(Field field, Object obj, Cursor cursor, int i) {
    try {//from   ww  w  . ja  v  a  2  s. co  m
        if (field.getType().equals(Long.TYPE))
            field.setLong(obj, cursor.getLong(i));
        else
            field.set(obj, Long.valueOf(cursor.getLong(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

public static void keep_setInt(Field field, Object obj, Cursor cursor, int i) {
    try {//from  www .  j  a  v a2  s .c  om
        if (field.getType().equals(Integer.TYPE))
            field.setInt(obj, cursor.getInt(i));
        else
            field.set(obj, Integer.valueOf(cursor.getInt(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

public static void keep_setFloat(Field field, Object obj, Cursor cursor, int i) {
    try {//from  www.j a v  a 2 s .c o m
        if (field.getType().equals(Float.TYPE))
            field.setFloat(obj, cursor.getFloat(i));
        else
            field.set(obj, Float.valueOf(cursor.getFloat(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}