List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:Main.java
static Unsafe getSMU() { try {//w ww.j a v a2 s .co m return sun.misc.Unsafe.getUnsafe(); } catch (SecurityException tryReflectionInstead) { // ignore } try { return java.security.AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> { Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; for (Field f : k.getDeclaredFields()) { f.setAccessible(true); Object x = f.get(null); if (k.isInstance(x)) return k.cast(x); } throw new NoSuchFieldError("the Unsafe"); }); } catch (java.security.PrivilegedActionException e) { throw new RuntimeException("Could not initialize intrinsics", e.getCause()); } }
From source file:Main.java
/** * Converts a given string into a color. * /* www . j ava 2 s . co m*/ * @param value * the string, either a name or a hex-string. * @return the color. */ public static Color stringToColor(final String value) { if (value == null) { return Color.black; } try { // get color by hex or octal value return Color.decode(value); } catch (NumberFormatException nfe) { // if we can't decode lets try to get it by name try { // try to get a color by name using reflection final Field f = Color.class.getField(value); return (Color) f.get(null); } catch (Exception ce) { // if we can't get any color return black return Color.black; } } }
From source file:Main.java
public static Object getFieldValue(Object receiver, Object defaultValue, Field field) { if (field == null) { return defaultValue; }// w w w.j a v a 2 s . co m try { return field.get(receiver); } catch (Exception e) { if (DEBUG) { e.printStackTrace(); } } return defaultValue; }
From source file:springobjectmapper.ReflectionHelper.java
public static Object[] getValues(List<Field> fields, Object o) { List<Object> results = new ArrayList<Object>(); for (Field field : fields) { try {/*from w ww .ja v a2s .c o m*/ results.add(field.get(o)); } catch (Exception e) { ReflectionUtils.handleReflectionException(e); } } return results.toArray(); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> List<T> generatePropertyList(Collection<?> collection, String property) { assert property != null; if (collection == null || collection.isEmpty()) { return new ArrayList<T>(0); }// w w w . j ava2s. c o m List<T> list = new ArrayList<T>(collection.size()); try { for (Object obj : collection) { Field field = obj.getClass().getDeclaredField(property); field.setAccessible(true); Object object = field.get(obj); list.add((T) object); } return list; } catch (Throwable e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Get all fields' value and put them to a map. * //from ww w. j a v a 2 s .c om * @param bean * @return Map */ public static Map<String, Object> getFieldValueMap(Object bean) { Class<?> cls = bean.getClass(); Map<String, Object> valueMap = new HashMap<String, Object>(); // Get all fields. Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); Object value = field.get(bean); // if(value == null) { // valueMap.put(field.getName(), ""); // continue; // } valueMap.put(field.getName(), value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return valueMap; }
From source file:Main.java
public static Object getDeclaredField(Object obj, Class<?> cls, String fieldName) throws NoSuchFieldException { if (obj == null || cls == null || fieldName == null) { throw new IllegalArgumentException("parameter can not be null!"); }/*from w ww.j a v a2 s. c om*/ try { Field declaredField = cls.getDeclaredField(fieldName); declaredField.setAccessible(true); return declaredField.get(obj); } catch (Exception e) { throw new NoSuchFieldException(fieldName); } }
From source file:Main.java
/** * Gets the {@code sun.misc.Unsafe} instance, or {@code null} if not available on this platform. *//*w w w . ja va 2 s .c om*/ private static sun.misc.Unsafe getUnsafe() { sun.misc.Unsafe unsafe = null; try { unsafe = AccessController.doPrivileged(new PrivilegedExceptionAction<Unsafe>() { @Override public sun.misc.Unsafe run() throws Exception { Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; for (Field f : k.getDeclaredFields()) { f.setAccessible(true); Object x = f.get(null); if (k.isInstance(x)) { return k.cast(x); } } // The sun.misc.Unsafe field does not exist. return null; } }); } catch (Throwable e) { // Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError // for Unsafe. } return unsafe; }
From source file:Main.java
public static boolean areNotificationsEnabled(Context context) { AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); ApplicationInfo appInfo = context.getApplicationInfo(); String pkg = context.getApplicationContext().getPackageName(); int uid = appInfo.uid; try {/*from w w w . j a v a2s . c o m*/ Class<?> appOpsClass = Class.forName(AppOpsManager.class.getName()); Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class); Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION); int value = (Integer) opPostNotificationValue.get(Integer.class); return ((Integer) checkOpNoThrowMethod.invoke(appOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED); } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | InvocationTargetException | IllegalAccessException | RuntimeException e) { return true; } }
From source file:com.basho.riak.client.api.convert.reflection.ClassUtil.java
public static <T> Object getFieldValue(Field f, T obj) { Object value = null;/*from w ww . j a v a 2 s.co m*/ try { value = f.get(obj); } catch (IllegalAccessException e) { throw new IllegalStateException("Unable to get Riak annotated field value", e); } return value; }