Ignore:
Timestamp:
2022-05-19T20:09:13+02:00 (3 years ago)
Author:
taylor.smock
Message:

Fix #21558: IOOBE in NotesDialog$NoteTableModel.getElementAt

This is caused when a user has a filter in the note panel,
and performs an action that removes the last note from
the panel.

Example:

  1. User filters on "open"
  2. User closes all notes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java

    r17275 r18454  
    143143    }
    144144
     145    /**
     146     * Get the result field for an extended dialog instance
     147     * @param instance The instance to get the result field for
     148     * @return The result field. May be private.
     149     * @throws NoSuchFieldException If the field cannot be found. Should never be thrown.
     150     */
     151    protected Field getResultField(ExtendedDialog instance) throws NoSuchFieldException {
     152        // Note that subclasses of ExtendedDialogMocker will not have "result" as a declared field.
     153        // Iterate up the chain until we get to a field that has "result" as a declared field.
     154        // Only reason for this is just in case someone overrides the logic in ExtendedDialog.
     155        Class<?> clazz = instance.getClass();
     156        Field resultField = null;
     157        // Store the exception, if any
     158        NoSuchFieldException noSuchFieldException = null;
     159        while (!Object.class.equals(clazz) && resultField == null) {
     160            try {
     161                resultField = clazz.getDeclaredField("result");
     162            } catch (NoSuchFieldException e) {
     163                clazz = instance.getClass().getSuperclass();
     164                // Only save the first exception
     165                if (noSuchFieldException == null) {
     166                    noSuchFieldException = e;
     167                }
     168            }
     169        }
     170        if (resultField == null) {
     171            throw noSuchFieldException;
     172        }
     173        return resultField;
     174    }
     175
    145176    @Mock
    146177    private void setupDialog(final Invocation invocation) {
     
    160191                final int mockResult = this.getMockResult(instance);
    161192                // TODO check validity of mockResult?
    162                 Field resultField = instance.getClass().getDeclaredField("result");
     193                final Field resultField = this.getResultField(instance);
    163194                resultField.setAccessible(true);
    164195                resultField.set(instance, mockResult);
Note: See TracChangeset for help on using the changeset viewer.