/** * Reads an accessible {@link Field}. * * @param field * the field to use * @param target * the object to call on, may be {@code null} for {@code static} fields * @return the field value * @throws IllegalArgumentException * if the field is {@code null} * @throws IllegalAccessException * if the field is not accessible */ public static Object readField(final Field field, final Object target) throws IllegalAccessException { return readField(field, target, false); }
/** * Reads the named {@code public} {@link Field}. Superclasses will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the value of the field * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty or could not be found * @throws IllegalAccessException * if the named field is not {@code public} */ public static Object readField(final Object target, final String fieldName) throws IllegalAccessException { return readField(target, fieldName, false); }
private static Object getVirtualFlow(Skin<?> skin) { try { // On JRE 9 and 10, the field is declared in TreeViewSkin // http://hg.openjdk.java.net/openjfx/9/rt/file/c734b008e3e8/modules/javafx.controls/src/main/java/javafx/scene/control/skin/TreeViewSkin.java#l85 // http://hg.openjdk.java.net/openjfx/10/rt/file/d14b61c6be12/modules/javafx.controls/src/main/java/javafx/scene/control/skin/TreeViewSkin.java#l85 // On JRE 8, the field is declared in the VirtualContainerBase superclass // http://hg.openjdk.java.net/openjfx/8/master/rt/file/f89b7dc932af/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/VirtualContainerBase.java#l68 return FieldUtils.readField(skin, "flow", true); } catch (IllegalAccessException ignored) { } return null; } }
/** * Reads a static {@link Field}. * * @param field * to read * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. * @return the field value * @throws IllegalArgumentException * if the field is {@code null} or not {@code static} * @throws IllegalAccessException * if the field is not made accessible */ public static Object readStaticField(final Field field, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(field != null, "The field must not be null"); Validate.isTrue(Modifier.isStatic(field.getModifiers()), "The field '%s' is not static", field.getName()); return readField(field, (Object) null, forceAccess); }
private void appendFields(final Class<?> clazz) { for (final Field field : FieldUtils.getAllFields(clazz)) { if (accept(field)) { try { diffBuilder.append(field.getName(), readField(field, left, true), readField(field, right, true)); } catch (final IllegalAccessException ex) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } }
/** * Gets a {@link Field} value by name. Only the class of the specified object will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match public fields. * @return the Field object * @throws IllegalArgumentException * if {@code target} is {@code null}, or the field name is blank or empty or could not be found * @throws IllegalAccessException * if the field is not made accessible */ public static Object readDeclaredField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(target != null, "target object must not be null"); final Class<?> cls = target.getClass(); final Field field = getDeclaredField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls, fieldName); // already forced access above, don't repeat it here: return readField(field, target, false); }
/** * Reads the named {@link Field}. Superclasses will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the field value * @throws IllegalArgumentException * if {@code target} is {@code null}, or the field name is blank or empty or could not be found * @throws IllegalAccessException * if the named field is not made accessible */ public static Object readField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(target != null, "target object must not be null"); final Class<?> cls = target.getClass(); final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate field %s on %s", fieldName, cls); // already forced access above, don't repeat it here: return readField(field, target, false); }
public static boolean validateAnno(Object object){ for (Field field:object.getClass().getDeclaredFields()){ NotNull notNullCheck = field.getAnnotation(NotNull.class); if (notNullCheck!=null){ try { Object val = FieldUtils.readField(field,object,true); if (StringUtils.isBlank(String.valueOf(val))){ logger.error("Field={}.{} can not be null!",object.getClass().getSimpleName(),field.getName()); return false; } } catch (IllegalAccessException e) { logger.error(e.getMessage(),e); } } } return true; }
public static <T> T getPrivateField( Object target, String fieldName, Class<T> fieldType ) throws Exception { return fieldType.cast( readField( target, fieldName, true ) ); }
@SuppressWarnings("unchecked") private static Map<InternalProperty, Object> dirtyReadProperties(Connection connection) throws IllegalAccessException { assertTrue(connection instanceof CalciteConnection); return (Map<InternalProperty, Object>) FieldUtils.readField(connection, "properties"); } }
@Test public void testReadNamedFieldForceAccess() throws Exception { assertEquals("s", FieldUtils.readField(publicChild, "s", true)); assertEquals("ss", FieldUtils.readField(publiclyShadowedChild, "s", true)); assertEquals("ss", FieldUtils.readField(privatelyShadowedChild, "s", true)); assertEquals(Boolean.FALSE, FieldUtils.readField(publicChild, "b", true)); assertEquals(Boolean.TRUE, FieldUtils.readField(publiclyShadowedChild, "b", true)); assertEquals(Boolean.TRUE, FieldUtils.readField(privatelyShadowedChild, "b", true)); assertEquals(I0, FieldUtils.readField(publicChild, "i", true)); assertEquals(I1, FieldUtils.readField(publiclyShadowedChild, "i", true)); assertEquals(I1, FieldUtils.readField(privatelyShadowedChild, "i", true)); assertEquals(D0, FieldUtils.readField(publicChild, "d", true)); assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d", true)); assertEquals(D1, FieldUtils.readField(privatelyShadowedChild, "d", true)); FieldUtils.readField(publicChild, null, true); fail("a null field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readField(publicChild, "", true); fail("an empty field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readField(publicChild, " ", true); fail("a blank field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readField((Object) null, "none", true); fail("a null target should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) {
@Test public void testReadField() throws Exception { final Field parentS = FieldUtils.getDeclaredField(parentClass, "s"); assertEquals("s", FieldUtils.readField(parentS, publicChild)); assertEquals("s", FieldUtils.readField(parentS, publiclyShadowedChild)); assertEquals("s", FieldUtils.readField(parentS, privatelyShadowedChild)); final Field parentB = FieldUtils.getDeclaredField(parentClass, "b", true); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, publicChild)); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, publiclyShadowedChild)); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, privatelyShadowedChild)); final Field parentI = FieldUtils.getDeclaredField(parentClass, "i", true); assertEquals(I0, FieldUtils.readField(parentI, publicChild)); assertEquals(I0, FieldUtils.readField(parentI, publiclyShadowedChild)); assertEquals(I0, FieldUtils.readField(parentI, privatelyShadowedChild)); final Field parentD = FieldUtils.getDeclaredField(parentClass, "d", true); assertEquals(D0, FieldUtils.readField(parentD, publicChild)); assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild)); assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild)); try { FieldUtils.readField(null, publicChild); fail("a null field should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { // expected } }
@Test public void testReadFieldForceAccess() throws Exception { final Field parentS = FieldUtils.getDeclaredField(parentClass, "s"); parentS.setAccessible(false); assertEquals("s", FieldUtils.readField(parentS, publicChild, true)); assertEquals("s", FieldUtils.readField(parentS, publiclyShadowedChild, true)); assertEquals("s", FieldUtils.readField(parentS, privatelyShadowedChild, true)); final Field parentB = FieldUtils.getDeclaredField(parentClass, "b", true); parentB.setAccessible(false); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, publicChild, true)); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, publiclyShadowedChild, true)); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, privatelyShadowedChild, true)); final Field parentI = FieldUtils.getDeclaredField(parentClass, "i", true); parentI.setAccessible(false); assertEquals(I0, FieldUtils.readField(parentI, publicChild, true)); assertEquals(I0, FieldUtils.readField(parentI, publiclyShadowedChild, true)); assertEquals(I0, FieldUtils.readField(parentI, privatelyShadowedChild, true)); final Field parentD = FieldUtils.getDeclaredField(parentClass, "d", true); parentD.setAccessible(false); assertEquals(D0, FieldUtils.readField(parentD, publicChild, true)); assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild, true)); assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild, true)); try { FieldUtils.readField(null, publicChild, true); fail("a null field should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { // expected } }
@Test public void testReadNamedField() throws Exception { assertEquals("s", FieldUtils.readField(publicChild, "s")); assertEquals("ss", FieldUtils.readField(publiclyShadowedChild, "s")); assertEquals("s", FieldUtils.readField(privatelyShadowedChild, "s")); FieldUtils.readField(publicChild, null); fail("a null field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readField(publicChild, ""); fail("an empty field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readField(publicChild, " "); fail("a blank field name should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readField((Object) null, "none"); fail("a null target should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.readField(publicChild, "b"); fail("expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { assertEquals(Boolean.TRUE, FieldUtils.readField(publiclyShadowedChild, "b")); try { FieldUtils.readField(privatelyShadowedChild, "b"); fail("expected IllegalArgumentException");
@Test public void testWriteNamedFieldForceAccess() throws Exception { FieldUtils.writeField(publicChild, "s", "S", true); assertEquals("S", FieldUtils.readField(publicChild, "s", true)); FieldUtils.writeField(publicChild, "b", Boolean.TRUE, true); assertEquals(Boolean.TRUE, FieldUtils.readField(publicChild, "b", true)); FieldUtils.writeField(publicChild, "i", Integer.valueOf(1), true); assertEquals(Integer.valueOf(1), FieldUtils.readField(publicChild, "i", true)); FieldUtils.writeField(publicChild, "d", Double.valueOf(1.0), true); assertEquals(Double.valueOf(1.0), FieldUtils.readField(publicChild, "d", true)); FieldUtils.writeField(publiclyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readField(publiclyShadowedChild, "s", true)); FieldUtils.writeField(publiclyShadowedChild, "b", Boolean.FALSE, true); assertEquals(Boolean.FALSE, FieldUtils.readField(publiclyShadowedChild, "b", true)); FieldUtils.writeField(publiclyShadowedChild, "i", Integer.valueOf(0), true); assertEquals(Integer.valueOf(0), FieldUtils.readField(publiclyShadowedChild, "i", true)); FieldUtils.writeField(publiclyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readField(publiclyShadowedChild, "d", true)); FieldUtils.writeField(privatelyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readField(privatelyShadowedChild, "s", true)); FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.FALSE, true); assertEquals(Boolean.FALSE, FieldUtils.readField(privatelyShadowedChild, "b", true)); FieldUtils.writeField(privatelyShadowedChild, "i", Integer.valueOf(0), true); assertEquals(Integer.valueOf(0), FieldUtils.readField(privatelyShadowedChild, "i", true)); FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readField(privatelyShadowedChild, "d", true)); }
@Test public void testWriteNamedField() throws Exception { FieldUtils.writeField(publicChild, "s", "S"); assertEquals("S", FieldUtils.readField(publicChild, "s")); try { FieldUtils.writeField(publicChild, "b", Boolean.TRUE); assertEquals("S", FieldUtils.readField(publiclyShadowedChild, "s")); FieldUtils.writeField(publiclyShadowedChild, "b", Boolean.FALSE); assertEquals(Boolean.FALSE, FieldUtils.readField(publiclyShadowedChild, "b")); FieldUtils.writeField(publiclyShadowedChild, "i", Integer.valueOf(0)); assertEquals(Integer.valueOf(0), FieldUtils.readField(publiclyShadowedChild, "i")); FieldUtils.writeField(publiclyShadowedChild, "d", Double.valueOf(0.0)); assertEquals(Double.valueOf(0.0), FieldUtils.readField(publiclyShadowedChild, "d")); assertEquals("S", FieldUtils.readField(privatelyShadowedChild, "s")); try { FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.TRUE);
/** * Gets the property. * * @param obj the obj * @param name the name * @param defaultValue the default value * @param forced whether or not to force access to the field * @return the property */ protected Object getProperty(Object obj, String name, Object defaultValue, boolean forced) { try { if (forced) { return FieldUtils.readField(obj, name, forced); } else { return PropertyUtils.isReadable(obj, name) ? PropertyUtils.getProperty(obj, name) : defaultValue; } } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { logger.error("", e); } logger.debug("Could not access property '{}' of object '{}'", name, obj); return defaultValue; }
public Object retrieveValue(Object instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (this.field == null) { return getter.invoke(instance); } else { return FieldUtils.readField(field, instance, true); } }
/** * @throws IllegalAccessException * @see Encounter#addProvider(EncounterRole,Provider) */ @Test public void addProvider_shouldNotAddSameProviderTwiceForRole() throws IllegalAccessException { //given Encounter encounter = new Encounter(); EncounterRole role = new EncounterRole(); Provider provider1 = new Provider(); //when encounter.addProvider(role, provider1); encounter.addProvider(role, provider1); //then // we need to cheat and use reflection to look at the private encounterProviders property; we don't want the getProvidersByRole method hiding duplicates from us Collection<EncounterProvider> providers = (Collection<EncounterProvider>) FieldUtils.readField(encounter, "encounterProviders", true); Assert.assertEquals(1, providers.size()); Assert.assertTrue(encounter.getProvidersByRole(role).contains(provider1)); }
public static <T> T readField(Object object, String field, Class<T> type) { try { Field actualField = getField(object.getClass(), field, true); if (!isInstanceOfOrWrappable(actualField.getType(), type)) { String message = format("Field %s is a type of %s instead of %s.", field, actualField.getType(), type); throw new IllegalStateException(message); } return (T) FieldUtils.readField(actualField, object, true); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }