Example usage for java.lang.reflect Field setBoolean

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void setBoolean(Object obj, boolean z) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the value of a field as a boolean on the specified object.

Usage

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getBoolean(x));

    f.setBoolean(x, false);
    System.out.println(f.getBoolean(x));

}

From source file:Main.java

public static void main(String... args) {
    Main ft = new Main();
    try {//www.j ava2s .c om
        Class<?> c = ft.getClass();
        Field f = c.getDeclaredField("b");
        f.setAccessible(true); // solution
        f.setBoolean(ft, Boolean.FALSE); // IllegalAccessException

        // production code should handle these exceptions more gracefully
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    } catch (IllegalArgumentException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:FieldTroubleToo.java

public static void main(String... args) {
    FieldTroubleToo ft = new FieldTroubleToo();
    try {//from w  w  w  .  j av a2s .c o  m
        Class<?> c = ft.getClass();
        Field f = c.getDeclaredField("b");
        // f.setAccessible(true); // solution
        f.setBoolean(ft, Boolean.FALSE); // IllegalAccessException

        // production code should handle these exceptions more gracefully
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    } catch (IllegalArgumentException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

/**
 * Force overflow menu in samsung devices
 *
 * @param context The activity context./*from  ww w  .ja v a  2 s . co m*/
 */
public static void forceOverFlowMenu(Context context) {
    try {
        ViewConfiguration config = ViewConfiguration.get(context);
        Field menuKeyFields = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");

        if (menuKeyFields != null) {
            menuKeyFields.setAccessible(true);
            menuKeyFields.setBoolean(config, false);
        }

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

From source file:Main.java

/**
 * Forces the appearance of a menu in the given context.
 * @param ctx//from w  w w.  j a v a  2  s  . co  m
 */
public static void showMenu(Context ctx) {
    try {
        ViewConfiguration config = ViewConfiguration.get(ctx);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");

        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
    }
}

From source file:api_proto3.TestElf.java

public static void setConfigUnitTest(boolean unitTest) {
    try {//from   www.java2 s.c  o  m
        Field field = HikariConfig.class.getDeclaredField("unitTest");
        field.setAccessible(true);
        field.setBoolean(null, unitTest);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static BitmapFactory.Options getBitmapOptions(Context context) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = true;//from  w  w  w  .j  av a2  s . c om
    options.inPreferredConfig = DEFAULT_BITMAP_CONFIG;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[DECODE_BUFFER_SIZE];
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
        Field field = null;
        try {
            field = BitmapFactory.Options.class.getDeclaredField("inNativeAlloc");
            field.setAccessible(true);
            field.setBoolean(options, true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    int displayDensityDpi = context.getResources().getDisplayMetrics().densityDpi;
    float displayDensity = context.getResources().getDisplayMetrics().density;
    int density = 0;
    if (displayDensityDpi > DENSITY_240 && displayDensityDpi <= DENSITY_400 && displayDensity > 1.5f) {
        density = (int) (displayDensityDpi * SCALE_FACTOR);
    } else if (displayDensityDpi > DENSITY_400 && displayDensity > 2.5f) {
        density = (int) (displayDensityDpi * SCALE_FACTOR_2);
    }
    if (density == 0)
        return options;
    options.inDensity = density;
    options.inTargetDensity = density;
    return options;
}

From source file:com.frostwire.android.gui.UniversalScanner.java

private static Uri nativeScanFile(Context context, String path) {
    try {//from  w w  w. j  a  v  a 2 s . c o  m
        File f = new File(path);

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

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

        try {
            Method setLocaleM = clazz.getDeclaredMethod("setLocale", String.class);
            setLocaleM.invoke(scanner, Locale.US.toString());
        } catch (Throwable e) {
            e.printStackTrace();
        }

        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
        }

        try {
            Field mFileCacheF = clazz.getDeclaredField("mNoMediaPaths");
            mFileCacheF.setAccessible(true);
            mFileCacheF.set(scanner, new HashMap<String, String>());
        } catch (Throwable e) {
            e.printStackTrace();
        }

        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:com.bt.download.android.gui.UniversalScanner.java

private static Uri nativeScanFile(Context context, String path) {
    try {//w  ww.  ja va  2s  .c om
        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:com.browseengine.bobo.serialize.JSONSerializer.java

private static void loadObject(Object retObj, Field f, JSONObject jsonObj) throws JSONSerializationException {
    String key = f.getName();// w ww  .jav  a2s .co  m
    Class type = f.getType();
    try {
        if (type.isPrimitive()) {
            if (type == Integer.TYPE) {
                f.setInt(retObj, jsonObj.getInt(key));
            } else if (type == Long.TYPE) {
                f.setLong(retObj, jsonObj.getInt(key));
            } else if (type == Short.TYPE) {
                f.setShort(retObj, (short) jsonObj.getInt(key));
            } else if (type == Boolean.TYPE) {
                f.setBoolean(retObj, jsonObj.getBoolean(key));
            } else if (type == Double.TYPE) {
                f.setDouble(retObj, jsonObj.getDouble(key));
            } else if (type == Float.TYPE) {
                f.setFloat(retObj, (float) jsonObj.getDouble(key));
            } else if (type == Character.TYPE) {
                char ch = jsonObj.getString(key).charAt(0);
                f.setChar(retObj, ch);
            } else if (type == Byte.TYPE) {
                f.setByte(retObj, (byte) jsonObj.getInt(key));
            } else {
                throw new JSONSerializationException("Unknown primitive: " + type);
            }
        } else if (type == String.class) {
            f.set(retObj, jsonObj.getString(key));
        } else if (JSONSerializable.class.isAssignableFrom(type)) {
            JSONObject jObj = jsonObj.getJSONObject(key);
            JSONSerializable serObj = deSerialize(type, jObj);
            f.set(retObj, serObj);
        }
    } catch (Exception e) {
        throw new JSONSerializationException(e.getMessage(), e);
    }
}