Search in sources :

Example 1 with InstanceCreator

use of org.terasology.persistence.typeHandling.InstanceCreator in project Terasology by MovingBlocks.

the class ConstructorLibrary method get.

public <T> ObjectConstructor<T> get(TypeInfo<T> typeInfo) {
    final Type type = typeInfo.getType();
    final Class<T> rawType = typeInfo.getRawType();
    // first try an instance creator
    // types must agree
    @SuppressWarnings("unchecked") final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type);
    if (typeCreator != null) {
        return () -> typeCreator.createInstance(type);
    }
    // Next try raw type match for instance creators
    // types must agree
    @SuppressWarnings("unchecked") final InstanceCreator<T> rawTypeCreator = (InstanceCreator<T>) instanceCreators.get(rawType);
    if (rawTypeCreator != null) {
        return () -> rawTypeCreator.createInstance(type);
    }
    // TODO remove using `AccessController.doPrivileged`
    return AccessController.doPrivileged((PrivilegedAction<ObjectConstructor<T>>) () -> {
        ObjectConstructor<T> defaultConstructor = newNoArgConstructor(rawType);
        if (defaultConstructor != null) {
            return defaultConstructor;
        }
        ObjectConstructor<T> defaultImplementation = newDefaultConstructor(typeInfo);
        if (defaultImplementation != null) {
            return defaultImplementation;
        }
        return newUnsafeAllocator(typeInfo);
    });
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InstanceCreator(org.terasology.persistence.typeHandling.InstanceCreator)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 InstanceCreator (org.terasology.persistence.typeHandling.InstanceCreator)1