List of usage examples for java.lang.reflect Field set
@CallerSensitive @ForceInline public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException
From source file:Main.java
public static <T> void setFinalField(Class<T> clazz, T object, String fieldName, Object value) { try {//from w w w.j a v a 2 s . co m Field headerField = clazz.getDeclaredField(fieldName); headerField.setAccessible(true); headerField.set(object, value); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e2) { throw new RuntimeException(e2); } }
From source file:Main.java
public static void setMember(Object object, String memberName, Object value) throws IllegalAccessException, NoSuchFieldException { Field declaredField = getField(object.getClass(), memberName); declaredField.setAccessible(true);/* w ww . j a va 2 s. c o m*/ declaredField.set(object, value); }
From source file:eu.riscoss.RemoteRiskAnalyserMain.java
static void loadJSmile() throws Exception { File dir = new File(FileUtils.getTempDirectory(), "jnativelibs-" + RandomStringUtils.randomAlphabetic(30)); if (!dir.mkdir()) { throw new Exception("failed to make dir"); }//from ww w .jav a 2 s. co m File jsmile = new File(dir, "libjsmile.so"); URL jsmURL = Thread.currentThread().getContextClassLoader().getResource("libjsmile.so"); FileUtils.copyURLToFile(jsmURL, jsmile); System.setProperty("java.library.path", System.getProperty("java.library.path") + ":" + dir.getAbsolutePath()); // flush the library paths Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths"); sysPathsField.setAccessible(true); sysPathsField.set(null, null); // Check that it works... System.loadLibrary("jsmile"); dir.deleteOnExit(); jsmile.deleteOnExit(); }
From source file:Main.java
/** * Copy fields from parent object to child object. * * @param parent parent object/* ww w . jav a 2 s.c o m*/ * @param child child object * @param <T> child class * @return filled child object */ public static <T> T shallowCopy(Object parent, T child) { try { List<Field> fields = new ArrayList<>(); Class clazz = parent.getClass(); do { fields.addAll(Arrays.asList(clazz.getDeclaredFields())); } while (!(clazz = clazz.getSuperclass()).equals(Object.class)); for (Field field : fields) { field.setAccessible(true); field.set(child, field.get(parent)); } return child; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void autoMappingJsonToObject(JSONObject json, Object obj) { Field[] fields = obj.getClass().getFields(); for (Field field : fields) { if (json.optString(field.getName()) != null) { try { field.set(obj, json.optString(field.getName())); } catch (Exception e) { }/*from w ww. j a v a 2s . c om*/ } } }
From source file:Main.java
public static void setProperty(Object paramObject1, String paramString, Object paramObject2) { try {//from w w w . j a v a 2s. com Field localField = paramObject1.getClass().getDeclaredField(paramString); localField.setAccessible(true); localField.set(paramObject1, paramObject2); } catch (Exception var4) { ; } }
From source file:Main.java
public static void objectCopy(Object from, Object to) throws Exception { if (from.getClass() != to.getClass()) { throw new IllegalArgumentException("[objectCopy]The left and right must be same class"); }//www. j av a 2s .co m Class<?> clz = from.getClass(); Field[] fs = clz.getDeclaredFields(); for (int i = 0; i < fs.length; i++) { Field field = fs[i]; field.setAccessible(true); Object value = field.get(from); field.set(to, value); } }
From source file:SampleSet.java
static void modifyWidth(Rectangle r, Integer widthParam) { Field widthField; Integer widthValue;/*from w w w. j a v a 2 s. c o m*/ Class c = r.getClass(); try { widthField = c.getField("width"); widthField.set(r, widthParam); } catch (NoSuchFieldException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } }
From source file:Main.java
/**set dialog dismiss or not*/ public static void setDialogDismiss(DialogInterface dialog, boolean dismiss) { try {/*from w ww . j a v a2 s .c om*/ Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing"); field.setAccessible(true); field.set(dialog, dismiss); dialog.dismiss(); } catch (Exception e) { e.printStackTrace(); } }
From source file:nl.ctrlaltdev.harbinger.testutil.TestUtil.java
public static final void resetMockHttpSessionId() { try {//from w w w.j a va 2s. co m Field nextId = MockHttpSession.class.getDeclaredField("nextId"); nextId.setAccessible(true); nextId.set(null, Integer.valueOf(1)); } catch (Exception e) { throw new RuntimeException(e); } }