public Factory(final String type, Class<? extends AbstractDataStore> clazz) { Objects.requireNonNull(type, "type"); Objects.requireNonNull(clazz, "clazz"); this.type = type; this.expectedClazz = clazz; try { mapper = ObjectMapper.forClass(clazz); } catch (ObjectMappingException e) { throw new ExceptionInInitializerError(e); } }
@SuppressWarnings("unchecked") public static <T> ObjectMapper<T>.BoundInstance forObject(T obj) throws ObjectMappingException { Preconditions.checkNotNull(obj); return forClass((Class<T>) obj.getClass()).bind(obj); }
/** * Creates a new object mapper bound to the given object. * * @param obj The object * @param <T> The object type * @return An appropriate object mapper instance. * @throws ObjectMappingException */ @SuppressWarnings("unchecked") public static <T> ObjectMapper<T>.BoundInstance forObject(@NonNull T obj) throws ObjectMappingException { Preconditions.checkNotNull(obj); return forClass((Class<T>) obj.getClass()).bind(obj); }
/** * Creates a new object mapper bound to the given object. * * @param obj The object * @param <T> The object type * @return An appropriate object mapper instance. * @throws ObjectMappingException */ @SuppressWarnings("unchecked") public static <T> ObjectMapper<T>.BoundInstance forObject(@NonNull T obj) throws ObjectMappingException { Preconditions.checkNotNull(obj); return forClass((Class<T>) obj.getClass()).bind(obj); }
mapper = ObjectMapper.forClass(configClass).bindToNew(); } catch (ObjectMappingException e) { SneakyThrow.throwException(e);
/** * Loads the given configuration file. */ public static void load(Path path) { System.out.println("Loading config from " + path.toString()); try { Files.createDirectories(path.getParent()); if (Files.notExists(path)) { Files.createFile(path); } loader = HoconConfigurationLoader.builder().setPath(path).build(); configMapper = ObjectMapper.forClass(ConfigBase.class).bindToNew(); node = loader.load(ConfigurationOptions.defaults().setHeader(HEADER)); config = configMapper.populate(node); configMapper.serialize(node); loader.save(node); } catch (Exception e) { System.err.println("Error loading configuration:"); e.printStackTrace(); } }
@Test public void testNullsPreserved() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final TestObject obj = mapper.bindToNew().populate(SimpleConfigurationNode.root()); assertNull(obj.stringVal); }
@Test public void testNoArglessConstructor() throws ObjectMappingException { Assertions.assertTrue(Assertions.assertThrows(ObjectMappingException.class, () -> { ObjectMapper<NonZeroArgConstructorObject> mapper = ObjectMapper.forClass(NonZeroArgConstructorObject.class); assertFalse(mapper.canCreateInstances()); mapper.bindToNew(); }).getMessage().startsWith("No zero-arg constructor")); }
@Test public void testCreateFromNode() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); source.getNode("test-key").setValue("some are born great, some achieve greatness, and some have greatness thrust upon them"); final TestObject obj = mapper.bindToNew().populate(source); assertEquals("some are born great, some achieve greatness, and some have greatness thrust upon them", obj.stringVal); }
@Test public void testKeyFromFieldName() throws ObjectMappingException { final ObjectMapper<FieldNameObject> mapper = ObjectMapper.forClass(FieldNameObject.class); final ConfigurationNode node = SimpleConfigurationNode.root(); node.getNode("loads").setValue(true); FieldNameObject obj = mapper.bindToNew().populate(node); assertTrue(obj.loads); }
@Test public void testCommentsApplied() throws ObjectMappingException { CommentedConfigurationNode node = SimpleCommentedConfigurationNode.root(); ObjectMapper<CommentedObject>.BoundInstance mapper = ObjectMapper.forClass(CommentedObject.class).bindToNew(); CommentedObject obj = mapper.populate(node); obj.color = "fuchsia"; obj.politician = "All of them"; mapper.serialize(node); assertEquals("You look nice today", node.getNode("commented-key").getComment().orElse(null)); assertEquals("fuchsia", node.getNode("commented-key").getString()); assertFalse(node.getNode("no-comment").getComment().isPresent()); }
@Test public void testLoadExistingObject() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); final TestObject instance = new TestObject(); source.getNode("test-key").setValue("boom"); mapper.bind(instance).populate(source); assertEquals("boom", instance.stringVal); }
@Test public void testDefaultsApplied() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); final TestObject instance = new TestObject(); instance.stringVal = "hi"; mapper.bind(instance).populate(source); assertEquals("hi", source.getNode("test-key").getString()); }
@Test public void testSuperclassFieldsIncluded() throws ObjectMappingException { final ObjectMapper<TestObjectChild> mapper = ObjectMapper.forClass(TestObjectChild.class); ConfigurationNode node = SimpleConfigurationNode.root(); node.getNode("child-setting").setValue(true); node.getNode("test-key").setValue("Parents get populated too!"); TestObjectChild instance = mapper.bindToNew().populate(node); assertEquals(true, instance.childSetting); assertEquals("Parents get populated too!", instance.stringVal); }
containingObject.inner = childObject; final ObjectMapper<ContainingObject> mapper = ObjectMapper.forClass(ContainingObject.class); mapper.bind(containingObject).serialize(node);