Search in sources :

Example 6 with FieldMetadata

use of org.terasology.reflection.metadata.FieldMetadata in project Terasology by MovingBlocks.

the class MappedContainerTypeHandler method deserialize.

@Override
public T deserialize(PersistedData data, DeserializationContext context) {
    try {
        T result = clazz.newInstance();
        for (Map.Entry<String, PersistedData> entry : data.getAsValueMap().entrySet()) {
            FieldMetadata fieldInfo = fieldByName.get(UriUtil.normalise(entry.getKey()));
            if (fieldInfo != null) {
                TypeHandler handler = mappedFields.get(fieldInfo);
                Object val = handler.deserialize(entry.getValue(), context);
                fieldInfo.setValue(result, val);
            }
        }
        return result;
    } catch (Exception e) {
        logger.error("Unable to deserialize {}", data, e);
    }
    return null;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) SimpleTypeHandler(org.terasology.persistence.typeHandling.SimpleTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) Map(java.util.Map)

Example 7 with FieldMetadata

use of org.terasology.reflection.metadata.FieldMetadata in project Terasology by MovingBlocks.

the class MetadataBuilderTest method testPrivateField.

@Test
public void testPrivateField() throws Exception {
    DefaultClassMetadata<PrivateField> metadata = new DefaultClassMetadata<>(new SimpleUri(), PrivateField.class, factory, copyStrategyLibrary);
    assertNotNull(metadata);
    assertEquals(1, metadata.getFieldCount());
    FieldMetadata fieldMetadata = metadata.getField("name");
    assertNotNull(fieldMetadata);
    assertEquals(String.class, fieldMetadata.getType());
    assertEquals("name", fieldMetadata.getName());
    assertNotNull(metadata.newInstance());
}
Also used : DefaultClassMetadata(org.terasology.reflection.metadata.DefaultClassMetadata) FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) SimpleUri(org.terasology.engine.SimpleUri) Test(org.junit.Test)

Aggregations

FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)7 PersistedData (org.terasology.persistence.typeHandling.PersistedData)3 Map (java.util.Map)2 SimpleUri (org.terasology.engine.SimpleUri)2 Component (org.terasology.entitySystem.Component)2 SimpleTypeHandler (org.terasology.persistence.typeHandling.SimpleTypeHandler)2 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)2 DefaultClassMetadata (org.terasology.reflection.metadata.DefaultClassMetadata)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1 BeforeDeactivateComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)1 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1 OnChangedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent)1 ComponentLibrary (org.terasology.entitySystem.metadata.ComponentLibrary)1 ReplicatedFieldMetadata (org.terasology.entitySystem.metadata.ReplicatedFieldMetadata)1 Prefab (org.terasology.entitySystem.prefab.Prefab)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 NetworkComponent (org.terasology.network.NetworkComponent)1 DeserializationContext (org.terasology.persistence.typeHandling.DeserializationContext)1