Skip to content

Commit

Permalink
Use TypeRegistry in ModuleEnvironmentSandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltak committed Aug 10, 2019
1 parent ef8c8b4 commit 66a9dcd
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import org.terasology.recording.RecordAndReplayCurrentStatus;
import org.terasology.recording.RecordAndReplaySerializer;
import org.terasology.recording.RecordAndReplayUtils;
import org.terasology.reflection.TypeRegistry;
import org.terasology.reflection.internal.TypeRegistryImpl;
import org.terasology.rendering.assets.animation.MeshAnimation;
import org.terasology.rendering.assets.animation.MeshAnimationImpl;
import org.terasology.rendering.assets.atlas.Atlas;
Expand Down Expand Up @@ -267,7 +269,10 @@ protected void setupConfig() {

@Override
protected void setupModuleManager(Set<Name> moduleNames) throws Exception {
ModuleManager moduleManager = ModuleManagerFactory.create();
TypeRegistryImpl typeRegistry = new TypeRegistryImpl();
context.put(TypeRegistry.class, typeRegistry);

ModuleManager moduleManager = ModuleManagerFactory.create(typeRegistry);
ModuleRegistry registry = moduleManager.getRegistry();

DependencyResolver resolver = new DependencyResolver(registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.terasology.engine.paths.PathManager;
import org.terasology.module.DependencyResolver;
import org.terasology.module.ResolutionResult;
import org.terasology.reflection.TypeRegistry;
import org.terasology.reflection.internal.TypeRegistryImpl;
import org.terasology.testUtil.ModuleManagerFactory;

Expand All @@ -33,24 +34,28 @@

public abstract class ModuleEnvironmentTest {
protected ModuleManager moduleManager;
protected TypeRegistry typeRegistry;

@Before
public void setup() throws Exception {
public void before() throws Exception {
final JavaArchive homeArchive = ShrinkWrap.create(JavaArchive.class);
final FileSystem vfs = ShrinkWrapFileSystems.newFileSystem(homeArchive);
PathManager.getInstance().useOverrideHomePath(vfs.getPath(""));

moduleManager = ModuleManagerFactory.create(getTypeRegistry());
typeRegistry = new TypeRegistryImpl();
moduleManager = ModuleManagerFactory.create((TypeRegistryImpl) typeRegistry);

DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(moduleManager.getRegistry().getModuleIds());

assumeTrue(result.isSuccess());

moduleManager.loadEnvironment(result.getModules(), true);

setup();
}

protected TypeRegistryImpl getTypeRegistry() {
return Mockito.mock(TypeRegistryImpl.class);
protected void setup() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.terasology.recording.RecordAndReplaySerializer;
import org.terasology.recording.RecordAndReplayUtils;
import org.terasology.recording.RecordedEventStore;
import org.terasology.reflection.TypeRegistry;
import org.terasology.world.block.BlockManager;
import org.terasology.world.chunks.blockdata.ExtraBlockDataManager;

Expand Down Expand Up @@ -104,7 +105,7 @@ public void setup() throws Exception {
context.put(CharacterStateEventPositionMap.class, characterStateEventPositionMap);
DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = new DirectionAndOriginPosRecorderList();
context.put(DirectionAndOriginPosRecorderList.class, directionAndOriginPosRecorderList);
RecordAndReplaySerializer recordAndReplaySerializer = new RecordAndReplaySerializer(engineEntityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager);
RecordAndReplaySerializer recordAndReplaySerializer = new RecordAndReplaySerializer(engineEntityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager, context.get(TypeRegistry.class));
context.put(RecordAndReplaySerializer.class, recordAndReplaySerializer);

Path savePath = PathManager.getInstance().getSavePath("world1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.terasology.recording.RecordAndReplaySerializer;
import org.terasology.recording.RecordAndReplayUtils;
import org.terasology.recording.RecordedEventStore;
import org.terasology.reflection.TypeRegistry;
import org.terasology.registry.CoreRegistry;
import org.terasology.world.WorldProvider;
import org.terasology.world.block.Block;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void setup() throws Exception {
recordAndReplayUtils = new RecordAndReplayUtils();
CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap();
DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = new DirectionAndOriginPosRecorderList();
recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager);
recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager, mock(TypeRegistry.class));
recordAndReplayCurrentStatus = context.get(RecordAndReplayCurrentStatus.class);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,28 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.nio.file.ShrinkWrapFileSystems;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Before;
import org.junit.Test;
import org.terasology.engine.module.ModuleManager;
import org.terasology.engine.paths.PathManager;
import org.terasology.ModuleEnvironmentTest;
import org.terasology.math.geom.Vector3f;
import org.terasology.module.DependencyResolver;
import org.terasology.module.ModuleEnvironment;
import org.terasology.module.ResolutionResult;
import org.terasology.naming.Name;
import org.terasology.persistence.ModuleContext;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.persistence.typeHandling.annotations.SerializedName;
import org.terasology.reflection.TypeInfo;
import org.terasology.rendering.nui.Color;
import org.terasology.testUtil.ModuleManagerFactory;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

public class TypeSerializerTest {
public class TypeSerializerTest extends ModuleEnvironmentTest {
private static final SomeClass<Integer> INSTANCE = new SomeClass<>(0xdeadbeef);
private static final String INSTANCE_JSON = "{\"generic-t\":-559038737,\"list\":[50,51,-52,-53],\"animals\":[{\"class\":\"org.terasology.persistence.serializers.TypeSerializerTest$Dog\",\"tailPosition\":[3.15,54.51,-0.001],\"data\":1},{\"class\":\"org.terasology.persistence.serializers.TypeSerializerTest$Cheetah\",\"spotColor\":[255,0,255,255],\"data\":2}]}";
private static final String INSTANCE_JSON = "{\"generic-t\":-559038737,\"list\":[50,51,-52,-53],\"animals\":[{\"class\":\"org.terasology.persistence.serializers.TypeSerializerTest$Dog\",\"tailPosition\":[3.15,54.51,-0.001],\"data\":{\"class\":\"java.lang.Integer\",\"content\":1}},{\"class\":\"org.terasology.persistence.serializers.TypeSerializerTest$Cheetah\",\"spotColor\":[255,0,255,255],\"data\":{\"class\":\"java.lang.Integer\",\"content\":2}}]}";

static {
INSTANCE.list.addAll(Lists.newArrayList(50, 51, -52, -53));
Expand All @@ -61,23 +52,11 @@ public class TypeSerializerTest {
private ProtobufSerializer protobufSerializer;
private GsonSerializer gsonSerializer;

@Before
public void setup() throws Exception {
final JavaArchive homeArchive = ShrinkWrap.create(JavaArchive.class);
final FileSystem vfs = ShrinkWrapFileSystems.newFileSystem(homeArchive);
PathManager.getInstance().useOverrideHomePath(vfs.getPath(""));
@Override
public void setup() {
ModuleContext.setContext(moduleManager.getEnvironment().get(new Name("unittest")));

ModuleManager moduleManager = ModuleManagerFactory.create();

DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(moduleManager.getRegistry().getModuleIds());

assumeTrue(result.isSuccess());

ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), true);
ModuleContext.setContext(environment.get(new Name("unittest")));

typeHandlerLibrary = TypeHandlerLibrary.forModuleEnvironment(moduleManager);
typeHandlerLibrary = TypeHandlerLibrary.forModuleEnvironment(moduleManager, typeRegistry);

protobufSerializer = new ProtobufSerializer(typeHandlerLibrary);
gsonSerializer = new GsonSerializer(typeHandlerLibrary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.terasology.network.NetworkMode;
import org.terasology.network.NetworkSystem;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.reflection.TypeRegistry;
import org.terasology.registry.CoreRegistry;

import java.util.ArrayList;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void setup() {
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.getEnvironment()).thenReturn(mock(ModuleEnvironment.class));
RecordAndReplaySerializer recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, eventStore,
recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager);
recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager, mock(TypeRegistry.class));
recordAndReplayCurrentStatus.setStatus(RecordAndReplayStatus.REPLAYING);
entity = entityManager.create();
Long id = entity.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ public class TypeRegistryImplTest extends ModuleEnvironmentTest {
Reflections.log = null;
}

private TypeRegistryImpl typeRegistry = new TypeRegistryImpl();

@Override
protected TypeRegistryImpl getTypeRegistry() {
return typeRegistry;
}

@Test
public void testRegistry() {
assertTrue(typeRegistry.getSubtypesOf(Collection.class).contains(TreeSet.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ private void initManagers() {
CopyStrategyLibrary copyStrategyLibrary = new CopyStrategyLibrary(reflectFactory);
rootContext.put(CopyStrategyLibrary.class, copyStrategyLibrary);

rootContext.put(TypeHandlerLibrary.class, TypeHandlerLibrary.forModuleEnvironment(moduleManager));
rootContext.put(TypeHandlerLibrary.class, TypeHandlerLibrary.forModuleEnvironment(moduleManager, typeRegistry));

changeStatus(TerasologyEngineStatus.INITIALIZING_ASSET_TYPES);
assetTypeManager = new ModuleAwareAssetTypeManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.terasology.recording.RecordAndReplayStatus;
import org.terasology.recording.RecordAndReplayUtils;
import org.terasology.recording.RecordedEventStore;
import org.terasology.reflection.TypeRegistry;
import org.terasology.reflection.copy.CopyStrategyLibrary;
import org.terasology.reflection.reflect.ReflectFactory;
import org.terasology.reflection.reflect.ReflectionReflectFactory;
Expand All @@ -76,7 +77,8 @@ public static void addReflectionBasedLibraries(Context context) {
context.put(CopyStrategyLibrary.class, copyStrategyLibrary);

ModuleManager moduleManager = context.get(ModuleManager.class);
TypeHandlerLibrary typeHandlerLibrary = TypeHandlerLibrary.forModuleEnvironment(moduleManager);
TypeRegistry typeRegistry = context.get(TypeRegistry.class);
TypeHandlerLibrary typeHandlerLibrary = TypeHandlerLibrary.forModuleEnvironment(moduleManager, typeRegistry);
context.put(TypeHandlerLibrary.class, typeHandlerLibrary);

EntitySystemLibrary library = new EntitySystemLibrary(context, typeHandlerLibrary);
Expand Down Expand Up @@ -134,7 +136,7 @@ public static void addEntityManagementRelatedClasses(Context context) {
CharacterStateEventPositionMap characterStateEventPositionMap = context.get(CharacterStateEventPositionMap.class);
DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = context.get(DirectionAndOriginPosRecorderList.class);
RecordedEventStore recordedEventStore = new RecordedEventStore();
RecordAndReplaySerializer recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager);
RecordAndReplaySerializer recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager, context.get(TypeRegistry.class));
context.put(RecordAndReplaySerializer.class, recordAndReplaySerializer);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.terasology.math.geom.Vector3f;
import org.terasology.math.geom.Vector3i;
import org.terasology.math.geom.Vector4f;
import org.terasology.module.ModuleEnvironment;
import org.terasology.naming.Name;
import org.terasology.persistence.typeHandling.coreTypes.BooleanTypeHandler;
import org.terasology.persistence.typeHandling.coreTypes.ByteArrayTypeHandler;
Expand Down Expand Up @@ -67,12 +66,12 @@
import org.terasology.persistence.typeHandling.reflection.ReflectionsSandbox;
import org.terasology.persistence.typeHandling.reflection.SerializationSandbox;
import org.terasology.reflection.TypeInfo;
import org.terasology.reflection.TypeRegistry;
import org.terasology.reflection.metadata.ClassMetadata;
import org.terasology.reflection.metadata.FieldMetadata;
import org.terasology.reflection.reflect.ConstructorLibrary;
import org.terasology.rendering.assets.texture.TextureRegion;
import org.terasology.rendering.nui.Color;
import org.terasology.utilities.ReflectionUtil;

import java.lang.reflect.Type;
import java.util.HashMap;
Expand Down Expand Up @@ -148,8 +147,8 @@ public TypeHandlerLibrary(Reflections reflections) {
this(new ReflectionsSandbox(reflections));
}

public TypeHandlerLibrary(ModuleManager moduleManager) {
this(new ModuleEnvironmentSandbox(moduleManager));
public TypeHandlerLibrary(ModuleManager moduleManager, TypeRegistry typeRegistry) {
this(new ModuleEnvironmentSandbox(moduleManager, typeRegistry));
}

/**
Expand All @@ -172,8 +171,8 @@ public static TypeHandlerLibrary withReflections(Reflections reflections) {
return library;
}

public static TypeHandlerLibrary forModuleEnvironment(ModuleManager moduleManager) {
TypeHandlerLibrary library = new TypeHandlerLibrary(moduleManager);
public static TypeHandlerLibrary forModuleEnvironment(ModuleManager moduleManager, TypeRegistry typeRegistry) {
TypeHandlerLibrary library = new TypeHandlerLibrary(moduleManager, typeRegistry);

populateWithDefaultHandlers(library);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,47 @@
package org.terasology.persistence.typeHandling.reflection;

import com.google.common.base.Preconditions;
import com.google.common.collect.Streams;
import org.terasology.engine.SimpleUri;
import org.terasology.engine.module.ModuleManager;
import org.terasology.module.ModuleEnvironment;
import org.terasology.naming.Name;
import org.terasology.persistence.ModuleContext;
import org.terasology.persistence.typeHandling.TypeHandler;
import org.terasology.reflection.TypeInfo;
import org.terasology.reflection.TypeRegistry;

import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.collect.Streams.stream;

public class ModuleEnvironmentSandbox implements SerializationSandbox {
// TODO: Use TypeRegistry

private final TypeRegistry typeRegistry;
private final ModuleManager moduleManager;

public ModuleEnvironmentSandbox(ModuleManager moduleManager) {
public ModuleEnvironmentSandbox(ModuleManager moduleManager, TypeRegistry typeRegistry) {
this.moduleManager = moduleManager;
this.typeRegistry = typeRegistry;
}

private ModuleEnvironment getModuleEnvironment() {
private ModuleEnvironment getEnvironment() {
return moduleManager.getEnvironment();
}

@Override
public <T> Optional<Class<? extends T>> findSubTypeOf(String subTypeIdentifier, Class<T> clazz) {
if (getModuleProviding(clazz) == null) {
// Assume that subTypeIdentifier is full name
return typeRegistry.load(subTypeIdentifier)
// If loaded class is not a subtype, return empty
.filter(clazz::isAssignableFrom)
.map(sub -> (Class<? extends T>) sub);
}

Iterator<Class<? extends T>> possibilities =
getModuleEnvironment()
.getSubtypesOf(clazz, subclass -> doesSubclassMatch(subclass, subTypeIdentifier))
typeRegistry
.getSubtypesOf(clazz)
.stream()
.filter(subclass -> doesSubclassMatch(subclass, subTypeIdentifier))
.iterator();

if (possibilities.hasNext()) {
Expand Down Expand Up @@ -83,7 +90,7 @@ private boolean doesSubclassMatch(Class<?> subclass, String subTypeIdentifier) {
}

// Now check through module and simple name
Name providingModule = getModuleEnvironment().getModuleProviding(subclass);
Name providingModule = getModuleProviding(subclass);
Name givenModuleName;

if (subTypeUri.isValid()) {
Expand All @@ -100,7 +107,11 @@ private boolean doesSubclassMatch(Class<?> subclass, String subTypeIdentifier) {
public <T> String getSubTypeIdentifier(Class<? extends T> subType, Class<T> baseType) {
String subTypeUri = getTypeUri(subType);

long subTypesWithSameUri = Streams.stream(getModuleEnvironment().getSubtypesOf(baseType))
if (getModuleProviding(baseType) == null) {
return subType.getName();
}

long subTypesWithSameUri = typeRegistry.getSubtypesOf(baseType).stream()
.map(this::getTypeUri)
.filter(subTypeUri::equals)
.count();
Expand All @@ -118,7 +129,7 @@ public <T> String getSubTypeIdentifier(Class<? extends T> subType, Class<T> base

@Override
public <T> boolean isValidTypeHandlerDeclaration(TypeInfo<T> type, TypeHandler<T> typeHandler) {
Name moduleDeclaringHandler = getModuleEnvironment().getModuleProviding(typeHandler.getClass());
Name moduleDeclaringHandler = getModuleProviding(typeHandler.getClass());

// If handler was declared outside of a module (engine or somewhere else), we allow it
// TODO: Possibly find better way to refer to engine module
Expand All @@ -133,18 +144,14 @@ public <T> boolean isValidTypeHandlerDeclaration(TypeInfo<T> type, TypeHandler<T
return false;
}

Name moduleDeclaringType = getModuleEnvironment().getModuleProviding(type.getRawType());
Name moduleDeclaringType = getModuleProviding(type.getRawType());

// Both the type and the handler must come from the same module
return Objects.equals(moduleDeclaringType, moduleDeclaringHandler);
}

private String getTypeUri(Class<?> type) {
if (type.getClassLoader() == null) {
return type.getName();
}

Name moduleProvidingType = getModuleEnvironment().getModuleProviding(type);
Name moduleProvidingType = getModuleProviding(type);

if (moduleProvidingType == null || moduleProvidingType.isEmpty()) {
return type.getName();
Expand All @@ -154,4 +161,12 @@ private String getTypeUri(Class<?> type) {

return new SimpleUri(moduleProvidingType, typeSimpleName).toString();
}

private Name getModuleProviding(Class<?> type) {
if (type.getClassLoader() == null) {
return null;
}

return getEnvironment().getModuleProviding(type);
}
}
Loading

0 comments on commit 66a9dcd

Please sign in to comment.