/** * Gets an accessible {@link Field} by name respecting scope. Only the specified class will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the Field object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty */ public static Field getDeclaredField(final Class<?> cls, final String fieldName) { return getDeclaredField(cls, fieldName, false); }
/** * Gets the value of a {@code static} {@link Field} by name. Only the specified class will be considered. * * @param cls * the {@link Class} 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 object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not made accessible */ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException { final Field field = getDeclaredField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName); // already forced access above, don't repeat it here: return readStaticField(field, false); }
/** * 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); }
/** * Writes a named {@code static} {@link Field}. Only the specified class will be considered. * * @param cls * {@link Class} on which the field is to be found * @param fieldName * to write * @param value * to set * @param forceAccess * whether to break scope restrictions using the {@code AccessibleObject#setAccessible(boolean)} method. * {@code false} will only match {@code public} fields. * @throws IllegalArgumentException * if {@code cls} is {@code null}, the field name is blank or empty, the field cannot be located or is * not {@code static}, or {@code value} is not assignable * @throws IllegalAccessException * if the field is not made accessible or is {@code final} */ public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException { final Field field = getDeclaredField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName); // already forced access above, don't repeat it here: writeField(field, (Object) null, value, false); }
/** * Writes a {@code public} {@link Field}. Only the specified class will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param value * to set * @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. * @throws IllegalArgumentException * if {@code target} is {@code null}, {@code fieldName} is blank or empty or could not be found, or * {@code value} is not assignable * @throws IllegalAccessException * if the field is not made accessible */ public static void writeDeclaredField(final Object target, final String fieldName, final Object value, 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.getName(), fieldName); // already forced access above, don't repeat it here: writeField(field, target, value, false); } }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldAccessIllegalArgumentException3() { FieldUtils.getDeclaredField(PublicChild.class, ""); }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldAccessIllegalArgumentException1() { FieldUtils.getDeclaredField(null, "none"); }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldForceAccessIllegalArgumentException4() { FieldUtils.getDeclaredField(PublicChild.class, " ", true); }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldAccessIllegalArgumentException2() { FieldUtils.getDeclaredField(PublicChild.class, null); }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldAccessIllegalArgumentException4() { FieldUtils.getDeclaredField(PublicChild.class, " "); }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldForceAccessIllegalArgumentException1() { FieldUtils.getDeclaredField(null, "none", true); }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldForceAccessIllegalArgumentException2() { FieldUtils.getDeclaredField(PublicChild.class, null, true); }
@Test(expected = IllegalArgumentException.class) public void testGetDeclaredFieldForceAccessIllegalArgumentException3() { FieldUtils.getDeclaredField(PublicChild.class, "", true); }
@Test public void testGetDeclaredField() { assertNull(FieldUtils.getDeclaredField(PublicChild.class, "VALUE")); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "s")); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "b")); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i")); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d")); assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE")); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s").getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b").getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i").getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d").getDeclaringClass()); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "VALUE")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "i")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d")); }
@Test public void testGetDeclaredFieldForceAccess() { assertEquals(PublicChild.class, FieldUtils.getDeclaredField(PublicChild.class, "VALUE", true).getDeclaringClass()); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "s", true)); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "b", true)); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i", true)); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d", true)); assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE", true)); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s", true).getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b", true).getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i", true).getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d", true).getDeclaringClass()); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "VALUE", true)); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s", true).getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b", true).getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "i", true).getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d", true).getDeclaringClass()); }
@Test public void testToStringLang1311() { assertEquals("int[]", TypeUtils.toString(int[].class)); assertEquals("java.lang.Integer[]", TypeUtils.toString(Integer[].class)); final Field stringListField = FieldUtils.getDeclaredField(getClass(), "stringListArray"); assertEquals("java.util.List<java.lang.String>[]", TypeUtils.toString(stringListField.getGenericType())); }
@Test public void testEmptySort() throws IOException, IllegalAccessException { SimpleFeatureSource fsp = getFeatureSource(SystemTestData.PRIMITIVEGEOFEATURE); Query q = new Query(); q.setSortBy(SortBy.UNSORTED); SimpleFeatureCollection fc = fsp.getFeatures(q); // the above call used to add a SortedSimpleFeatureCollection wrapper that in turn killed // some visit optimizations while (fc instanceof DecoratingFeatureCollection) { assertThat(fc, not(instanceOf(SortedSimpleFeatureCollection.class))); Field field = FieldUtils.getDeclaredField(SortedSimpleFeatureCollection.class, "delegate"); field.setAccessible(true); Object delegate = field.get(fc); fc = (SimpleFeatureCollection) delegate; } }
@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 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 } }
/** * Gets an accessible {@link Field} by name respecting scope. Only the specified class will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the Field object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty */ public static Field getDeclaredField(final Class<?> cls, final String fieldName) { return getDeclaredField(cls, fieldName, false); }