Skip to content

Commit

Permalink
fix: AbstractDataProvider serialization (#14870) (#14873)
Browse files Browse the repository at this point in the history
Replace lambda with anonymous inner class in AbstractDataProvider to fix serialization issue(s).
Test available in flow-components project since the issue only happens when the data provider is used with a grid.

Fixes #14654

Co-authored-by: Teppo Kurki <teppo.kurki@vaadin.com>
  • Loading branch information
vaadin-bot and tepi committed Oct 18, 2022
1 parent c7ea41c commit b0d2f51
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package com.vaadin.flow.data.provider;

import com.vaadin.flow.data.provider.DataChangeEvent.DataRefreshEvent;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.shared.Registration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.function.Consumer;

import com.vaadin.flow.data.provider.DataChangeEvent.DataRefreshEvent;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.shared.Registration;

/**
* Abstract data provider implementation which takes care of refreshing data
* from the underlying data provider.
Expand Down Expand Up @@ -56,9 +56,8 @@ public DataListenerWrapper(SerializableConsumer<?> listener) {
public Registration addDataProviderListener(
DataProviderListener<T> listener) {
// Using an anonymous class instead of lambda or method reference to
// prevent potential
// self reference serialization issues when clients holds a reference
// to the Registration instance returned by this method
// prevent potential self reference serialization issues when clients
// hold a reference to the Registration instance returned by this method
SerializableConsumer<DataChangeEvent> consumer = new SerializableConsumer<DataChangeEvent>() {
@Override
public void accept(DataChangeEvent dataChangeEvent) {
Expand Down Expand Up @@ -105,7 +104,19 @@ protected <E> Registration addListener(Class<E> eventType,

DataListenerWrapper wrapper = new DataListenerWrapper(method);

wrapper.registration = Registration.addAndRemove(list, wrapper);
final Registration registration = Registration.addAndRemove(list,
wrapper);

// Using an anonymous class instead of lambda or method reference to
// prevent potential self reference serialization issues when clients
// hold a reference to the Registration instance returned by this method
wrapper.registration = new Registration() {
@Override
public void remove() {
registration.remove();
}
};

return wrapper.registration;
}

Expand Down

0 comments on commit b0d2f51

Please sign in to comment.