List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:Main.java
/** * Helper to get the Activity result Intent. This must only be called on the main thread. * * @param activity Activity whose result Intent is to be obtained. * @return Result Intent of the Activity. *///from w w w .j a v a 2s . c om @Nullable public static Intent getActivityResultData(@NonNull final Activity activity) { if (activity == null) { throw new IllegalArgumentException("activity cannot be null"); //$NON-NLS-1$ } if (Looper.getMainLooper() != Looper.myLooper()) { throw new AssertionError("Must only be called on the main thread"); } /* * This is a hack to obtain the Activity result data. There is no official way to check this using the Android * testing frameworks, so accessing the internals of the Activity object is the only way. This could * break on newer versions of Android. */ try { final Field resultIntentField = Activity.class.getDeclaredField("mResultData"); //$NON-NLS-1$ resultIntentField.setAccessible(true); return ((Intent) resultIntentField.get(activity)); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Field findFieldByClassAndTypeAndName(Class<?> targetObject, Class<?> fieldType, String fieldName) {//from w w w . jav a2 s . c o m Class clazz = targetObject; do { for (Field field : clazz.getDeclaredFields()) { if (field.getType() == fieldType && field.getName().equals(fieldName)) { field.setAccessible(true); return field; } } clazz = clazz.getSuperclass(); } while (clazz != null); throw new NoSuchFieldError("Field of type " + fieldType.getName() + " in class " + targetObject.getName()); }
From source file:Main.java
public static String[][] returnTable(Collection E) { if ((E == null) || E.isEmpty()) { System.out.println("The collection is empty!"); }/* w w w . j a v a2 s .c o m*/ Set<Field> collectionFields = new TreeSet<>(new Comparator<Field>() { @Override public int compare(Field o1, Field o2) { return o1.getName().compareTo(o2.getName()); } }); for (Object o : E) { Class c = o.getClass(); createSetOfFields(collectionFields, c); while (c.getSuperclass() != null) { c = c.getSuperclass(); createSetOfFields(collectionFields, c); } } String[][] exitText = new String[E.size() + 1][collectionFields.size() + 1]; exitText[0][0] = String.format("%20s", "Class"); int indexOfColumn = 0; for (Field f : collectionFields) { exitText[0][indexOfColumn + 1] = String.format("%20s", f.getName()); indexOfColumn++; } int indexOfRow = 0; for (Object o : E) { indexOfColumn = 0; exitText[indexOfRow + 1][0] = String.format("%20s", o.getClass().getSimpleName()); for (Field field : collectionFields) { try { field.setAccessible(true); if (field.get(o) instanceof Date) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20tD", field.get(o)); } else { String temp = String.valueOf(field.get(o)); exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", temp); } } catch (Exception e) { exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", "-"); } indexOfColumn++; } indexOfRow++; } return exitText; }
From source file:Main.java
public static boolean replaceField(String className, String fieldName, Object desObj, Object fieldObj) { Field tempField; try {// w w w.ja va2s . c o m tempField = Class.forName(className).getDeclaredField(fieldName); tempField.setAccessible(true); tempField.set(desObj, fieldObj); return true; } catch (NoSuchFieldException e) { // TODO Auto-generated catch block Log.i("DEX", "" + e.getMessage()); // e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
From source file:Main.java
protected static void makeAccessible(final Field field) { if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { field.setAccessible(true); }/*from w w w .jav a 2 s .com*/ }
From source file:Main.java
/** * Force overflow menu in samsung devices * * @param context The activity context.//from w ww . ja v a 2 s. c om */ public static void forceOverFlowMenu(Context context) { try { ViewConfiguration config = ViewConfiguration.get(context); Field menuKeyFields = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); if (menuKeyFields != null) { menuKeyFields.setAccessible(true); menuKeyFields.setBoolean(config, false); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.netflix.spinnaker.kork.jedis.JedisHealthIndicatorFactory.java
public static HealthIndicator build(Pool<Jedis> jedisPool) { try {/*from w w w .ja v a 2s .c o m*/ final Pool<Jedis> src = jedisPool; final Field poolAccess = Pool.class.getDeclaredField("internalPool"); poolAccess.setAccessible(true); GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool); return () -> { Jedis jedis = null; Health.Builder health; try { jedis = src.getResource(); if ("PONG".equals(jedis.ping())) { health = Health.up(); } else { health = Health.down(); } } catch (Exception ex) { health = Health.down(ex); } finally { if (jedis != null) jedis.close(); } health.withDetail("maxIdle", internal.getMaxIdle()); health.withDetail("minIdle", internal.getMinIdle()); health.withDetail("numActive", internal.getNumActive()); health.withDetail("numIdle", internal.getNumIdle()); health.withDetail("numWaiters", internal.getNumWaiters()); return health.build(); }; } catch (IllegalAccessException | NoSuchFieldException e) { throw new BeanCreationException("Error creating Redis health indicator", e); } }
From source file:Main.java
public static void loadContext(android.content.Context context, Field field, Object object) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {// w w w.ja va 2 s .com field.setAccessible(true); Class fType = (Class) field.getGenericType(); Constructor constructor = fType.getConstructor(android.content.Context.class); if (constructor != null) { field.set(object, constructor.newInstance(context)); } }
From source file:io.apiman.plugins.keycloak_oauth_policy.ClaimLookup.java
private static boolean hasJsonPropertyAnnotation(Field f) { for (Field g : f.getType().getDeclaredFields()) { g.setAccessible(true); if (g.getAnnotation(JsonProperty.class) != null) return true; }//from www . j av a2 s. c o m return false; }
From source file:com.evolveum.midpoint.repo.sql.util.HibernateToSqlTranslator.java
/** * Do not use in production code! Only for testing purposes only. Used for example during query engine upgrade. * Method provides translation from hibernate {@link Criteria} to plain SQL string query. * * @param criteria/*from ww w. j a va2s . c om*/ * @return SQL string, null if criteria parameter was null. */ public static String toSql(Criteria criteria) { if (criteria == null) { return null; } try { CriteriaImpl c; if (criteria instanceof CriteriaImpl) { c = (CriteriaImpl) criteria; } else { CriteriaImpl.Subcriteria subcriteria = (CriteriaImpl.Subcriteria) criteria; c = (CriteriaImpl) subcriteria.getParent(); } SessionImpl s = (SessionImpl) c.getSession(); SessionFactoryImplementor factory = s.getSessionFactory(); String[] implementors = factory.getImplementors(c.getEntityOrClassName()); CriteriaLoader loader = new CriteriaLoader( (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), factory, c, implementors[0], s.getLoadQueryInfluencers()); Field f = OuterJoinLoader.class.getDeclaredField("sql"); f.setAccessible(true); return (String) f.get(loader); } catch (Exception ex) { throw new SystemException(ex.getMessage(), ex); } }