From a0c9384abcdcab15c370ab6f7553c0b8a80fb74f Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Sun, 21 Jan 2024 20:22:36 +0000 Subject: [PATCH 1/9] feat: prevent serialisation of private component fields --- .../typeHandling/TypeHandlerLibraryImpl.java | 5 + .../ComponentTypeHandlerFactory.java | 108 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java index d8b0d0d0f9c..d27a756ffbe 100644 --- a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java +++ b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java @@ -3,6 +3,7 @@ package org.terasology.engine.persistence.typeHandling; +import com.google.common.collect.Maps; import org.joml.Quaternionf; import org.joml.Quaternionfc; import org.joml.Vector2f; @@ -31,6 +32,7 @@ import org.terasology.engine.persistence.typeHandling.extensionTypes.UITextureRegionTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.AssetTypeHandlerFactory; import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.ComponentClassTypeHandlerFactory; +import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.ComponentTypeHandlerFactory; import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.TextureRegionAssetTypeHandlerFactory; import org.terasology.engine.persistence.typeHandling.mathTypes.AABBfTypeHandler; import org.terasology.engine.persistence.typeHandling.mathTypes.AABBiTypeHandler; @@ -72,6 +74,7 @@ import org.terasology.persistence.typeHandling.TypeHandlerLibrary; import org.terasology.persistence.typeHandling.reflection.SerializationSandbox; import org.terasology.reflection.TypeRegistry; +import org.terasology.reflection.reflect.ConstructorLibrary; /** * A library of type handlers. This is used for the construction of class metadata. This library should be initialised @@ -159,6 +162,8 @@ private static void populateWithDefaultHandlers(TypeHandlerLibrary serialization serializationLibrary.addTypeHandler(Quaternionfc.class, new QuaternionfcTypeHandler()); serializationLibrary.addTypeHandler(IntegerRange.class, new IntegerRangeHandler()); + + serializationLibrary.addTypeHandlerFactory(new ComponentTypeHandlerFactory(new ConstructorLibrary(Maps.newHashMap()))); } } diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java new file mode 100644 index 00000000000..d4f876e677d --- /dev/null +++ b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java @@ -0,0 +1,108 @@ +// Copyright 2024 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 + +package org.terasology.engine.persistence.typeHandling.extensionTypes.factories; + +import com.google.common.collect.Maps; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.terasology.gestalt.entitysystem.component.Component; +import org.terasology.persistence.typeHandling.TypeHandler; +import org.terasology.persistence.typeHandling.TypeHandlerContext; +import org.terasology.persistence.typeHandling.TypeHandlerFactory; +import org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler; +import org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler; +import org.terasology.reflection.ReflectionUtil; +import org.terasology.reflection.TypeInfo; +import org.terasology.reflection.reflect.ConstructorLibrary; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Map; +import java.util.Optional; + +/** + * A {@link TypeHandlerFactory} for serialising component types. + * It is similar to {@link org.terasology.persistence.typeHandling.coreTypes.factories.ObjectFieldMapTypeHandlerFactory} + * but does not allow serialising private fields. + */ +public class ComponentTypeHandlerFactory implements TypeHandlerFactory { + private Logger logger = LoggerFactory.getLogger(ComponentTypeHandlerFactory.class); + + private ConstructorLibrary constructorLibrary; + + public ComponentTypeHandlerFactory(ConstructorLibrary constructorLibrary) { + this.constructorLibrary = constructorLibrary; + } + + @Override + public Optional> create(TypeInfo typeInfo, TypeHandlerContext context) { + Class typeClass = typeInfo.getRawType(); + if (!Component.class.isAssignableFrom(typeClass)) { + return Optional.empty(); + } + + if (!Modifier.isAbstract(typeClass.getModifiers()) + && !typeClass.isLocalClass() + && !(typeClass.isMemberClass() && !Modifier.isStatic(typeClass.getModifiers()))) { + Map> fieldTypeHandlerMap = Maps.newLinkedHashMap(); + + getResolvedFields(typeInfo).forEach( + (field, fieldType) -> { + Optional> declaredFieldTypeHandler = + context.getTypeHandlerLibrary().getTypeHandler(fieldType); + + TypeInfo fieldTypeInfo = TypeInfo.of(fieldType); + + fieldTypeHandlerMap.put( + field, + new RuntimeDelegatingTypeHandler( + declaredFieldTypeHandler.orElse(null), + fieldTypeInfo, + context + ) + ); + } + ); + + ObjectFieldMapTypeHandler mappedHandler = + new ObjectFieldMapTypeHandler<>(constructorLibrary.get(typeInfo), fieldTypeHandlerMap); + + return Optional.of(mappedHandler); + } + + return Optional.empty(); + } + + private Map getResolvedFields(TypeInfo typeInfo) { + return AccessController.doPrivileged((PrivilegedAction>) () -> { + Map fields = Maps.newLinkedHashMap(); + + Type type = typeInfo.getType(); + Class rawType = typeInfo.getRawType(); + + while (!Object.class.equals(rawType)) { + for (Field field : rawType.getDeclaredFields()) { + if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) { + continue; + } + + if (!Modifier.isPublic(field.getModifiers())) { + logger.warn("Field {}#{} will not be serialised. Terasology no longer supports serialising private fields.", field.getName(), rawType.getTypeName()); + continue; + } + + Type fieldType = ReflectionUtil.resolveType(type, field.getGenericType()); + fields.put(field, fieldType); + } + + rawType = rawType.getSuperclass(); + } + + return fields; + }); + } +} From 873fe5bf2594653225e4c525333d5dfc93e690eb Mon Sep 17 00:00:00 2001 From: soloturn Date: Mon, 29 Jan 2024 02:51:50 +0100 Subject: [PATCH 2/9] log line shorter for ComponentTypeHandlerFactory --- .../extensionTypes/factories/ComponentTypeHandlerFactory.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java index d4f876e677d..99c3cd2f2b8 100644 --- a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java +++ b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java @@ -91,7 +91,8 @@ private Map getResolvedFields(TypeInfo typeInfo) { } if (!Modifier.isPublic(field.getModifiers())) { - logger.warn("Field {}#{} will not be serialised. Terasology no longer supports serialising private fields.", field.getName(), rawType.getTypeName()); + logger.atWarn().addArgument(field.getName()).addArgument(rawType.getTypeName()). + log("Field {}#{} will not be serialised. Terasology no longer supports serialising private fields."); continue; } From 717832568a66207a3798bc663a462210026b94d3 Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Sun, 18 Feb 2024 16:39:31 +0000 Subject: [PATCH 3/9] feat(TypeHandlerLibrary): do not serialise private fields in ObjectFieldMapTypeHandler by default --- .../typeHandling/TypeHandlerLibraryImpl.java | 5 - .../ComponentTypeHandlerFactory.java | 109 ------------------ .../coreTypes/ObjectFieldMapTypeHandler.java | 18 ++- .../ObjectFieldMapTypeHandlerFactory.java | 19 ++- 4 files changed, 34 insertions(+), 117 deletions(-) delete mode 100644 engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java index d27a756ffbe..d8b0d0d0f9c 100644 --- a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java +++ b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java @@ -3,7 +3,6 @@ package org.terasology.engine.persistence.typeHandling; -import com.google.common.collect.Maps; import org.joml.Quaternionf; import org.joml.Quaternionfc; import org.joml.Vector2f; @@ -32,7 +31,6 @@ import org.terasology.engine.persistence.typeHandling.extensionTypes.UITextureRegionTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.AssetTypeHandlerFactory; import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.ComponentClassTypeHandlerFactory; -import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.ComponentTypeHandlerFactory; import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.TextureRegionAssetTypeHandlerFactory; import org.terasology.engine.persistence.typeHandling.mathTypes.AABBfTypeHandler; import org.terasology.engine.persistence.typeHandling.mathTypes.AABBiTypeHandler; @@ -74,7 +72,6 @@ import org.terasology.persistence.typeHandling.TypeHandlerLibrary; import org.terasology.persistence.typeHandling.reflection.SerializationSandbox; import org.terasology.reflection.TypeRegistry; -import org.terasology.reflection.reflect.ConstructorLibrary; /** * A library of type handlers. This is used for the construction of class metadata. This library should be initialised @@ -162,8 +159,6 @@ private static void populateWithDefaultHandlers(TypeHandlerLibrary serialization serializationLibrary.addTypeHandler(Quaternionfc.class, new QuaternionfcTypeHandler()); serializationLibrary.addTypeHandler(IntegerRange.class, new IntegerRangeHandler()); - - serializationLibrary.addTypeHandlerFactory(new ComponentTypeHandlerFactory(new ConstructorLibrary(Maps.newHashMap()))); } } diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java deleted file mode 100644 index 99c3cd2f2b8..00000000000 --- a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/factories/ComponentTypeHandlerFactory.java +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright 2024 The Terasology Foundation -// SPDX-License-Identifier: Apache-2.0 - -package org.terasology.engine.persistence.typeHandling.extensionTypes.factories; - -import com.google.common.collect.Maps; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.terasology.gestalt.entitysystem.component.Component; -import org.terasology.persistence.typeHandling.TypeHandler; -import org.terasology.persistence.typeHandling.TypeHandlerContext; -import org.terasology.persistence.typeHandling.TypeHandlerFactory; -import org.terasology.persistence.typeHandling.coreTypes.ObjectFieldMapTypeHandler; -import org.terasology.persistence.typeHandling.coreTypes.RuntimeDelegatingTypeHandler; -import org.terasology.reflection.ReflectionUtil; -import org.terasology.reflection.TypeInfo; -import org.terasology.reflection.reflect.ConstructorLibrary; - -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.util.Map; -import java.util.Optional; - -/** - * A {@link TypeHandlerFactory} for serialising component types. - * It is similar to {@link org.terasology.persistence.typeHandling.coreTypes.factories.ObjectFieldMapTypeHandlerFactory} - * but does not allow serialising private fields. - */ -public class ComponentTypeHandlerFactory implements TypeHandlerFactory { - private Logger logger = LoggerFactory.getLogger(ComponentTypeHandlerFactory.class); - - private ConstructorLibrary constructorLibrary; - - public ComponentTypeHandlerFactory(ConstructorLibrary constructorLibrary) { - this.constructorLibrary = constructorLibrary; - } - - @Override - public Optional> create(TypeInfo typeInfo, TypeHandlerContext context) { - Class typeClass = typeInfo.getRawType(); - if (!Component.class.isAssignableFrom(typeClass)) { - return Optional.empty(); - } - - if (!Modifier.isAbstract(typeClass.getModifiers()) - && !typeClass.isLocalClass() - && !(typeClass.isMemberClass() && !Modifier.isStatic(typeClass.getModifiers()))) { - Map> fieldTypeHandlerMap = Maps.newLinkedHashMap(); - - getResolvedFields(typeInfo).forEach( - (field, fieldType) -> { - Optional> declaredFieldTypeHandler = - context.getTypeHandlerLibrary().getTypeHandler(fieldType); - - TypeInfo fieldTypeInfo = TypeInfo.of(fieldType); - - fieldTypeHandlerMap.put( - field, - new RuntimeDelegatingTypeHandler( - declaredFieldTypeHandler.orElse(null), - fieldTypeInfo, - context - ) - ); - } - ); - - ObjectFieldMapTypeHandler mappedHandler = - new ObjectFieldMapTypeHandler<>(constructorLibrary.get(typeInfo), fieldTypeHandlerMap); - - return Optional.of(mappedHandler); - } - - return Optional.empty(); - } - - private Map getResolvedFields(TypeInfo typeInfo) { - return AccessController.doPrivileged((PrivilegedAction>) () -> { - Map fields = Maps.newLinkedHashMap(); - - Type type = typeInfo.getType(); - Class rawType = typeInfo.getRawType(); - - while (!Object.class.equals(rawType)) { - for (Field field : rawType.getDeclaredFields()) { - if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) { - continue; - } - - if (!Modifier.isPublic(field.getModifiers())) { - logger.atWarn().addArgument(field.getName()).addArgument(rawType.getTypeName()). - log("Field {}#{} will not be serialised. Terasology no longer supports serialising private fields."); - continue; - } - - Type fieldType = ReflectionUtil.resolveType(type, field.getGenericType()); - fields.put(field, fieldType); - } - - rawType = rawType.getSuperclass(); - } - - return fields; - }); - } -} diff --git a/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java b/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java index 8cf6faa9944..fca9239f442 100644 --- a/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java +++ b/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java @@ -12,9 +12,12 @@ import org.terasology.persistence.typeHandling.TypeHandler; import org.terasology.persistence.typeHandling.TypeHandlerLibrary; import org.terasology.persistence.typeHandling.annotations.SerializedName; +import org.terasology.reflection.ReflectionUtil; import org.terasology.reflection.reflect.ObjectConstructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Modifier; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -48,10 +51,17 @@ public PersistedData serializeNonNull(T value, PersistedDataSerializer serialize Object val; try { - val = field.get(value); + if (Modifier.isPrivate(field.getModifiers())) { + val = ReflectionUtil.findGetter(field).invoke(value); + } else { + val = field.get(value); + } } catch (IllegalAccessException e) { logger.error("Field {} is inaccessible", field); continue; + } catch (InvocationTargetException e) { + logger.error("Failed to involve getter for field {}", field); + continue; } if (!Objects.equals(val, Defaults.defaultValue(field.getType()))) { @@ -101,7 +111,11 @@ public Optional deserialize(PersistedData data) { Optional fieldValue = handler.deserialize(entry.getValue()); if (fieldValue.isPresent()) { - field.set(result, fieldValue.get()); + if (Modifier.isPrivate(field.getModifiers())) { + ReflectionUtil.findSetter(field).invoke(result); + } else { + field.set(result, fieldValue.get()); + } } else { logger.error("Could not deserialize field {}", field.getName()); } diff --git a/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactory.java b/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactory.java index 488f6404997..ea9a653bd2d 100644 --- a/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactory.java +++ b/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactory.java @@ -3,6 +3,8 @@ package org.terasology.persistence.typeHandling.coreTypes.factories; import com.google.common.collect.Maps; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.terasology.persistence.typeHandling.TypeHandler; import org.terasology.persistence.typeHandling.TypeHandlerContext; import org.terasology.persistence.typeHandling.TypeHandlerFactory; @@ -17,10 +19,14 @@ import java.lang.reflect.Type; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.Optional; public class ObjectFieldMapTypeHandlerFactory implements TypeHandlerFactory { + private static final Logger logger = LoggerFactory.getLogger(ObjectFieldMapTypeHandlerFactory.class); + private static final List PRIVATE_OVERRIDE_ANNOTATIONS = List.of("org.terasology.nui.LayoutConfig"); private ConstructorLibrary constructorLibrary; @@ -77,7 +83,18 @@ private Map getResolvedFields(TypeInfo typeInfo) { continue; } - field.setAccessible(true); + if (Arrays.stream(field.getAnnotations()) + .anyMatch(annotation -> PRIVATE_OVERRIDE_ANNOTATIONS.contains(annotation.getClass().getCanonicalName()))) { + field.setAccessible(true); + } else { + if (Modifier.isPrivate(field.getModifiers()) && ReflectionUtil.findGetter(field) == null + && ReflectionUtil.findSetter(field) == null) { + logger.atWarn().addArgument(field.getName()).addArgument(rawType.getTypeName()). + log("Field {}#{} will not be serialised. Terasology no longer supports serialising private fields."); + continue; + } + } + Type fieldType = ReflectionUtil.resolveType(type, field.getGenericType()); fields.put(field, fieldType); } From 7701cc1d04ded8d66bc31a88968ed0be3f52c6f8 Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Sun, 18 Feb 2024 18:31:15 +0000 Subject: [PATCH 4/9] fix: fix ObjectFieldMapTypeHandlerFactoryTest tests --- .../ObjectFieldMapTypeHandlerFactoryTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java b/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java index 738dbfb45b5..45f40b575de 100644 --- a/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java +++ b/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java @@ -105,12 +105,12 @@ public void testMultipleObjectsOfDifferentType() { } private static class SingleTypeClass { - private T t; + public T t; } private static class MultiTypeClass { - private T t; - private U u; + public T t; + public U u; } @edu.umd.cs.findbugs.annotations.SuppressFBWarnings( @@ -119,7 +119,7 @@ private static class MultiTypeClass { " creation based on member types of input class TypeInfo. ") @SuppressWarnings("PMD.UnusedPrivateField") private static class SomeClass { - private T t; - private List list; + public T t; + public List list; } } From 9aa53fed26de7e328dbf074ad3f97ba03d5417de Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Sun, 18 Feb 2024 19:38:34 +0000 Subject: [PATCH 5/9] fix: fix TypeSerializerTest tests --- .../serializers/TypeSerializerTest.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java index dcdfa70494a..0e110e3393b 100644 --- a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java +++ b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java @@ -110,14 +110,14 @@ void testJsonSerializeDeserialize() throws IOException { assertEquals(INSTANCE, deserializedInstance); } - private static final class SomeClass { + public static final class SomeClass { @SerializedName("generic-t") - private T data; - private final List list = Lists.newArrayList(); - private final Set> animals = Sets.newHashSet(); - private Animal singleAnimal; + public T data; + public List list = Lists.newArrayList(); + public Set> animals = Sets.newHashSet(); + public Animal singleAnimal; - private SomeClass(T data) { + public SomeClass(T data) { this.data = data; } @@ -151,10 +151,10 @@ public String toString() { } @SuppressWarnings("checkstyle:FinalClass") - private static class Animal { - protected final T data; + public static class Animal { + public T data; - private Animal(T data) { + public Animal(T data) { this.data = data; } @@ -176,11 +176,11 @@ public int hashCode() { } } - private static final class Dog extends Animal { - private final Vector3f tailPosition; - private final Vector3f headPosition; + public static final class Dog extends Animal { + public Vector3f tailPosition; + public Vector3f headPosition; - private Dog(T data, Vector3f tailPosition, Vector3f headPosition) { + public Dog(T data, Vector3f tailPosition, Vector3f headPosition) { super(data); this.tailPosition = tailPosition; this.headPosition = headPosition; @@ -216,10 +216,10 @@ public int hashCode() { } } - private static final class Cheetah extends Animal { - private final Color spotColor; + public static final class Cheetah extends Animal { + public Color spotColor; - private Cheetah(T data, Color spotColor) { + public Cheetah(T data, Color spotColor) { super(data); this.spotColor = spotColor; } From 456efcad5a3e310dd7b2a66b63117b2a2a8dd69f Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Sun, 18 Feb 2024 19:39:37 +0000 Subject: [PATCH 6/9] fix: fix VectorTypeSerializerTest tests --- .../persistence/serializers/VectorTypeSerializerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/VectorTypeSerializerTest.java b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/VectorTypeSerializerTest.java index 0ad30391fb7..86e78c66440 100644 --- a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/VectorTypeSerializerTest.java +++ b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/VectorTypeSerializerTest.java @@ -59,13 +59,13 @@ void testSerializationConstant() { assertEquals(new Vector2f(1.0f, 2.0f), o.v3, .00001f); } - static class TestObject { + public static class TestObject { public Vector3f v1; public Vector2f v2; public Vector4f v3; } - static class TestObject2 { + public static class TestObject2 { public Vector3fc v1; public Vector4fc v2; public Vector2fc v3; From 2cbdba0f124e206da5e7c6c930b4307a0ce41628 Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Wed, 21 Feb 2024 19:11:28 +0000 Subject: [PATCH 7/9] fix: fix BlockAreaTypeHandlerTest tests --- .../typeHandling/mathTypes/BlockAreaTypeHandlerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/mathTypes/BlockAreaTypeHandlerTest.java b/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/mathTypes/BlockAreaTypeHandlerTest.java index 15f88346aa5..973c488beec 100644 --- a/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/mathTypes/BlockAreaTypeHandlerTest.java +++ b/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/mathTypes/BlockAreaTypeHandlerTest.java @@ -55,7 +55,7 @@ public void testGsonSerialization() throws IOException { assertEquals(new BlockArea(0, 0, 1, 1), o.b2); } - static class TestObject { + public static class TestObject { public BlockArea b1; public BlockAreac b2; } From 6f284d7209ac3b3dbfebea6627bd62fbfdc33bc2 Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Wed, 21 Feb 2024 20:45:35 +0000 Subject: [PATCH 8/9] style: fix checkstyle issues in TypeSerializerTest.java --- .../persistence/serializers/TypeSerializerTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java index 0e110e3393b..87e6cba2d6c 100644 --- a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java +++ b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java @@ -117,7 +117,7 @@ public static final class SomeClass { public Set> animals = Sets.newHashSet(); public Animal singleAnimal; - public SomeClass(T data) { + SomeClass(T data) { this.data = data; } @@ -154,7 +154,7 @@ public String toString() { public static class Animal { public T data; - public Animal(T data) { + Animal(T data) { this.data = data; } @@ -180,7 +180,7 @@ public static final class Dog extends Animal { public Vector3f tailPosition; public Vector3f headPosition; - public Dog(T data, Vector3f tailPosition, Vector3f headPosition) { + Dog(T data, Vector3f tailPosition, Vector3f headPosition) { super(data); this.tailPosition = tailPosition; this.headPosition = headPosition; @@ -219,7 +219,7 @@ public int hashCode() { public static final class Cheetah extends Animal { public Color spotColor; - public Cheetah(T data, Color spotColor) { + Cheetah(T data, Color spotColor) { super(data); this.spotColor = spotColor; } From 91c852f6af8f1a55ed71bbfb6e4335acde4c661a Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Sun, 25 Feb 2024 18:45:11 +0000 Subject: [PATCH 9/9] fix: catch setter errors explicitly in ObjectFieldTypeMapHandler --- .../coreTypes/ObjectFieldMapTypeHandler.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java b/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java index fca9239f442..e1f51f8d093 100644 --- a/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java +++ b/subsystems/TypeHandlerLibrary/src/main/java/org/terasology/persistence/typeHandling/coreTypes/ObjectFieldMapTypeHandler.java @@ -60,7 +60,7 @@ public PersistedData serializeNonNull(T value, PersistedDataSerializer serialize logger.error("Field {} is inaccessible", field); continue; } catch (InvocationTargetException e) { - logger.error("Failed to involve getter for field {}", field); + logger.error("Failed to invoke getter for field {}", field); continue; } @@ -103,7 +103,7 @@ public Optional deserialize(PersistedData data) { Field field = fieldByName.get(fieldName); if (field == null) { - logger.error("Cound not find field with name {}", fieldName); + logger.error("Could not find field with name {}", fieldName); continue; } @@ -112,7 +112,11 @@ public Optional deserialize(PersistedData data) { if (fieldValue.isPresent()) { if (Modifier.isPrivate(field.getModifiers())) { - ReflectionUtil.findSetter(field).invoke(result); + try { + ReflectionUtil.findSetter(field).invoke(result); + } catch (InvocationTargetException e) { + logger.error("Failed to invoke setter for field {}", field); + } } else { field.set(result, fieldValue.get()); }