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);
});
}
Aggregations