/** * Writes a named {@code public 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 * @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 {@code public} or is {@code final} */ public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeDeclaredStaticField(cls, fieldName, value, false); }
assertEquals("new", StaticContainer.mutablePublic); try { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutableProtected", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePackage", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) {
@Test public void testWriteDeclaredNamedStaticFieldForceAccess() throws Exception { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePublic", "new", true); assertEquals("new", StaticContainer.mutablePublic); FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutableProtected", "new", true); assertEquals("new", StaticContainer.getMutableProtected()); FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePackage", "new", true); assertEquals("new", StaticContainer.getMutablePackage()); FieldUtils.writeDeclaredStaticField(StaticContainer.class, "mutablePrivate", "new", true); assertEquals("new", StaticContainer.getMutablePrivate()); try { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PUBLIC", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PROTECTED", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PACKAGE", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeDeclaredStaticField(StaticContainer.class, "IMMUTABLE_PRIVATE", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) {
/** * Writes a named {@code public 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 * @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 {@code public} or is {@code final} */ public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeDeclaredStaticField(cls, fieldName, value, false); }
/** * Writes a named {@code public 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 * @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 {@code public} or is {@code final} */ public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeDeclaredStaticField(cls, fieldName, value, false); }
/** * Writes a named {@code public 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 * @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 {@code public} or is {@code final} */ public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeDeclaredStaticField(cls, fieldName, value, false); }