List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:Main.java
public static int getFieldIntSafely(Class clz, String fieldName, Object instance) { try {//from w w w . ja va 2 s. c o m Field field = clz.getDeclaredField(fieldName); field.setAccessible(true); return field.getInt(instance); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } return 0; }
From source file:Main.java
private static void setField(Class type, String name, Object instance, Object value) throws NoSuchFieldException, IllegalAccessException { Field field = type.getDeclaredField(name); field.setAccessible(true); field.set(instance, value);/*from w w w . j a va2s . c om*/ }
From source file:Main.java
public static String getId(Object t) { try {// w ww.j av a2s.co m Field idField = t.getClass().getDeclaredField("id"); idField.setAccessible(true); return idField.get(t).toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Object fieldGet(Object object, String fieldName) throws Exception { Field field = object.getClass().getDeclaredField(fieldName); field.setAccessible(true); return field.get(object); }
From source file:Main.java
public static void fieldSet(Object object, String fieldName, Object value) throws Exception { Field field = object.getClass().getDeclaredField(fieldName); field.setAccessible(true); field.set(object, value);//from ww w. j a v a 2s . c om }
From source file:Main.java
public static void setStaticFieldValue(@SuppressWarnings("rawtypes") final Class clazz, final String name, final Object value) throws Exception { Field field = clazz.getDeclaredField(name); field.setAccessible(true); // There seems to be no field "modifiers" in Android/DalvikVM? // Field modifiersField = Field.class.getDeclaredField("modifiers"); // modifiersField.setAccessible(true); // modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, value);//w w w .j ava 2s . c o m }
From source file:Main.java
/** * Get a Hashtable of all class member fields (including private) of the * given object/* ww w . j av a 2 s . com*/ * * @param objectInstance Object to get data from * @return Hashtable of all values * * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Map<String, Object> getAllFields(Object objectInstance) throws IllegalArgumentException, IllegalAccessException // NOSONAR { HashMap<String, Object> map = new HashMap<String, Object>(); Class<? extends Object> clazz = objectInstance.getClass(); Field fields[] = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; f.setAccessible(true); String fieldName = f.getName(); Object fieldValue = f.get(objectInstance); map.put(fieldName, fieldValue); } return map; }
From source file:Main.java
protected static Object getDeclaredField(Object obj, String name) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { final Field f = obj.getClass().getDeclaredField(name); f.setAccessible(true); return f.get(obj); }
From source file:Main.java
public static <T> void setFinalField(Class<T> clazz, T object, String fieldName, Object value) { try {/* w ww. j a v a2 s .c o 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
private static void loadClass(Class clazz) throws NoSuchFieldException, IllegalAccessException { final Field id = clazz.getDeclaredField("ID"); id.setAccessible(true); id.get(null);/*from www .jav a 2 s . c om*/ }