List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:Main.java
public static Dialog waitForCurrentDialogShowing(final Activity activity, final long timeout) throws InterruptedException { long start = System.currentTimeMillis(); try {// ww w . ja v a 2s . co m Field currentDialogField = activity.getClass().getDeclaredField("currentDialog"); currentDialogField.setAccessible(true); Dialog dialog = null; while (dialog == null || !dialog.isShowing()) { dialog = (Dialog) currentDialogField.get(activity); Thread.sleep(50); long now = System.currentTimeMillis(); if (now - start > timeout) { throw new InterruptedException( String.format("Timeout exceeded while waiting for dialog showing")); } } return dialog; } catch (Exception e) { throw new InterruptedException(String.format("Exception while waiting for dialog showing")); } }
From source file:Main.java
/** * Locates a given field anywhere in the class inheritance hierarchy. * * @param instance an object to search the field into. * @param name field name//from w w w .jav a2 s .c om * @return a field object * @throws NoSuchFieldException if the field cannot be located */ static Field findField(Object instance, String name) throws NoSuchFieldException { for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { try { Field field = clazz.getDeclaredField(name); if (!field.isAccessible()) { field.setAccessible(true); } return field; } catch (NoSuchFieldException e) { // ignore and search next } } throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass()); }
From source file:cn.edu.zjnu.acm.judge.SystemOutCharset.java
private static Object get(Field field, Object o) throws Exception { if (!field.isAccessible()) { field.setAccessible(true); }/*from w w w .ja v a 2 s . c o m*/ return field.get(o); }
From source file:Main.java
/** * Enable/Disable mobile data.//from ww w.j a va2s . co m * @param context * @param enabled */ public static void setMobileDataEnabled(Context context, boolean enabled) { try { final ConnectivityManager conman = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); final Class conmanClass = Class.forName(conman.getClass().getName()); final Field connectivityManagerField = conmanClass.getDeclaredField("mService"); connectivityManagerField.setAccessible(true); final Object connectivityManager = connectivityManagerField.get(conman); final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName()); final Method setMobileDataEnabledMethod = connectivityManagerClass .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); // Method call for enabling mobile data.. setMobileDataEnabledMethod.invoke(connectivityManager, enabled); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
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. j a v a 2 s. c o 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
private static void registerFieldSetter(final Class<?> iClass, String fieldName, Field f) { // TRY TO GET THE VALUE BY ACCESSING DIRECTLY TO THE PROPERTY if (!f.isAccessible()) f.setAccessible(true); setters.put(iClass.getName() + "." + fieldName, f); }
From source file:com.oembedler.moon.graphql.engine.dfs.SchemaHelper.java
public static void replaceTypeReferencesForUnionType(final GraphQLSchema schema, final Set<GraphQLUnionType> graphQLUnionTypeMap) { Field graphQLTypesField = ReflectionUtils.findField(GraphQLUnionType.class, "types"); graphQLTypesField.setAccessible(true); for (GraphQLUnionType graphQLUnionType : graphQLUnionTypeMap) { List<GraphQLType> graphQLTypes = new ArrayList<>(); for (GraphQLType graphQLType : graphQLUnionType.getTypes()) { if (graphQLType instanceof GraphQLTypeReference) { graphQLTypes.add(schema.getType(graphQLType.getName())); } else { graphQLTypes.add(graphQLType); }//from www. j ava 2s.co m } ReflectionUtils.setField(graphQLTypesField, graphQLUnionType, graphQLTypes); } }
From source file:Main.java
private static int getImageViewFieldValue(Object object, String fieldName) { int value = 0; try {//from w w w . j a v a2 s .c om Field field = ImageView.class.getDeclaredField(fieldName); field.setAccessible(true); int fieldValue = (Integer) field.get(object); if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) { value = fieldValue; } } catch (Exception e) { Log.e("", e.getMessage()); } return value; }
From source file:springobjectmapper.ReflectionHelper.java
public static Field getField(Class<?> c, String name) { do {/*from w ww . j a v a2 s .c o m*/ try { Field field = c.getDeclaredField(name); field.setAccessible(true); return field; } catch (NoSuchFieldException ex) { } catch (Exception e) { ReflectionUtils.handleReflectionException(e); } c = c.getSuperclass(); } while (c != null); throw new RuntimeException("Field " + name + " was not found!"); }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.KITKAT) private static boolean setProxyLollipop(final Context context, String host, int port) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", port + ""); try {//w w w.j a va 2s . c o m Context appContext = context.getApplicationContext(); Class applictionClass = Class.forName("android.app.Application"); Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk"); mLoadedApkField.setAccessible(true); Object mloadedApk = mLoadedApkField.get(appContext); Class loadedApkClass = Class.forName("android.app.LoadedApk"); Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers"); mReceiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk); for (Object receiverMap : receivers.values()) { for (Object receiver : ((ArrayMap) receiverMap).keySet()) { Class clazz = receiver.getClass(); if (clazz.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); onReceiveMethod.invoke(receiver, appContext, intent); } } } } catch (Exception e) { e.printStackTrace(); return false; } return true; }