Search in sources :

Example 1 with MapCopyStrategy

use of org.terasology.reflection.copy.strategy.MapCopyStrategy in project Terasology by MovingBlocks.

the class CopyStrategyLibrary method getStrategy.

// TODO: Consider CopyStrategyFactory system for Collections and similar
public CopyStrategy<?> getStrategy(Type genericType) {
    Class<?> typeClass = ReflectionUtil.getClassOfType(genericType);
    if (typeClass == null) {
        logger.error("Cannot obtain class for type {}, using default strategy", genericType);
        return defaultStrategy;
    }
    if (List.class.isAssignableFrom(typeClass)) {
        // For lists, create the handler for the contained type and wrap in a list type handler
        Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
        if (parameter != null) {
            CopyStrategy<?> contentStrategy = getStrategy(parameter);
            return new ListCopyStrategy<>(contentStrategy);
        }
        logger.error("List field is not parametrized - using default strategy");
        return new ListCopyStrategy<>(defaultStrategy);
    } else if (Set.class.isAssignableFrom(typeClass)) {
        // For sets:
        Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
        if (parameter != null) {
            CopyStrategy<?> contentStrategy = getStrategy(parameter);
            return new SetCopyStrategy<>(contentStrategy);
        }
        logger.error("Set field is not parametrized - using default strategy");
        return new SetCopyStrategy<>(defaultStrategy);
    } else if (Map.class.isAssignableFrom(typeClass)) {
        // For Maps, create the handler for the value type
        Type keyParameter = ReflectionUtil.getTypeParameter(genericType, 0);
        CopyStrategy<?> keyStrategy;
        if (keyParameter != null) {
            keyStrategy = getStrategy(keyParameter);
        } else {
            logger.error("Map field is missing key parameter - using default strategy");
            keyStrategy = defaultStrategy;
        }
        Type valueParameter = ReflectionUtil.getTypeParameter(genericType, 1);
        CopyStrategy<?> valueStrategy;
        if (valueParameter != null) {
            valueStrategy = getStrategy(valueParameter);
        } else {
            logger.error("Map field is missing value parameter - using default strategy");
            valueStrategy = defaultStrategy;
        }
        return new MapCopyStrategy<>(keyStrategy, valueStrategy);
    } else if (strategies.containsKey(typeClass)) {
        // For known types, just use the handler
        return strategies.get(typeClass);
    } else if (typeClass.getAnnotation(MappedContainer.class) != null) {
        if (Modifier.isAbstract(typeClass.getModifiers()) || typeClass.isLocalClass() || (typeClass.isMemberClass() && !Modifier.isStatic(typeClass.getModifiers()))) {
            logger.error("Type {} is not a valid mapped class", typeClass);
            return defaultStrategy;
        }
        try {
            ClassMetadata<?, ?> classMetadata = new DefaultClassMetadata<>(new SimpleUri(), typeClass, reflectFactory, this);
            return new MappedContainerCopyStrategy<>(classMetadata);
        } catch (NoSuchMethodException e) {
            logger.error("Unable to create copy strategy for field of type {}: no publicly accessible default constructor", typeClass.getSimpleName());
            return defaultStrategy;
        }
    } else {
        logger.debug("Using default copy strategy for {}", typeClass);
        strategies.put(typeClass, defaultStrategy);
        return defaultStrategy;
    }
}
Also used : MapCopyStrategy(org.terasology.reflection.copy.strategy.MapCopyStrategy) Set(java.util.Set) ListCopyStrategy(org.terasology.reflection.copy.strategy.ListCopyStrategy) MapCopyStrategy(org.terasology.reflection.copy.strategy.MapCopyStrategy) MappedContainerCopyStrategy(org.terasology.reflection.copy.strategy.MappedContainerCopyStrategy) SetCopyStrategy(org.terasology.reflection.copy.strategy.SetCopyStrategy) SimpleUri(org.terasology.engine.SimpleUri) MappedContainerCopyStrategy(org.terasology.reflection.copy.strategy.MappedContainerCopyStrategy) Type(java.lang.reflect.Type) DefaultClassMetadata(org.terasology.reflection.metadata.DefaultClassMetadata) ListCopyStrategy(org.terasology.reflection.copy.strategy.ListCopyStrategy) MappedContainer(org.terasology.reflection.MappedContainer)

Aggregations

Type (java.lang.reflect.Type)1 Set (java.util.Set)1 SimpleUri (org.terasology.engine.SimpleUri)1 MappedContainer (org.terasology.reflection.MappedContainer)1 ListCopyStrategy (org.terasology.reflection.copy.strategy.ListCopyStrategy)1 MapCopyStrategy (org.terasology.reflection.copy.strategy.MapCopyStrategy)1 MappedContainerCopyStrategy (org.terasology.reflection.copy.strategy.MappedContainerCopyStrategy)1 SetCopyStrategy (org.terasology.reflection.copy.strategy.SetCopyStrategy)1 DefaultClassMetadata (org.terasology.reflection.metadata.DefaultClassMetadata)1