List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:Main.java
public static Activity getCurrentTopActivity() throws ClassNotFoundException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException { Class<?> activityThreadClass = Class.forName("android.app.ActivityThread"); Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null); Field activitiesField = activityThreadClass.getDeclaredField("mActivities"); activitiesField.setAccessible(true); Map<?, ?> activities = (Map<?, ?>) activitiesField.get(activityThread); for (Object activityRecord : activities.values()) { Class<?> activityRecordClass = activityRecord.getClass(); Field pausedField = activityRecordClass.getDeclaredField("paused"); pausedField.setAccessible(true); if (!pausedField.getBoolean(activityRecord)) { Field activityField = activityRecordClass.getDeclaredField("activity"); activityField.setAccessible(true); Activity activity = (Activity) activityField.get(activityRecord); return activity; }//from w ww. j av a 2 s. c o m } return null; }
From source file:Main.java
private static void setField(Object paramObject1, Class<?> paramClass, String paramString, Object paramObject2) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException { Field localField = paramClass.getDeclaredField(paramString); localField.setAccessible(true);/*from ww w .ja v a 2s.c o m*/ localField.set(paramObject1, paramObject2); }
From source file:com.koda.integ.hbase.util.CacheableSerializer.java
@SuppressWarnings("unchecked") private static Field getProtectedField(Class klass, String fieldName) { Field field;/* ww w . j a v a 2 s. co m*/ try { field = klass.getDeclaredField(fieldName); field.setAccessible(true); } catch (Exception e) { throw new AssertionError(e); } return field; }
From source file:io.logspace.it.InfrastructureRule.java
private static Field getField(Class<?> type, String fieldName) { Class<?> currentType = type; while (currentType != null) { try {/* w ww. ja v a 2 s .co m*/ return currentType.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { // nothing to do } catch (SecurityException e) { e.printStackTrace(); } currentType = currentType.getSuperclass(); } return null; }
From source file:Main.java
public static void dumphreadLocals() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException { Thread thread = Thread.currentThread(); Field threadLocalField = thread.getClass().getDeclaredField("threadLocals"); threadLocalField.setAccessible(true); Object threadLocalTable = threadLocalField.get(thread); Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); //Class threadLocalMapClass = Class.forName(threadLocalField.getType().getName()); Field tableField = threadLocalMapClass.getDeclaredField("table"); tableField.setAccessible(true);/*from ww w . jav a2 s . co m*/ Object[] table = (Object[]) tableField.get(threadLocalTable); Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry"); Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value"); entryValueField.setAccessible(true); //Field referenceField = Reference.class.getDeclaredField("referent") ; //referenceField.setAccessible(true); for (Object entry : table) { if (entry != null) { Object threadLocalValue = entryValueField.get(entry); printObject(threadLocalValue); //ThreadLocal threadLocal = (ThreadLocal)referenceField.get(entry); //System.out.println("thread local value "+threadLocal); } } }
From source file:Main.java
public static void dumphreadLocals() { try {/* w w w . ja va 2 s .c o m*/ // Get a reference to the thread locals table of the current thread Thread thread = Thread.currentThread(); Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); Object threadLocalTable = threadLocalsField.get(thread); // Get a reference to the array holding the thread local variables // inside the // ThreadLocalMap of the current thread @SuppressWarnings("rawtypes") Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); Field tableField = threadLocalMapClass.getDeclaredField("table"); tableField.setAccessible(true); Object[] table = (Object[]) tableField.get(threadLocalTable); @SuppressWarnings("rawtypes") Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry"); Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value"); entryValueField.setAccessible(true); // The key to the ThreadLocalMap is a WeakReference object. The // referent field of this object // is a reference to the actual ThreadLocal variable Field referentField = Reference.class.getDeclaredField("referent"); referentField.setAccessible(true); for (Object entry : table) { // Each entry in the table array of ThreadLocalMap is an Entry // object // representing the thread local reference and its value if (entry != null) { Object tlcValue = entryValueField.get(entry); ThreadLocal threadLocal = (ThreadLocal) referentField.get(entry); // System.out.println("thread local value "+tlcValue); // if(threadLocal) printObject(threadLocal, tlcValue); } } System.out.println("__________________________"); } catch (Exception e) { // We will tolerate an exception here and just log it throw new IllegalStateException(e); } }
From source file:com.github.piasy.biv.example.App.java
static void fixLeakCanary696(Context context) { if (!isEmui()) { Log.w(TAG, "not emui"); return;// w w w.ja v a 2 s . c o m } try { Class clazz = Class.forName("android.gestureboost.GestureBoostManager"); Log.w(TAG, "clazz " + clazz); Field _sGestureBoostManager = clazz.getDeclaredField("sGestureBoostManager"); _sGestureBoostManager.setAccessible(true); Field _mContext = clazz.getDeclaredField("mContext"); _mContext.setAccessible(true); Object sGestureBoostManager = _sGestureBoostManager.get(null); if (sGestureBoostManager != null) { _mContext.set(sGestureBoostManager, context); } } catch (Exception ignored) { ignored.printStackTrace(); } }
From source file:Main.java
public static String getResourceString(String paramString, Activity paramActivity) { Class localClass = null; if (0 == 0)/*from ww w.ja v a 2 s .c o m*/ ; try { localClass = Class.forName(paramActivity.getPackageName() + ".R$string"); String str = paramActivity.getResources() .getString(((Integer) localClass.getDeclaredField(paramString).get(null)).intValue()); return str; } catch (Exception localException) { Log.e(paramActivity.getPackageName(), localException.getMessage(), localException); paramActivity.finish(); } return ""; }
From source file:com.searchbox.core.ref.ReflectionUtils.java
public static Field findUnderlying(Class<?> element, String fieldName) { if (element != null) { Field field = null;/*from w w w. j a va2s . co m*/ try { field = element.getDeclaredField(fieldName); } catch (Exception e) { } if (field != null) { return field; } else { return findUnderlying(element.getSuperclass(), fieldName); } } else { return null; } }
From source file:com.infinira.aerospike.dataaccess.util.Utils.java
public static boolean set(Object object, String fieldName, Object fieldValue) { Assert.notNull(object, "Object cannot be null."); Assert.notNull(fieldName, "Fieldname cannot be null."); Class<?> clazz = object.getClass(); while (clazz != null) { try {//w ww. j ava 2s . c om Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, fieldValue); return true; } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); } catch (Exception e) { throw new IllegalStateException(e); } } return false; }