Skip to content

Commit

Permalink
Initialize reflection methods on first use
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb committed Aug 15, 2024
1 parent 2305f58 commit 3a298ef
Showing 1 changed file with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions src/main/java/org/jabref/gui/util/FilteredListProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,23 @@

public class FilteredListProxy {
private static final Logger LOGGER = LoggerFactory.getLogger(FilteredListProxy.class);
private static final Method BEGIN_CHANGE_METHOD;
private static final Method END_CHANGE_METHOD;
private static final Method NEXT_ADD_METHOD;
private static final Method NEXT_UPDATE_METHOD;
private static final Method NEXT_REMOVE_METHOD;
private static final Method REFILTER_METHOD;
private static final Method ENSURE_SIZE_METHOD;
private static final Method GET_PREDICATE_IMPL_METHOD;
private static final Field FILTERED_FIELD;
private static final Field SIZE_FIELD;

static {
try {
BEGIN_CHANGE_METHOD = ObservableListBase.class.getDeclaredMethod("beginChange");
END_CHANGE_METHOD = ObservableListBase.class.getDeclaredMethod("endChange");
NEXT_ADD_METHOD = ObservableListBase.class.getDeclaredMethod("nextAdd", int.class, int.class);
NEXT_UPDATE_METHOD = ObservableListBase.class.getDeclaredMethod("nextUpdate", int.class);
NEXT_REMOVE_METHOD = ObservableListBase.class.getDeclaredMethod("nextRemove", int.class, Object.class);

REFILTER_METHOD = FilteredList.class.getDeclaredMethod("refilter");
ENSURE_SIZE_METHOD = FilteredList.class.getDeclaredMethod("ensureSize", int.class);
GET_PREDICATE_IMPL_METHOD = FilteredList.class.getDeclaredMethod("getPredicateImpl");

FILTERED_FIELD = FilteredList.class.getDeclaredField("filtered");
SIZE_FIELD = FilteredList.class.getDeclaredField("size");

BEGIN_CHANGE_METHOD.setAccessible(true);
END_CHANGE_METHOD.setAccessible(true);
NEXT_ADD_METHOD.setAccessible(true);
NEXT_UPDATE_METHOD.setAccessible(true);
NEXT_REMOVE_METHOD.setAccessible(true);

REFILTER_METHOD.setAccessible(true);
ENSURE_SIZE_METHOD.setAccessible(true);
GET_PREDICATE_IMPL_METHOD.setAccessible(true);

FILTERED_FIELD.setAccessible(true);
SIZE_FIELD.setAccessible(true);
} catch (NoSuchMethodException | NoSuchFieldException e) {
throw new RuntimeException("Failed to initialize reflective methods or fields", e);
}
}
private static boolean initialized = false;
private static Method BEGIN_CHANGE_METHOD;
private static Method END_CHANGE_METHOD;
private static Method NEXT_ADD_METHOD;
private static Method NEXT_UPDATE_METHOD;
private static Method NEXT_REMOVE_METHOD;
private static Method REFILTER_METHOD;
private static Method ENSURE_SIZE_METHOD;
private static Method GET_PREDICATE_IMPL_METHOD;
private static Field FILTERED_FIELD;
private static Field SIZE_FIELD;

public static void refilterListReflection(FilteredList<BibEntryTableViewModel> filteredList) {
try {
if (!initialized) {
initReflection();
}
REFILTER_METHOD.invoke(filteredList);
} catch (Exception e) {
LOGGER.warn("Could not refilter list", e);
Expand All @@ -69,6 +41,9 @@ public static void refilterListReflection(FilteredList<BibEntryTableViewModel> f

public static void refilterListReflection(FilteredList<BibEntryTableViewModel> filteredList, int sourceFrom, int sourceTo) {
try {
if (!initialized) {
initReflection();
}
if (sourceFrom < 0 || sourceTo > filteredList.getSource().size() || sourceFrom > sourceTo) {
throw new IndexOutOfBoundsException();
}
Expand Down Expand Up @@ -116,4 +91,34 @@ public static void refilterListReflection(FilteredList<BibEntryTableViewModel> f
LOGGER.warn("Could not refilter list", e);
}
}

private static void initReflection() throws NoSuchMethodException, NoSuchFieldException {
BEGIN_CHANGE_METHOD = ObservableListBase.class.getDeclaredMethod("beginChange");
END_CHANGE_METHOD = ObservableListBase.class.getDeclaredMethod("endChange");
NEXT_ADD_METHOD = ObservableListBase.class.getDeclaredMethod("nextAdd", int.class, int.class);
NEXT_UPDATE_METHOD = ObservableListBase.class.getDeclaredMethod("nextUpdate", int.class);
NEXT_REMOVE_METHOD = ObservableListBase.class.getDeclaredMethod("nextRemove", int.class, Object.class);

REFILTER_METHOD = FilteredList.class.getDeclaredMethod("refilter");
ENSURE_SIZE_METHOD = FilteredList.class.getDeclaredMethod("ensureSize", int.class);
GET_PREDICATE_IMPL_METHOD = FilteredList.class.getDeclaredMethod("getPredicateImpl");

FILTERED_FIELD = FilteredList.class.getDeclaredField("filtered");
SIZE_FIELD = FilteredList.class.getDeclaredField("size");

BEGIN_CHANGE_METHOD.setAccessible(true);
END_CHANGE_METHOD.setAccessible(true);
NEXT_ADD_METHOD.setAccessible(true);
NEXT_UPDATE_METHOD.setAccessible(true);
NEXT_REMOVE_METHOD.setAccessible(true);

REFILTER_METHOD.setAccessible(true);
ENSURE_SIZE_METHOD.setAccessible(true);
GET_PREDICATE_IMPL_METHOD.setAccessible(true);

FILTERED_FIELD.setAccessible(true);
SIZE_FIELD.setAccessible(true);

initialized = true;
}
}

0 comments on commit 3a298ef

Please sign in to comment.