removed reflection access

This commit is contained in:
Robinson 2021-05-17 00:13:09 +02:00
parent 6a41f7cec8
commit 092b908aeb
3 changed files with 134 additions and 144 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2010 Martin Grotzke * Copyright 2021 dorkbox, llc
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -8,24 +8,22 @@
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*
*/ */
package dorkbox.serializers; package dorkbox.serializers;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer; import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
/** /**
* A kryo {@link Serializer} for lists created via {@link Arrays#asList(Object...)}. * A kryo {@link Serializer} for lists created via {@link Arrays#asList(Object...)}.
* <p> * <p>
@ -35,85 +33,102 @@ import java.util.List;
* *
* @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a> * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
*/ */
public class ArraysAsListSerializer extends Serializer<List<?>> { public
class ArraysAsListSerializer extends Serializer<List<?>> {
private Field _arrayField; public
ArraysAsListSerializer() {
public ArraysAsListSerializer() {
try {
_arrayField = Class.forName( "java.util.Arrays$ArrayList" ).getDeclaredField( "a" );
_arrayField.setAccessible( true );
} catch ( final Exception e ) {
throw new RuntimeException( e );
}
} }
@Override @Override
public List<?> read(final Kryo kryo, final Input input, final Class<? extends List<?>> type) { public
List<?> read(final Kryo kryo, final Input input, final Class<? extends List<?>> type) {
final int length = input.readInt(true); final int length = input.readInt(true);
Class<?> componentType = kryo.readClass( input ).getType(); Class<?> componentType = kryo.readClass(input)
.getType();
if (componentType.isPrimitive()) { if (componentType.isPrimitive()) {
componentType = getPrimitiveWrapperClass(componentType); componentType = getPrimitiveWrapperClass(componentType);
} }
try { try {
final Object items = Array.newInstance( componentType, length ); final Object items = Array.newInstance(componentType, length);
for( int i = 0; i < length; i++ ) { for (int i = 0; i < length; i++) {
Array.set(items, i, kryo.readClassAndObject( input )); Array.set(items, i, kryo.readClassAndObject(input));
} }
return Arrays.asList( (Object[])items ); return Arrays.asList((Object[]) items);
} catch ( final Exception e ) { } catch (final Exception e) {
throw new RuntimeException( e ); throw new RuntimeException(e);
} }
} }
@Override @Override
public void write(final Kryo kryo, final Output output, final List<?> obj) { public
void write(final Kryo kryo, final Output output, final List<?> obj) {
try { try {
final Object[] array = (Object[]) _arrayField.get( obj ); int size = obj.size();
output.writeInt(array.length, true); output.writeInt(size, true);
final Class<?> componentType = array.getClass().getComponentType();
kryo.writeClass( output, componentType ); if (size == 0) {
for( final Object item : array ) { final Class<?> componentType = obj.toArray()
kryo.writeClassAndObject( output, item ); .getClass()
.getComponentType();
kryo.writeClass(output, componentType);
} }
} catch ( final RuntimeException e ) { else {
// Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write... kryo.writeClass(output,
// handles SerializationException specifically (resizing the buffer)... obj.get(0)
throw e; .getClass());
} catch ( final Exception e ) { for (final Object item : obj) {
throw new RuntimeException( e ); kryo.writeClassAndObject(output, item);
}
}
} catch (final RuntimeException e) {
// Don't eat and wrap RuntimeExceptions because the ObjectBuffer.write...
// handles SerializationException specifically (resizing the buffer)...
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
} }
} }
@Override @Override
public List<?> copy(Kryo kryo, List<?> original) { public
List<?> copy(Kryo kryo, List<?> original) {
try { try {
final Object[] array = (Object[]) _arrayField.get(original); Object[] object = original.toArray();
kryo.reference(array); kryo.reference(object);
Object[] arrayCopy = kryo.copy(array); Object[] arrayCopy = kryo.copy(object);
return Arrays.asList(arrayCopy); return Arrays.asList(arrayCopy);
} catch (final Exception e) { } catch (final Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private static Class<?> getPrimitiveWrapperClass(final Class<?> c) { private static
Class<?> getPrimitiveWrapperClass(final Class<?> c) {
if (c.isPrimitive()) { if (c.isPrimitive()) {
if (c.equals(Long.TYPE)) { if (c.equals(Long.TYPE)) {
return Long.class; return Long.class;
} else if (c.equals(Integer.TYPE)) { }
else if (c.equals(Integer.TYPE)) {
return Integer.class; return Integer.class;
} else if (c.equals(Double.TYPE)) { }
else if (c.equals(Double.TYPE)) {
return Double.class; return Double.class;
} else if (c.equals(Float.TYPE)) { }
else if (c.equals(Float.TYPE)) {
return Float.class; return Float.class;
} else if (c.equals(Boolean.TYPE)) { }
else if (c.equals(Boolean.TYPE)) {
return Boolean.class; return Boolean.class;
} else if (c.equals(Character.TYPE)) { }
else if (c.equals(Character.TYPE)) {
return Character.class; return Character.class;
} else if (c.equals(Short.TYPE)) { }
else if (c.equals(Short.TYPE)) {
return Short.class; return Short.class;
} else if (c.equals(Byte.TYPE)) { }
else if (c.equals(Byte.TYPE)) {
return Byte.class; return Byte.class;
} }
} }

View File

@ -16,95 +16,76 @@
*/ */
package dorkbox.serializers; package dorkbox.serializers;
import static com.esotericsoftware.minlog.Log.TRACE;
import static com.esotericsoftware.minlog.Log.trace;
import java.lang.reflect.Field;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer; import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
/** /**
* A serializer for {@link EnumMap}s. * A serializer for {@link EnumMap}s.
* *
* @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a> * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
*/ */
public class EnumMapSerializer extends Serializer<EnumMap<? extends Enum<?>, ?>> { public
class EnumMapSerializer extends Serializer<EnumMap<? extends Enum<?>, ?>> {
private static final Field TYPE_FIELD;
static {
try {
TYPE_FIELD = EnumMap.class.getDeclaredField( "keyType" );
TYPE_FIELD.setAccessible( true );
} catch ( final Exception e ) {
throw new RuntimeException( "The EnumMap class seems to have changed, could not access expected field.", e );
}
}
// Workaround reference reading, this should be removed sometimes. See also static {
// https://groups.google.com/d/msg/kryo-users/Eu5V4bxCfws/k-8UQ22y59AJ }
private static final Object FAKE_REFERENCE = new Object();
@Override @Override
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
public EnumMap<? extends Enum<?>, ?> copy(final Kryo kryo, final EnumMap<? extends Enum<?>, ?> original) { public
EnumMap<? extends Enum<?>, ?> copy(final Kryo kryo, final EnumMap<? extends Enum<?>, ?> original) {
// Make a shallow copy to copy the private key type of the original map without using reflection. // Make a shallow copy to copy the private key type of the original map without using reflection.
// This will work for empty original maps as well. // This will work for empty original maps as well.
final EnumMap copy = new EnumMap(original); final EnumMap copy = new EnumMap(original);
for (final Map.Entry entry : original.entrySet()) { for (final Map.Entry entry : original.entrySet()) {
copy.put((Enum)entry.getKey(), kryo.copy(entry.getValue())); copy.put((Enum) entry.getKey(), kryo.copy(entry.getValue()));
} }
return copy; return copy;
}
@SuppressWarnings( { "unchecked", "rawtypes" } )
private EnumMap<? extends Enum<?>, ?> create(final Kryo kryo, final Input input,
final Class<? extends EnumMap<? extends Enum<?>, ?>> type) {
final Class<? extends Enum<?>> keyType = kryo.readClass( input ).getType();
return new EnumMap( keyType );
} }
@Override @Override
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({"rawtypes", "unchecked"})
public EnumMap<? extends Enum<?>, ?> read(final Kryo kryo, final Input input, public
final Class<? extends EnumMap<? extends Enum<?>, ?>> type) { EnumMap<? extends Enum<?>, ?> read(final Kryo kryo, final Input input, final Class<? extends EnumMap<? extends Enum<?>, ?>> type) {
kryo.reference(FAKE_REFERENCE); final Class<? extends Enum<?>> keyType = kryo.readClass(input)
final EnumMap<? extends Enum<?>, ?> result = create(kryo, input, type); .getType();
final Class<Enum<?>> keyType = getKeyType( result ); final EnumMap<? extends Enum<?>, ?> result = new EnumMap(keyType);
final Enum<?>[] enumConstants = keyType.getEnumConstants(); final Enum<?>[] enumConstants = keyType.getEnumConstants();
final EnumMap rawResult = result; final EnumMap rawResult = result;
final int size = input.readInt(true); final int size = input.readInt(true);
for ( int i = 0; i < size; i++ ) { for (int i = 0; i < size; i++) {
final int ordinal = input.readVarInt(true); final int ordinal = input.readVarInt(true);
final Enum<?> key = enumConstants[ordinal]; final Enum<?> key = enumConstants[ordinal];
final Object value = kryo.readClassAndObject( input ); final Object value = kryo.readClassAndObject(input);
rawResult.put( key, value ); rawResult.put(key, value);
} }
return result; return result;
} }
@Override @Override
public void write(final Kryo kryo, final Output output, final EnumMap<? extends Enum<?>, ?> map) { public
kryo.writeClass( output, getKeyType( map ) ); void write(final Kryo kryo, final Output output, final EnumMap<? extends Enum<?>, ?> map) {
output.writeInt(map.size(), true); if (map.isEmpty()) {
for ( final Map.Entry<? extends Enum<?>,?> entry : map.entrySet() ) { throw new KryoException("An EnumMap must not be empty to be serialized, the type can not be safely inferred.");
output.writeVarInt(entry.getKey().ordinal(), true); } else {
kryo.writeClassAndObject(output, entry.getValue()); @SuppressWarnings("unchecked")
} Class<Enum<?>> keyType = (Class<Enum<?>>) map.keySet()
if ( TRACE ) trace( "kryo", "Wrote EnumMap: " + map ); .iterator()
} .next()
.getDeclaringClass();
@SuppressWarnings("unchecked") kryo.writeClass(output, keyType);
private Class<Enum<?>> getKeyType( final EnumMap<?, ?> map ) { output.writeInt(map.size(), true);
try { for (final Map.Entry<? extends Enum<?>, ?> entry : map.entrySet()) {
return (Class<Enum<?>>)TYPE_FIELD.get( map ); output.writeVarInt(entry.getKey()
} catch ( final Exception e ) { .ordinal(), true);
throw new RuntimeException( "Could not access keys field.", e ); kryo.writeClassAndObject(output, entry.getValue());
}
} }
} }
} }

View File

@ -16,69 +16,63 @@
*/ */
package dorkbox.serializers; package dorkbox.serializers;
import static com.esotericsoftware.minlog.Log.TRACE;
import static com.esotericsoftware.minlog.Log.trace;
import java.lang.reflect.Field;
import java.util.EnumSet; import java.util.EnumSet;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.Serializer; import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
/** /**
* A serializer for {@link EnumSet}s. * A serializer for {@link EnumSet}s.
*
* @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
*/ */
@SuppressWarnings( { "unchecked", "rawtypes" } ) @SuppressWarnings({"unchecked", "rawtypes"})
public class EnumSetSerializer extends Serializer<EnumSet<? extends Enum<?>>> { public
class EnumSetSerializer extends Serializer<EnumSet<? extends Enum<?>>> {
private static final Field TYPE_FIELD;
static {
try {
TYPE_FIELD = EnumSet.class.getDeclaredField( "elementType" );
TYPE_FIELD.setAccessible( true );
} catch ( final Exception e ) {
throw new RuntimeException( "The EnumSet class seems to have changed, could not access expected field.", e );
}
}
@Override @Override
public EnumSet<? extends Enum<?>> copy (final Kryo kryo, final EnumSet<? extends Enum<?>> original) { public
EnumSet<? extends Enum<?>> copy(final Kryo kryo, final EnumSet<? extends Enum<?>> original) {
return original.clone(); return original.clone();
} }
@Override @Override
public EnumSet read(final Kryo kryo, final Input input, final Class<? extends EnumSet<? extends Enum<?>>> type) { public
final Class<Enum> elementType = kryo.readClass( input ).getType(); EnumSet read(final Kryo kryo, final Input input, final Class<? extends EnumSet<? extends Enum<?>>> type) {
final EnumSet result = EnumSet.noneOf( elementType ); final Class<Enum> elementType = kryo.readClass(input)
.getType();
final EnumSet result = EnumSet.noneOf(elementType);
final int size = input.readInt(true); final int size = input.readInt(true);
final Enum<?>[] enumConstants = elementType.getEnumConstants(); final Enum<?>[] enumConstants = elementType.getEnumConstants();
for ( int i = 0; i < size; i++ ) { for (int i = 0; i < size; i++) {
result.add( enumConstants[input.readInt(true)] ); result.add(enumConstants[input.readInt(true)]);
} }
return result; return result;
} }
@Override @Override
public void write(final Kryo kryo, final Output output, final EnumSet<? extends Enum<?>> set) { public
kryo.writeClass( output, getElementType( set ) ); void write(final Kryo kryo, final Output output, final EnumSet<? extends Enum<?>> set) {
output.writeInt( set.size(), true ); if (set.isEmpty()) {
for (final Enum item : set) { EnumSet<? extends Enum<?>> tmp = EnumSet.complementOf(set);
output.writeInt(item.ordinal(), true); if (tmp.isEmpty()) throw new KryoException("An EnumSet must have a defined Enum to be serialized.");
Class<Enum<?>> type = (Class<Enum<?>>) tmp.iterator()
.next()
.getDeclaringClass();
kryo.writeClass(output, type);
output.writeInt(0, true);
} }
else {
Class<Enum<?>> type = (Class<Enum<?>>) set.iterator()
.next()
.getDeclaringClass();
kryo.writeClass(output, type);
output.writeInt(set.size(), true);
if ( TRACE ) trace( "kryo", "Wrote EnumSet: " + set ); for (final Enum item : set) {
} output.writeInt(item.ordinal(), true);
}
private Class<? extends Enum<?>> getElementType( final EnumSet<? extends Enum<?>> set ) {
try {
return (Class)TYPE_FIELD.get( set );
} catch ( final Exception e ) {
throw new RuntimeException( "Could not access keys field.", e );
} }
} }
} }