List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:Main.java
public static void setField(String paramString, Object paramObject1, Object paramObject2) throws Exception { if (TextUtils.isEmpty(paramString)) throw new RuntimeException("field name can not be empty"); if (paramObject1 == null) throw new RuntimeException("target object can not be null"); Field localField = paramObject1.getClass().getDeclaredField(paramString); if (localField == null) throw new RuntimeException("target object: " + paramObject1.getClass().getName() + " do not have this field: " + paramString); localField.setAccessible(true); localField.set(paramObject1, paramObject2); }
From source file:Main.java
/** * meizu Flyme set status bar light mode *///w ww .jav a 2s .c o m public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) { boolean result = false; if (window != null) { try { WindowManager.LayoutParams lp = window.getAttributes(); Field darkFlag = WindowManager.LayoutParams.class .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); darkFlag.setAccessible(true); meizuFlags.setAccessible(true); int bit = darkFlag.getInt(null); int value = meizuFlags.getInt(lp); if (dark) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); window.setAttributes(lp); result = true; } catch (Exception e) { } } return result; }
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 .j a va 2s. 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:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java
public static Object setFieldValue(Object input, Object value, String fieldName) throws Exception { Class searchClass = input.getClass(); while (searchClass != null) { Field[] fields = searchClass.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) { field.setAccessible(true); //Check to see if we're trying to set a int, long, etc with a String if (field.getType().getName().equals("java.lang.Long")) { if (value instanceof String) { field.set(input, Long.getLong((String) value)); } else { field.set(input, value); }//from w w w . j ava2 s. com } else if (field.getType().getName().equals("java.lang.Integer")) { if (value instanceof String) { field.set(input, Integer.getInteger((String) value)); } else { field.set(input, value); } } else { field.set(input, value); } } } searchClass = searchClass.getSuperclass(); } return input; }
From source file:org.trpr.platform.batch.impl.spring.admin.repository.MapStepExecutionDao.java
private static void copy(final StepExecution sourceExecution, final StepExecution targetExecution) { // Cheaper than full serialization is a reflective field copy, which is // fine for volatile storage ReflectionUtils.doWithFields(StepExecution.class, new ReflectionUtils.FieldCallback() { @Override//w w w . java 2 s .c o m public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); field.set(targetExecution, field.get(sourceExecution)); } }); }
From source file:com.prowidesoftware.deprecation.DeprecationUtils.java
/** * Helper hack to set environment variables from Java code *//*from w w w. j av a 2 s .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.stargis.pipe.util.ReflectionUtils.java
/** * ?, ?DeclaredField, ?./* w w w . j av a2 s . c om*/ * * ?Object?, null. */ public static Field getAccessibleField(final Object obj, final String fieldName) { // Assert.notNull(obj, "object?"); // Assert.hasText(fieldName, "fieldName"); for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Field field = superClass.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (NoSuchFieldException e) {//NOSONAR // Field??,? } } return null; }
From source file:io.github.pellse.decorator.util.reflection.ReflectionUtils.java
private static <T> T privateSetField(T obj, Field field, Object value, boolean override) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); if (field.get(obj) == null || override) field.set(obj, value);/* w w w. j ava 2 s .co m*/ return obj; }
From source file:com.npower.dm.util.BeanHelper.java
/** * private/protected/*from w w w . j av a2 s . com*/ */ static public Object getDeclaredProperty(Object object, Field field) throws IllegalAccessException { assert object != null; assert field != null; boolean accessible = field.isAccessible(); field.setAccessible(true); Object result = field.get(object); field.setAccessible(accessible); return result; }
From source file:org.venice.piazza.servicecontroller.messaging.ServiceMessageThreadManagerTest.java
static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true);//from w ww. j a v a2 s . co m Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, newValue); }