/** * Writes a {@code public static} {@link Field}. * * @param field * to write * @param value * to set * @throws IllegalArgumentException * if the field is {@code null} or 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 writeStaticField(final Field field, final Object value) throws IllegalAccessException { writeStaticField(field, value, false); }
/** * Writes a named {@code public static} {@link Field}. Superclasses 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 writeStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeStaticField(cls, fieldName, value, false); }
/** * Writes a named {@code static} {@link Field}. Superclasses 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 * {@link java.lang.reflect.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 writeStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException { 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: writeStaticField(field, value, false); }
writeStaticField(field, afterValue = Integer.valueOf(optionValue)); } else if (isIn(type, long.class, Long.class)) { writeStaticField(field, afterValue = Long.valueOf(optionValue)); } else if (isIn(type, boolean.class, Boolean.class)) { writeStaticField(field, afterValue = Boolean.valueOf(optionValue)); } else if (isIn(type, double.class, Double.class)) { writeStaticField(field, afterValue = Double.valueOf(optionValue)); } else if (isIn(type, float.class, Float.class)) { writeStaticField(field, afterValue = Float.valueOf(optionValue)); } else if (isIn(type, byte.class, Byte.class)) { writeStaticField(field, afterValue = Byte.valueOf(optionValue)); } else if (isIn(type, short.class, Short.class)) { writeStaticField(field, afterValue = Short.valueOf(optionValue)); } else { printer.println(format("Options[%s] type[%s] desupported.", optionName, type.getSimpleName())).finish();
@Test public void testWriteNamedStaticField() throws Exception { FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePublic", "new"); assertEquals("new", StaticContainer.mutablePublic); try { FieldUtils.writeStaticField(StaticContainerChild.class, "mutableProtected", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePackage", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new"); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new");
@Test public void testWriteStaticField() throws Exception { Field field = StaticContainer.class.getDeclaredField("mutablePublic"); FieldUtils.writeStaticField(field, "new"); assertEquals("new", StaticContainer.mutablePublic); field = StaticContainer.class.getDeclaredField("mutableProtected"); try { FieldUtils.writeStaticField(field, "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new"); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) {
@Test public void testWriteStaticFieldForceAccess() throws Exception { Field field = StaticContainer.class.getDeclaredField("mutablePublic"); FieldUtils.writeStaticField(field, "new", true); assertEquals("new", StaticContainer.mutablePublic); field = StaticContainer.class.getDeclaredField("mutableProtected"); FieldUtils.writeStaticField(field, "new", true); assertEquals("new", StaticContainer.getMutableProtected()); field = StaticContainer.class.getDeclaredField("mutablePackage"); FieldUtils.writeStaticField(field, "new", true); assertEquals("new", StaticContainer.getMutablePackage()); field = StaticContainer.class.getDeclaredField("mutablePrivate"); FieldUtils.writeStaticField(field, "new", true); assertEquals("new", StaticContainer.getMutablePrivate()); field = StaticContainer.class.getDeclaredField("IMMUTABLE_PUBLIC"); try { FieldUtils.writeStaticField(field, "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(field, "new", true); fail("Expected IllegalAccessException");
@Test public void testWriteNamedStaticFieldForceAccess() throws Exception { FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePublic", "new", true); assertEquals("new", StaticContainer.mutablePublic); FieldUtils.writeStaticField(StaticContainerChild.class, "mutableProtected", "new", true); assertEquals("new", StaticContainer.getMutableProtected()); FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePackage", "new", true); assertEquals("new", StaticContainer.getMutablePackage()); FieldUtils.writeStaticField(StaticContainerChild.class, "mutablePrivate", "new", true); assertEquals("new", StaticContainer.getMutablePrivate()); try { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PUBLIC", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PROTECTED", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PACKAGE", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) { FieldUtils.writeStaticField(StaticContainerChild.class, "IMMUTABLE_PRIVATE", "new", true); fail("Expected IllegalAccessException"); } catch (final IllegalAccessException e) {
public static void resetClock(final Class<?> clazz) throws IllegalAccessException { System.clearProperty(ClockFactory.PROPERTY_NAME); final Field field = FieldUtils.getField(clazz, "CLOCK", true); FieldUtils.removeFinalModifier(field, true); FieldUtils.writeStaticField(field, ClockFactory.getClock(), false); }
@Test public void testWriteDeclaredNamedStaticField() throws Exception { FieldUtils.writeStaticField(StaticContainer.class, "mutablePublic", "new"); assertEquals("new", StaticContainer.mutablePublic); try {
/** * Writes a {@code public static} {@link Field}. * * @param field * to write * @param value * to set * @throws IllegalArgumentException * if the field is {@code null} or 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 writeStaticField(final Field field, final Object value) throws IllegalAccessException { writeStaticField(field, value, false); }
/** * Writes a {@code public static} {@link Field}. * * @param field * to write * @param value * to set * @throws IllegalArgumentException * if the field is {@code null} or 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 writeStaticField(final Field field, final Object value) throws IllegalAccessException { writeStaticField(field, value, false); }
/** * Writes a {@code public static} {@link Field}. * * @param field * to write * @param value * to set * @throws IllegalArgumentException * if the field is {@code null} or 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 writeStaticField(final Field field, final Object value) throws IllegalAccessException { writeStaticField(field, value, false); }
/** * Writes a named {@code public static} {@link Field}. Superclasses 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 writeStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeStaticField(cls, fieldName, value, false); }
/** * Writes a named {@code public static} {@link Field}. Superclasses 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 writeStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeStaticField(cls, fieldName, value, false); }
/** * Writes a named {@code public static} {@link Field}. Superclasses 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 writeStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { writeStaticField(cls, fieldName, value, false); }
private void fixTikaInitWarning() { // A check for Tesseract OCR parser is done the first time a Tika // parser is used. We remove this check since we manage Tesseract OCR // via Importer config only. try { FieldUtils.writeStaticField( TesseractOCRParser.class, "HAS_WARNED", true, true); } catch (IllegalAccessException | IllegalArgumentException e) { LOG.warn("Could not disable invalid Tessaract OCR warning. " + "If you see such warning, you can ignore."); } // A check for SQL-Lite is also done and we do not want it. try { FieldUtils.writeStaticField( SQLite3Parser.class, "HAS_WARNED", true, true); } catch (IllegalAccessException | IllegalArgumentException e) { LOG.warn("Could not disable \"sqlite-jdbc\" warning. " + "If you see such warning, you can ignore."); } }
/** * Writes a named {@code static} {@link Field}. Superclasses 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 * {@link java.lang.reflect.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 writeStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException { 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: writeStaticField(field, value, false); }
/** * Writes a named {@code static} {@link Field}. Superclasses 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 * {@link java.lang.reflect.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 writeStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException { 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: writeStaticField(field, value, false); }
/** * Writes into the field the given value. * @param object the given value to write. * @return the previous value. * @throws Exception */ public T write(T object) throws Exception { T previousValue = read(); ObjectField finalObjectField = getFinalObjectField(); if (finalObjectField.getInstanceOrClass() instanceof Class) { FieldUtils.writeStaticField((Class) finalObjectField.getInstanceOrClass(), finalObjectField.getFieldName(), object, true); } else { FieldUtils.writeField(finalObjectField.getInstanceOrClass(), finalObjectField.getFieldName(), object, true); } return previousValue; } }