List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:org.grails.orm.hibernate.cfg.GrailsIdentifierGeneratorFactory.java
public static void applyNewInstance(Configuration cfg) throws IllegalArgumentException, IllegalAccessException { Field field = ReflectionUtils.findField(Configuration.class, "identifierGeneratorFactory"); field.setAccessible(true); field.set(cfg, new GrailsIdentifierGeneratorFactory()); }
From source file:com.microsoft.applicationinsights.internal.channel.common.ApacheSenderFactoryTest.java
private static ApacheSender createApacheSender(boolean isNewVersion) throws NoSuchFieldException, IllegalAccessException { Field field = ClassDataUtils.class.getDeclaredField("verifier"); field.setAccessible(true); ClassDataVerifier mockVerifier = Mockito.mock(ClassDataVerifier.class); Mockito.doReturn(isNewVersion).when(mockVerifier).verifyClassExists(anyString()); field.set(ClassDataUtils.INSTANCE, mockVerifier); ApacheSender sender = new ApacheSenderFactory().create(); assertNotNull(sender);/*from w w w. j a v a2 s . co m*/ return sender; }
From source file:Main.java
public static void quitLoop() { try {/* w ww. j a v a2s. co m*/ Field mQuitAllowed = MessageQueue.class.getDeclaredField("mQuitAllowed"); mQuitAllowed.setAccessible(true); mQuitAllowed.set(Looper.myQueue(), true); Looper.myLooper().quit(); Field mQuiting = MessageQueue.class.getDeclaredField("mQuiting"); mQuiting.setAccessible(true); mQuiting.set(Looper.myQueue(), false); mQuitAllowed.set(Looper.myQueue(), false); } catch (Exception e) { throw new IllegalStateException( "Sofia modal runnables are not " + "supported on this version of the Android API because the " + "MessageQueue.mQuitAllowed field could not be found or " + "there was a problem changing it."); } }
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 w w w .j av a 2 s.c om * @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
public static boolean overrideClassLoader(ClassLoader cl, File dex, File opt) { try {/* w w w . ja va 2 s .co m*/ ClassLoader bootstrap = cl.getParent(); Field fPathList = BaseDexClassLoader.class.getDeclaredField("pathList"); fPathList.setAccessible(true); Object pathList = fPathList.get(cl); Class cDexPathList = bootstrap.loadClass("dalvik.system.DexPathList"); Field fDexElements = cDexPathList.getDeclaredField("dexElements"); fDexElements.setAccessible(true); Object dexElements = fDexElements.get(pathList); DexClassLoader cl2 = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, bootstrap); Object pathList2 = fPathList.get(cl2); Object dexElements2 = fDexElements.get(pathList2); Object element2 = Array.get(dexElements2, 0); int n = Array.getLength(dexElements) + 1; Object newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), n); Array.set(newDexElements, 0, element2); for (int i = 0; i < n - 1; i++) { Object element = Array.get(dexElements, i); Array.set(newDexElements, i + 1, element); } fDexElements.set(pathList, newDexElements); return true; } catch (Exception e) { Log.e("lcast", "fail to override classloader " + cl + " with " + dex, e); return false; } }
From source file:Main.java
public static int getMoveType(PackageInfo pInfo, ApplicationInfo info) { int moveType = MOVEAPPTYPE_NONE; if ((FLAG_EXTERNAL_STORAGE & info.flags) != 0) { moveType = MOVEAPPTYPE_MOVETOPHONE; } else if (pInfo != null) { int installLocation = 1; try {/*from w w w . ja va 2 s.com*/ Field field = pInfo.getClass().getDeclaredField("installLocation"); field.setAccessible(true); installLocation = field.getInt(pInfo); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (installLocation == 0) { moveType = MOVEAPPTYPE_MOVETOSDCARD; } else if (installLocation == -1 || installLocation == 1) { moveType = MOVEAPPTYPE_NONE; } } return moveType; }
From source file:Main.java
public static void setField(Object object, String fieldName, Object fieldValue) { Class<?> objectClass = object.getClass(); if (objectClass != null) { try {// w w w . j a v a 2 s . c o m Field field = objectClass.getDeclaredField(fieldName); field.setAccessible(true); Type type = field.getGenericType(); //This is bad, but dear God I can't figure out how to convert a runtime Type to its actual type through reflection. I know how in C#.... if (type.toString().toLowerCase().contains("short")) { fieldValue = Short.parseShort((String) fieldValue); } else if (type.toString().toLowerCase().contains("integer")) { fieldValue = Integer.parseInt((String) fieldValue); } else if (type.toString().toLowerCase().contains("long")) { fieldValue = Long.parseLong((String) fieldValue); } else if (type.toString().toLowerCase().contains("boolean")) { fieldValue = "1".equals(fieldValue); //Java, you're the worst language. And SQLite isn't helping. } field.set(object, fieldValue); } catch (NoSuchFieldException e) { return; } catch (Exception e) { throw new IllegalStateException(e); } } }
From source file:Main.java
public static Object invokeField(Class<?> cls, Object object, String fieldName) throws Exception { Field field = cls.getDeclaredField(fieldName); boolean accessible = field.isAccessible(); try {/*w ww . j a v a2 s . co m*/ field.setAccessible(true); return field.get(object); } finally { field.setAccessible(accessible); } }
From source file:Main.java
@SuppressWarnings("rawtypes") public static Object getFieldObject(Object target, Class clazz, String fieldName) { try {// w w w . ja va2s .c o m Field field = clazz.getDeclaredField(fieldName); if (!field.isAccessible()) { field.setAccessible(true); } return field.get(target); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static int getMoveType(PackageInfo pInfo, ApplicationInfo info) { int moveType = MOVEAPPTYPE_NONE; if ((FLAG_EXTERNAL_STORAGE & info.flags) != 0) { moveType = MOVEAPPTYPE_MOVETOPHONE; } else if (pInfo != null) { int installLocation = 1; try {// ww w.j ava 2s .co m Field field = pInfo.getClass().getDeclaredField("installLocation"); field.setAccessible(true); installLocation = field.getInt(pInfo); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (installLocation == 0) { moveType = MOVEAPPTYPE_MOVETOSDCARD; } else if (installLocation == -1 || installLocation == 1) { moveType = MOVEAPPTYPE_NONE; } } System.out.println("moveType..............." + moveType); return moveType; }