Example usage for java.lang.reflect Field setInt

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the value of a field as an int on the specified object.

Usage

From source file:net.sf.keystore_explorer.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Hack to disable crypto restrictions until Java 9 is out.
 *
 * See http://stackoverflow.com/a/22492582/2672392
 *///from   w  w  w  .j a v a2 s .  c  om
public static void removeRestrictions() {
    try {
        Class<?> jceSecurityClass = Class.forName("javax.crypto.JceSecurity");
        Class<?> cryptoPermissionsClass = Class.forName("javax.crypto.CryptoPermissions");
        Class<?> cryptoAllPermissionClass = Class.forName("javax.crypto.CryptoAllPermission");

        Field isRestrictedField = jceSecurityClass.getDeclaredField("isRestricted");
        isRestrictedField.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL);
        isRestrictedField.set(null, false);

        Field defaultPolicyField = jceSecurityClass.getDeclaredField("defaultPolicy");
        defaultPolicyField.setAccessible(true);
        PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null);

        Field permsField = cryptoPermissionsClass.getDeclaredField("perms");
        permsField.setAccessible(true);
        ((Map<?, ?>) permsField.get(defaultPolicy)).clear();

        Field cryptoAllPermissionInstanceField = cryptoAllPermissionClass.getDeclaredField("INSTANCE");
        cryptoAllPermissionInstanceField.setAccessible(true);
        defaultPolicy.add((Permission) cryptoAllPermissionInstanceField.get(null));
    } catch (Exception e) {
        // ignore
    }
}

From source file:Main.java

public static BitmapFactory.Options getBitmapOptions(DisplayMetrics mDisplayMetrics) {
    try {/*  w w  w.  ja v a  2s  . c  om*/
        // TODO I think this can all be done without reflection now because all these properties are SDK 4
        final Field density = DisplayMetrics.class.getDeclaredField("DENSITY_DEFAULT");
        final Field inDensity = BitmapFactory.Options.class.getDeclaredField("inDensity");
        final Field inTargetDensity = BitmapFactory.Options.class.getDeclaredField("inTargetDensity");
        final Field targetDensity = DisplayMetrics.class.getDeclaredField("densityDpi");
        final BitmapFactory.Options options = new BitmapFactory.Options();
        inDensity.setInt(options, density.getInt(null));
        inTargetDensity.setInt(options, targetDensity.getInt(mDisplayMetrics));
        return options;
    } catch (final IllegalAccessException ex) {
        Log.d(TAG, "Couldn't access fields.", ex);
    } catch (final NoSuchFieldException ex) {
        Log.d(TAG, "Couldn't find fields.", ex);
    }
    return null;
}

From source file:Main.java

public static NetworkInfo createNetworkInfo(final int type, final boolean connected) throws Exception {
    Constructor<NetworkInfo> ctor = NetworkInfo.class.getDeclaredConstructor(int.class);
    ctor.setAccessible(true);//w w w  . j av a 2  s . c  o m
    NetworkInfo networkInfo = ctor.newInstance(0);
    Field typeField = NetworkInfo.class.getDeclaredField("mNetworkType");
    Field connectedField = NetworkInfo.class.getDeclaredField("mState");
    Field detailedStateField = NetworkInfo.class.getDeclaredField("mDetailedState");
    typeField.setAccessible(true);
    connectedField.setAccessible(true);
    detailedStateField.setAccessible(true);
    typeField.setInt(networkInfo, type);
    connectedField.set(networkInfo,
            connected == true ? NetworkInfo.State.CONNECTED : NetworkInfo.State.DISCONNECTED);
    detailedStateField.set(networkInfo,
            connected == true ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED);
    return networkInfo;
}

From source file:org.jboss.pnc.causeway.ctl.ImportControllerTest.java

static void setFinalField(Object obj, Field field, Object newValue) throws ReflectiveOperationException {
    field.setAccessible(true);//ww w. jav  a2 s  .  co m

    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

    field.set(obj, newValue);
}

From source file:com.microsoft.tfs.client.common.ui.framework.helper.SWTUtil.java

public static int addGridLayoutVerticalIndent(final Control[] controls, final int verticalIndent) {
    Check.notNull(controls, "controls"); //$NON-NLS-1$

    if (SWT.getVersion() >= 3100) {
        for (int i = 0; i < controls.length; i++) {
            GridData gridData = (GridData) controls[i].getLayoutData();
            if (gridData == null) {
                gridData = new GridData();
                controls[i].setLayoutData(gridData);
            }/*  w w  w .j a  v a2 s  .  c  o  m*/

            try {
                final Class gridDataClass = gridData.getClass();
                final Field viField = gridDataClass.getField("verticalIndent"); //$NON-NLS-1$

                viField.setInt(gridData, verticalIndent);
            } catch (final Exception e) {
                break;
            }
        }
        return 0;
    }

    final GridLayout layout = (GridLayout) controls[0].getParent().getLayout();
    final int spacing = Math.max(0, verticalIndent - layout.verticalSpacing);
    final Control spacer = createGridLayoutSpacer(controls[0].getParent(), SWT.DEFAULT, spacing,
            layout.numColumns, 1);
    spacer.moveAbove(controls[0]);
    return 1;
}

From source file:Main.java

public static boolean setStatusBarDarkIcon(Window window, boolean dark) {
    boolean result = false;
    if (window != null) {
        try {/*w  ww  .j a v  a2s.co m*/
            WindowManager.LayoutParams lp = window.getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class
                    .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (dark) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            window.setAttributes(lp);
            result = true;
        } catch (Exception ignored) {
        }
    }
    return result;
}

From source file:Main.java

/**
 * meizu Flyme set status bar light mode
 *///from  w w w. java 2 s. co  m
public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
    boolean result = false;
    if (window != null) {
        try {
            WindowManager.LayoutParams lp = window.getAttributes();
            Field darkFlag = WindowManager.LayoutParams.class
                    .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            int bit = darkFlag.getInt(null);
            int value = meizuFlags.getInt(lp);
            if (dark) {
                value |= bit;
            } else {
                value &= ~bit;
            }
            meizuFlags.setInt(lp, value);
            window.setAttributes(lp);
            result = true;
        } catch (Exception e) {

        }
    }
    return result;
}

From source file:net.minecraftforge.common.util.EnumHelper.java

public static void setFailsafeFieldValue(Field field, @Nullable Object target, @Nullable Object value)
        throws Exception {
    field.setAccessible(true);/* w  w w  . j  a v  a  2  s  .c o m*/
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    Object fieldAccessor = newFieldAccessor.invoke(reflectionFactory, field, false);
    fieldAccessorSet.invoke(fieldAccessor, target, value);
}

From source file:org.sonar.api.batch.rule.Checks.java

private static void configureField(Object check, Field field, String value) {
    try {/*from  w ww.ja  v a  2s  .co  m*/
        field.setAccessible(true);

        if (field.getType().equals(String.class)) {
            field.set(check, value);

        } else if (int.class == field.getType()) {
            field.setInt(check, Integer.parseInt(value));

        } else if (short.class == field.getType()) {
            field.setShort(check, Short.parseShort(value));

        } else if (long.class == field.getType()) {
            field.setLong(check, Long.parseLong(value));

        } else if (double.class == field.getType()) {
            field.setDouble(check, Double.parseDouble(value));

        } else if (boolean.class == field.getType()) {
            field.setBoolean(check, Boolean.parseBoolean(value));

        } else if (byte.class == field.getType()) {
            field.setByte(check, Byte.parseByte(value));

        } else if (Integer.class == field.getType()) {
            field.set(check, Integer.parseInt(value));

        } else if (Long.class == field.getType()) {
            field.set(check, Long.parseLong(value));

        } else if (Double.class == field.getType()) {
            field.set(check, Double.parseDouble(value));

        } else if (Boolean.class == field.getType()) {
            field.set(check, Boolean.parseBoolean(value));

        } else {
            throw new SonarException(
                    "The type of the field " + field + " is not supported: " + field.getType());
        }
    } catch (IllegalAccessException e) {
        throw new SonarException(
                "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(),
                e);
    }
}

From source file:org.apache.hadoop.hbase.fs.HFileSystem.java

/**
 * Add an interceptor on the calls to the namenode#getBlockLocations from the DFSClient
 * linked to this FileSystem. See HBASE-6435 for the background.
 * <p/>/*  w w w . ja va2s.co m*/
 * There should be no reason, except testing, to create a specific ReorderBlocks.
 *
 * @return true if the interceptor was added, false otherwise.
 */
static boolean addLocationsOrderInterceptor(Configuration conf, final ReorderBlocks lrb) {
    if (!conf.getBoolean("hbase.filesystem.reorder.blocks", true)) { // activated by default
        LOG.debug("addLocationsOrderInterceptor configured to false");
        return false;
    }

    FileSystem fs;
    try {
        fs = FileSystem.get(conf);
    } catch (IOException e) {
        LOG.warn("Can't get the file system from the conf.", e);
        return false;
    }

    if (!(fs instanceof DistributedFileSystem)) {
        LOG.debug("The file system is not a DistributedFileSystem. " + "Skipping on block location reordering");
        return false;
    }

    DistributedFileSystem dfs = (DistributedFileSystem) fs;
    DFSClient dfsc = dfs.getClient();
    if (dfsc == null) {
        LOG.warn("The DistributedFileSystem does not contain a DFSClient. Can't add the location "
                + "block reordering interceptor. Continuing, but this is unexpected.");
        return false;
    }

    try {
        Field nf = DFSClient.class.getDeclaredField("namenode");
        nf.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(nf, nf.getModifiers() & ~Modifier.FINAL);

        ClientProtocol namenode = (ClientProtocol) nf.get(dfsc);
        if (namenode == null) {
            LOG.warn("The DFSClient is not linked to a namenode. Can't add the location block"
                    + " reordering interceptor. Continuing, but this is unexpected.");
            return false;
        }

        ClientProtocol cp1 = createReorderingProxy(namenode, lrb, conf);
        nf.set(dfsc, cp1);
        LOG.info("Added intercepting call to namenode#getBlockLocations so can do block reordering"
                + " using class " + lrb.getClass());
    } catch (NoSuchFieldException e) {
        LOG.warn("Can't modify the DFSClient#namenode field to add the location reorder.", e);
        return false;
    } catch (IllegalAccessException e) {
        LOG.warn("Can't modify the DFSClient#namenode field to add the location reorder.", e);
        return false;
    }

    return true;
}