List of usage examples for java.lang.reflect Field setInt
@CallerSensitive @ForceInline public void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException
From source file:com.xxxifan.devbox.core.util.ViewUtils.java
/** * set status bar icon to light theme, which is called dark mode. * should be called in onCreate()//from w w w .j a va2 s .c o m */ public static void setStatusBarLightMode(Activity activity, boolean lightMode) { if (activity == null || activity.getWindow() == null) { return; } Window window = activity.getWindow(); boolean changed = false; // try miui try { Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); int darkIcon = field.getInt(layoutParams); Method extraFlagField = window.getClass().getMethod("setExtraFlags", int.class, int.class); extraFlagField.invoke(window, lightMode ? darkIcon : 0, darkIcon); changed = true; } catch (Exception ignored) { } // try flyme try { WindowManager.LayoutParams lp = window.getAttributes(); Field darkIcon = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); darkIcon.setAccessible(true); meizuFlags.setAccessible(true); int bit = darkIcon.getInt(null); int value = meizuFlags.getInt(lp); if (lightMode) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); window.setAttributes(lp); changed = true; } catch (Exception ignored) { } if (!changed && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { int visibility = window.getDecorView().getSystemUiVisibility(); if (lightMode) { visibility |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; } else { visibility &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; } window.getDecorView().setSystemUiVisibility(visibility); } }
From source file:com.cardvlaue.sys.util.ScreenUtil.java
/** * ???Flyme4.0 ???Flyme/*from w ww . j av a 2 s.com*/ * * @param window ?? * @param dark ???? * @return boolean ?true */ 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) { // e.printStackTrace(); } } return result; }
From source file:com.vk.sdk.api.model.ParseUtils.java
/** * Parses object with follow rules://from w w w .ja v a 2s .c o m * * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link String}, * arrays of primitive types, {@link String}s and {@link com.vk.sdk.api.model.VKApiModel}s, * list implementation line {@link com.vk.sdk.api.model.VKList}, {@link com.vk.sdk.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdk.api.model.VKPhotoSizes}, * {@link com.vk.sdk.api.model.VKApiModel}s. * * 4. Boolean fields defines by vk_int == 1 expression. * @param object object to initialize * @param source data to read values * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Parcelable.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?????????? ???????: // ?? ?? ????????, ?? ? ????????? ???????? getFields() ???????? ??? ???. // ?????? ? ??????? ???????????, ????????? ?? ? ????????, ?????? Android ? ???????? ????????? ??????????. throw new JSONException(e.getMessage()); } } return object; }
From source file:org.flycraft.vkontakteapi.model.ParseUtils.java
/** * Parses object with follow rules:/*from w ww . j a v a 2 s . c o m*/ * <p/> * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link java.lang.String}, * arrays of primitive types, {@link java.lang.String}s and {@link org.flycraft.vkontakteapi.model.VKApiModel}s, * list implementation line {@link org.flycraft.vkontakteapi.model.VKList}, {@link org.flycraft.vkontakteapi.model.VKAttachments.VKApiAttachment} or {@link org.flycraft.vkontakteapi.model.VKPhotoSizes}, * {@link org.flycraft.vkontakteapi.model.VKApiModel}s. * <p/> * 4. Boolean fields defines by vk_int == 1 expression. * * @param object object to initialize * @param source data to read values * @param <T> type of result * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?: // , getFields() . // ? ? ?, ? ?, Android ? . throw new JSONException(e.getMessage()); } } return object; }
From source file:com.vk.sdkweb.api.model.ParseUtils.java
/** * Parses object with follow rules:/*from w w w . j a v a 2 s. co m*/ * * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link java.lang.String}, * arrays of primitive types, {@link java.lang.String}s and {@link com.vk.sdkweb.api.model.VKApiModel}s, * list implementation line {@link com.vk.sdkweb.api.model.VKList}, {@link com.vk.sdkweb.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdkweb.api.model.VKPhotoSizes}, * {@link com.vk.sdkweb.api.model.VKApiModel}s. * * 4. Boolean fields defines by vk_int == 1 expression. * * @param object object to initialize * @param source data to read values * @param <T> type of result * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings("rawtypes") public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Parcelable.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?: // , getFields() . // ? ? ?, ? ?, Android ? . throw new JSONException(e.getMessage()); } } return object; }
From source file:android.reflect.ClazzLoader.java
/** * ????//w w w .j a va2 s. co m */ public static <O, V> void setFieldValue(O o, Field field, V v) { if (field != null) { try { field.setAccessible(true); if (v == null) { field.set(o, null); } else { Class<?> vType = v.getClass(); if (vType == Integer.TYPE) { field.setInt(o, (Integer) v); } else if (vType == Long.TYPE) { field.setLong(o, (Long) v); } else if (vType == Boolean.TYPE) { field.setBoolean(o, (Boolean) v); } else if (vType == Float.TYPE) { field.setFloat(o, (Float) v); } else if (vType == Short.TYPE) { field.setShort(o, (Short) v); } else if (vType == Byte.TYPE) { field.setByte(o, (Byte) v); } else if (vType == Double.TYPE) { field.setDouble(o, (Double) v); } else if (vType == Character.TYPE) { field.setChar(o, (Character) v); } else { field.set(o, v); } } } catch (Throwable t) { Log.e(TAG, t); } } }
From source file:jef.tools.security.EncrypterUtil.java
/** * ???/*w w w .j av a 2s. com*/ * @return */ public static boolean removeCryptographyRestrictions() { if (!isRestrictedCryptography()) { return false; } try { /* * Do the following, but with reflection to bypass access checks: * * JceSecurity.isRestricted = false; * JceSecurity.defaultPolicy.perms.clear(); * JceSecurity.defaultPolicy.add(CryptoAllPermission.INSTANCE); */ final Class<?> jceSecurity = Class.forName("javax.crypto.JceSecurity"); final Class<?> cryptoPermissions = Class.forName("javax.crypto.CryptoPermissions"); final Class<?> cryptoAllPermission = Class.forName("javax.crypto.CryptoAllPermission"); final Field isRestrictedField = jceSecurity.getDeclaredField("isRestricted"); isRestrictedField.setAccessible(true); final Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL); isRestrictedField.set(null, false); final Field defaultPolicyField = jceSecurity.getDeclaredField("defaultPolicy"); defaultPolicyField.setAccessible(true); final PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null); final Field perms = cryptoPermissions.getDeclaredField("perms"); perms.setAccessible(true); ((Map<?, ?>) perms.get(defaultPolicy)).clear(); final Field instance = cryptoAllPermission.getDeclaredField("INSTANCE"); instance.setAccessible(true); defaultPolicy.add((Permission) instance.get(null)); return true; } catch (final Exception e) { LogUtil.error("Failed to remove cryptography restrictions", e); return false; } }
From source file:org.apache.openjpa.enhance.Reflection.java
/** * Set the value of the given field in the given object. *///from ww w . java 2 s . c om public static void set(Object target, Field field, int value) { if (target == null || field == null) return; makeAccessible(field, field.getModifiers()); try { field.setInt(target, value); } catch (Throwable t) { throw wrapReflectionException(t, _loc.get("set-field", new Object[] { target, field, value, "int" })); } }
From source file:org.acoveo.tools.Reflection.java
/** * Set the value of the given field in the given object. *//*from ww w.j ava 2 s. c o m*/ public static void set(Object target, Field field, int value) { if (target == null || field == null) return; makeAccessible(field, field.getModifiers()); try { field.setInt(target, value); } catch (Throwable t) { throw wrapReflectionException(t); } }
From source file:org.opendaylight.sxp.route.core.RoutingServiceFactoryTest.java
private void changeOsIsLinuxField(final boolean mockedOsName) throws NoSuchFieldException, IllegalAccessException { final Field osNameField = SystemUtils.class.getDeclaredField("IS_OS_LINUX"); osNameField.setAccessible(true);//w w w .j a va 2 s .c om Field modifiers = osNameField.getClass().getDeclaredField("modifiers"); modifiers.setAccessible(true); modifiers.setInt(osNameField, osNameField.getModifiers() & ~Modifier.FINAL); osNameField.set(null, mockedOsName); }