List of usage examples for java.lang Class getDeclaredField
@CallerSensitive public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
From source file:Main.java
public static Object invokeField(Class<?> cls, Object object, String fieldName) throws Exception { Field field = cls.getDeclaredField(fieldName); boolean accessible = field.isAccessible(); try {/* ww w . j av a 2 s . c om*/ field.setAccessible(true); return field.get(object); } finally { field.setAccessible(accessible); } }
From source file:Main.java
private static void setField(Object obj, Class<?> cls, String field, Object value) { try {//from w w w . java 2 s . c o m Field localField = cls.getDeclaredField(field); localField.setAccessible(true); localField.set(obj, value); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
From source file:Main.java
public static int getFieldIntSafely(Class clz, String fieldName, Object instance) { try {//from w w w .j ava 2 s . c om 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
public static void setField(Class cls, String name, Object obj, Object value) { try {/*from w ww. j a v a 2 s . c om*/ Field nameField = cls.getDeclaredField(name); nameField.setAccessible(true); nameField.set(obj, value); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static Object getFieldValue(Class<?> fieldClass, String fieldName, Object instance) { try {//w ww. j a v a 2 s.co m Field e = fieldClass.getDeclaredField(fieldName); e.setAccessible(true); return e.get(instance); } catch (Exception var4) { var4.printStackTrace(); return null; } }
From source file:Main.java
@SuppressWarnings("rawtypes") public static Object getFieldObject(Object target, Class clazz, String fieldName) { try {//from www. j a v a 2 s . c o m Field field = clazz.getDeclaredField(fieldName); if (!field.isAccessible()) { field.setAccessible(true); } return field.get(target); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static Field getField(Class<?> klass, String member) throws NoSuchFieldException { try {/* w w w .j a va 2s .com*/ return klass.getDeclaredField(member); } catch (NoSuchFieldException e) { if (klass.getSuperclass() != null) return getField(klass.getSuperclass(), member); else throw new NoSuchFieldException(String.format("Class does not contain member %s!", member)); } }
From source file:Main.java
/** * Get the private field data value of the given instance of a class * //from www. j av a 2 s. com * @param objectInstance Object to get private member data from * @param fieldName Name of private member to get data from * @return Private member's data * * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Object getPrivateField(Object objectInstance, String fieldName) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException // NOSONAR { Class<? extends Object> clazz = objectInstance.getClass(); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return field.get(objectInstance); }
From source file:Main.java
/** * Set the private field data value of the given instance of a class * /*from w w w . j ava 2s . c om*/ * @param objectInstance Object to set private member data to * @param fieldName Name of private member to set data for * @param value Private member's data to set * * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void setPrivateField(Object objectInstance, String fieldName, Object value) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException // NOSONAR { Class<? extends Object> clazz = objectInstance.getClass(); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); field.set(objectInstance, value); }
From source file:Main.java
private static void injectClassField(Class clazz, String fieldStr, long newValue) { try {/*w w w .j a v a2s . c o m*/ final Field field = clazz.getDeclaredField(fieldStr); if (field != null) { field.setAccessible(true); Object obj = null; field.set(obj, newValue); } } catch (Exception e) { Log.d("REFLECTION", clazz.getSimpleName() + " injection failed for field: " + fieldStr); } }