List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:com.infinira.aerospike.dataaccess.util.Utils.java
@SuppressWarnings("unchecked") public static <V> V get(Object object, String fieldName) { Assert.notNull(object, "Object cannot be null."); Assert.notNull(fieldName, "Fieldname cannot be null."); Class<?> clazz = object.getClass(); while (clazz != null) { try {//from w ww .j a v a2 s . c o m Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return (V) field.get(object); } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); } catch (Exception e) { throw new IllegalStateException(e); } } return null; }
From source file:Main.java
/** * Extracts a field from a class using reflection. * * @param clazz from which to get the field object * @param name the name of the field object * @return the field object./* w ww . ja v a 2s . c o m*/ * @throws PrivilegedActionException */ static Field getField(final Class<?> clazz, final String name) throws PrivilegedActionException { final PrivilegedExceptionAction<Field> action = new PrivilegedExceptionAction<Field>() { public Field run() throws Exception { Field field = clazz.getDeclaredField(name); field.setAccessible(true); return field; } }; return AccessController.doPrivileged(action); }
From source file:de.cbb.mplayer.util.ReflectionHelper.java
/** * Searches for a field in the given class and all of its super classes. * @param clazz Class to start the search for the field * @param name Name of the field//from w ww. ja v a2 s. com * @return The field that was found * @throws NoSuchFieldException */ public static Field getField(Class<?> clazz, String name) throws NoSuchFieldException { Class<?> searchClass = clazz; Field field = null; while (field == null && searchClass != null) { try { field = searchClass.getDeclaredField(name); } catch (NoSuchFieldException e) { searchClass = searchClass.getSuperclass(); } } if (field == null) { throw new NoSuchFieldException(clazz.getSimpleName() + "." + name); //$NON-NLS-1$ } return field; }
From source file:Main.java
private static ArrayList<ArrayList> sortObjectArrayListSimpleMaster(ArrayList listIn, String paramName) { ArrayList<ArrayList> answer = new ArrayList<ArrayList>(); ArrayList newList = new ArrayList(); ArrayList<Integer> indices = new ArrayList<Integer>(); try {//w w w.jav a 2 s . c om if (listIn.size() > 0) { Class<?> c = listIn.get(0).getClass(); Field f = c.getDeclaredField(paramName); f.setAccessible(true); Class<?> t = f.getType(); Double dd = new Double(14); Float ff = new Float(14); Integer ii = new Integer(14); Map sortedPos = new LinkedHashMap(); Map sortedNeg = new LinkedHashMap(); Map unsorted = new LinkedHashMap(); int indexCount = 0; long count = 0; if (t.isPrimitive()) { for (Object thisObj : listIn) { Object o = f.get(thisObj); double d = 0; if (t.getName().equals("char")) { d = (int) ((Character) o); } else if (t.isInstance(dd)) d = (Double) o; else if (t.isInstance(ff)) d = (Float) o; else if (t.isInstance(ii)) d = (Integer) o; else d = new Double(o.toString()); boolean isNegative = false; if (d < 0) { isNegative = true; d = Math.abs(d); } String format = "%1$30f"; String newKey = String.format(format, d); String format2 = "%1$20d"; String countString = String.format(format2, count); newKey += "-" + countString; if (isNegative) { sortedNeg.put(newKey, thisObj); } else { sortedPos.put(newKey, thisObj); } unsorted.put(thisObj, indexCount); count++; indexCount++; } TreeMap<String, Object> resultPos = new TreeMap(); resultPos.putAll(sortedPos); sortedPos = resultPos; TreeMap<String, Object> resultNeg = new TreeMap(); resultNeg.putAll(sortedNeg); sortedNeg = resultNeg; } else if (t.isInstance(paramName)) { // System.out.println("is a string with value " + o); for (Object thisObj : listIn) { String key = (String) (f.get(thisObj)); sortedPos.put(key + "-" + count, thisObj); unsorted.put(thisObj, indexCount); count++; indexCount++; } TreeMap<String, Object> result = new TreeMap(String.CASE_INSENSITIVE_ORDER); result.putAll(sortedPos); sortedPos = result; } Iterator itNeg = sortedNeg.entrySet().iterator(); while (itNeg.hasNext()) { Map.Entry pairs = (Map.Entry) itNeg.next(); newList.add(pairs.getValue()); itNeg.remove(); } Collections.reverse(newList); Iterator itPos = sortedPos.entrySet().iterator(); while (itPos.hasNext()) { Map.Entry pairs = (Map.Entry) itPos.next(); Object obj = pairs.getValue(); newList.add(obj); indices.add((Integer) unsorted.get(obj)); itPos.remove(); } } } catch (Exception e) { System.out .println("problem sorting list. listIn.size(): " + listIn.size() + " and param: " + paramName); answer.add(newList); answer.add(indices); return answer; } answer.add(newList); answer.add(indices); return answer; }
From source file:com.junly.common.util.ReflectUtils.java
/** * <p class="detail">/*from w ww .j a va 2 s . c om*/ * ? * </p> * @author wan.Dong * @date 20161112 * @param obj * @param name ?? * @return */ public static Object getProperty(Object obj, String name) { if (obj != null) { Class<?> clazz = obj.getClass(); while (clazz != null) { Field field = null; try { field = clazz.getDeclaredField(name); } catch (Exception e) { clazz = clazz.getSuperclass(); continue; } try { field.setAccessible(true); return field.get(obj); } catch (Exception e) { return null; } finally { field.setAccessible(false); } } } return null; }
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!"); }/* w w w .j a va 2s.c o m*/ 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
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. ja va 2 s .com*/ 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:edu.umd.cs.marmoset.utilities.JProcess.java
/** * Uses reflection to extract the pid, a private field of the private class UNIXProcess. * This will fail on any non-Unix platform that doesn't use UNIXProcess. It may * fail if the UNIXProcess class changes at all. It may fail anyway for unpredictable * reasons.//from w w w . ja v a 2 s .c o m * @param process The process * @return the pid of this process * @throws NoSuchFieldException * @throws IllegalAccessException */ public static int getPid(Process process) throws NoSuchFieldException, IllegalAccessException { Class<? extends Process> processClass = process.getClass(); Field pidField = processClass.getDeclaredField("pid"); pidField.setAccessible(true); return pidField.getInt(process); }
From source file:com.prowidesoftware.deprecation.DeprecationUtils.java
/** * Helper hack to set environment variables from Java code *//* ww w.ja v a 2s .c o m*/ @SuppressWarnings({ "unchecked", "rawtypes" }) private static void setEnv(final String key, final String value) { try { Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); theEnvironmentField.setAccessible(true); Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null); env.put(key, value); Field theCaseInsensitiveEnvironmentField = processEnvironmentClass .getDeclaredField("theCaseInsensitiveEnvironment"); theCaseInsensitiveEnvironmentField.setAccessible(true); Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null); cienv.put(key, value); } catch (NoSuchFieldException e) { try { Class[] classes = Collections.class.getDeclaredClasses(); Map<String, String> env = System.getenv(); for (Class cl : classes) { if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { Field field = cl.getDeclaredField("m"); field.setAccessible(true); Object obj = field.get(env); Map<String, String> map = (Map<String, String>) obj; map.clear(); map.put(key, value); } } } catch (Exception e2) { e2.printStackTrace(); } } catch (Exception e1) { e1.printStackTrace(); } }
From source file:com.relicum.titleapi.Reflection.ReflectionUtil.java
public static void setPrivateField(Class<EntityArmorStand> clazz, Object handle, String fieldName, Object value) throws Exception { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true);//from w ww . ja v a 2 s.co m field.set(handle, value); }