Assume serialization proxy 47/102047/13
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 22:21:15 +0000 (00:21 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 17 Aug 2022 06:39:59 +0000 (08:39 +0200)
Ditch reflexive access in favor of the (smaller) serialization proxy.
Since we have introduced writeReplace() before, we can now assume fields
are not touched. Ditches troublesome reflection.

Change-Id: I837fe0379c77542f2de2ea54d6e563d491f1a8db
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/ImmutableOffsetMap.java

index a04166477c0c3601f2847c1b036cf144eeba119e..6f9f137b3d3cd3bbdf78ca6c50e9a3b35683959d 100644 (file)
@@ -14,18 +14,13 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.annotations.Beta;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.UnmodifiableIterator;
-import java.io.IOException;
-import java.io.ObjectInputStream;
 import java.io.Serial;
 import java.io.Serializable;
-import java.lang.reflect.Field;
 import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.AbstractSet;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import org.eclipse.jdt.annotation.NonNull;
@@ -58,12 +53,6 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
             return MutableOffsetMap.orderedCopyOf(this);
         }
 
-        @Override
-        void setFields(final List<K> keys, final V[] values) throws IOException {
-            setField(this, OFFSETS_FIELD, OffsetMapCache.orderedOffsets(keys));
-            setField(this, ARRAY_FIELD, values);
-        }
-
         @Override
         Object writeReplace() {
             return new OIOMv1(this);
@@ -83,14 +72,6 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
             return MutableOffsetMap.unorderedCopyOf(this);
         }
 
-        @Override
-        void setFields(final List<K> keys, final V[] values) throws IOException {
-            final Map<K, Integer> newOffsets = OffsetMapCache.unorderedOffsets(keys);
-
-            setField(this, OFFSETS_FIELD, newOffsets);
-            setField(this, ARRAY_FIELD, OffsetMapCache.adjustedArray(newOffsets, keys, values));
-        }
-
         @Override
         Object writeReplace() {
             return new UIOMv1(this);
@@ -100,8 +81,8 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
     @Serial
     private static final long serialVersionUID = 1L;
 
-    private final transient @NonNull ImmutableMap<K, Integer> offsets;
-    private final transient @NonNull V[] objects;
+    private final @NonNull ImmutableMap<K, Integer> offsets;
+    private final @NonNull V[] objects;
     private transient int hashCode;
 
     /**
@@ -120,8 +101,6 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
     @Override
     public abstract @NonNull MutableOffsetMap<K, V> toModifiableMap();
 
-    abstract void setFields(List<K> keys, V[] values) throws IOException;
-
     /**
      * Create an {@link ImmutableOffsetMap} as a copy of an existing map. This is actually not completely true, as this
      * method returns an {@link ImmutableMap} for empty and singleton inputs, as those are more memory-efficient. This
@@ -382,45 +361,4 @@ public abstract sealed class ImmutableOffsetMap<K, V> implements UnmodifiableMap
 
     @Serial
     abstract Object writeReplace();
-
-    // FIXME: this is ugly, use an Externalizable proxy
-    private static final Field OFFSETS_FIELD = fieldFor("offsets");
-    private static final Field ARRAY_FIELD = fieldFor("objects");
-
-    private static @NonNull Field fieldFor(final @NonNull String name) {
-        final Field f;
-        try {
-            f = ImmutableOffsetMap.class.getDeclaredField(name);
-        } catch (NoSuchFieldException | SecurityException e) {
-            throw new IllegalStateException("Failed to lookup field " + name, e);
-        }
-
-        f.setAccessible(true);
-        return f;
-    }
-
-    private static void setField(final @NonNull ImmutableOffsetMap<?, ?> map, final @NonNull Field field,
-            final Object value) throws IOException {
-        try {
-            field.set(map, value);
-        } catch (IllegalArgumentException | IllegalAccessException e) {
-            throw new IOException("Failed to set field " + field, e);
-        }
-    }
-
-    @Serial
-    @SuppressWarnings("unchecked")
-    private void readObject(final @NonNull ObjectInputStream in) throws IOException, ClassNotFoundException {
-        final int s = in.readInt();
-
-        final List<K> keys = new ArrayList<>(s);
-        final V[] values = (V[]) new Object[s];
-
-        for (int i = 0; i < s; ++i) {
-            keys.add((K)in.readObject());
-            values[i] = (V)in.readObject();
-        }
-
-        setFields(keys, values);
-    }
 }