/** * Sets the given value on an the receivers's accessible field with the given name. * * @param receiver the receiver, never <code>null</code> * @param fieldName the field's name, never <code>null</code> * @param value the value to set * * @throws NoSuchFieldException see {@link Class#getField(String)} * @throws SecurityException see {@link Class#getField(String)} * @throws IllegalAccessException see {@link Field#set(Object, Object)} * @throws IllegalArgumentException see {@link Field#set(Object, Object)} */ public void set(Object receiver, String fieldName, /* @Nullable */ Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Preconditions.checkNotNull(receiver,"receiver"); Preconditions.checkNotNull(fieldName,"fieldName"); Class<? extends Object> clazz = receiver.getClass(); Field f = getDeclaredField(clazz, fieldName); if (!f.isAccessible()) f.setAccessible(true); f.set(receiver, value); }
/** * Sets the given value on an the receivers's accessible field with the given name. * * @param receiver the receiver, never <code>null</code> * @param fieldName the field's name, never <code>null</code> * @param value the value to set * * @throws NoSuchFieldException see {@link Class#getField(String)} * @throws SecurityException see {@link Class#getField(String)} * @throws IllegalAccessException see {@link Field#set(Object, Object)} * @throws IllegalArgumentException see {@link Field#set(Object, Object)} */ public void set(Object receiver, String fieldName, /* @Nullable */ Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Preconditions.checkNotNull(receiver,"receiver"); Preconditions.checkNotNull(fieldName,"fieldName"); Class<? extends Object> clazz = receiver.getClass(); Field f = getDeclaredField(clazz, fieldName); if (!f.isAccessible()) f.setAccessible(true); f.set(receiver, value); }
/** * Retrieves the value of the given accessible field of the given receiver. * * @param receiver the container of the field, not <code>null</code> * @param fieldName the field's name, not <code>null</code> * @return the value of the field * * @throws NoSuchFieldException see {@link Class#getField(String)} * @throws SecurityException see {@link Class#getField(String)} * @throws IllegalAccessException see {@link Field#get(Object)} * @throws IllegalArgumentException see {@link Field#get(Object)} */ @SuppressWarnings("unchecked") /* @Nullable */ public <T> T get(Object receiver, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Preconditions.checkNotNull(receiver,"receiver"); Preconditions.checkNotNull(fieldName,"fieldName"); Class<? extends Object> clazz = receiver.getClass(); Field f = getDeclaredField(clazz, fieldName); if (!f.isAccessible()) f.setAccessible(true); return (T) f.get(receiver); }
/** * Retrieves the value of the given accessible field of the given receiver. * * @param receiver the container of the field, not <code>null</code> * @param fieldName the field's name, not <code>null</code> * @return the value of the field * * @throws NoSuchFieldException see {@link Class#getField(String)} * @throws SecurityException see {@link Class#getField(String)} * @throws IllegalAccessException see {@link Field#get(Object)} * @throws IllegalArgumentException see {@link Field#get(Object)} */ @SuppressWarnings("unchecked") /* @Nullable */ public <T> T get(Object receiver, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Preconditions.checkNotNull(receiver,"receiver"); Preconditions.checkNotNull(fieldName,"fieldName"); Class<? extends Object> clazz = receiver.getClass(); Field f = getDeclaredField(clazz, fieldName); if (!f.isAccessible()) f.setAccessible(true); return (T) f.get(receiver); }