/** * Set the hidden private field {@link TreeItem#children} through reflection and hook the hidden * {@link ListChangeListener} in {@link TreeItem#childrenListener} to the list * @param list the list to set */ @SuppressWarnings({ "unchecked", "javadoc" }) protected void setHiddenFieldChildren(ObservableList<TreeItem<T>> list) { ReflectionUtil.setFieldValue(this, "children", list); //$NON-NLS-1$ Object childrenListener = ReflectionUtil.getFieldValue(this, "childrenListener"); //$NON-NLS-1$ list.addListener((ListChangeListener<? super TreeItem<T>>) childrenListener); }
/** * Utility method to set a field to a value. If the field is not accessible, it will be set to be accessible. * @param object Instance in which the value should be set * @param name Name of the field who's value should be set * @param value The value to be set */ public static void setFieldValue(Object object, String name, Object value) { try { Field field = getField(object.getClass(), name); if (!field.isAccessible()) { field.setAccessible(true); } field.set(object, value); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException("Could not set field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$ } }
/** * Utility method to read a field value. If the field is not accessible, it will be set to be accessible. * @param object Instance in which the value should be read * @param name Name of the field who's value should be read * @return The value of the field */ public static Object getFieldValue(Object object, String name) { try { Field field = getField(object.getClass(), name); if (!field.isAccessible()) { field.setAccessible(true); } return field.get(object); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException("Could not read field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$ } }
/** * Set the hidden private field {@link TreeItem#children} through reflection and hook the hidden * {@link ListChangeListener} in {@link TreeItem#childrenListener} to the list * @param list the list to set */ @SuppressWarnings({ "unchecked", "javadoc" }) protected void setHiddenFieldChildren(ObservableList<TreeItem<T>> list) { ReflectionUtil.setFieldValue(this, "children", list); //$NON-NLS-1$ Object childrenListener = ReflectionUtil.getFieldValue(this, "childrenListener"); //$NON-NLS-1$ list.addListener((ListChangeListener<? super TreeItem<T>>) childrenListener); }