@Override public void serialize(TypeToken<?> type, Text obj, ConfigurationNode value) throws ObjectMappingException { String json = (String) obj.toContainer().get(Queries.JSON).get(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new StringReader(json))) .build(); try { value.setValue(gsonLoader.load()); } catch (IOException e) { throw new ObjectMappingException(e); } }
public PlayerFilter(WebHook hook, ConfigurationNode config) { super(hook, config); try { players = config.getList(TypeToken.of(String.class)); } catch (ObjectMappingException e) { e.printStackTrace(); } }
@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")); }
public ItemTypeFilter(WebHook hook, ConfigurationNode config) { super(hook, config); try { items = config.getList(TypeToken.of(ItemType.class)); } catch (ObjectMappingException e) { e.printStackTrace(); } }
@Override public Text deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { StringWriter writer = new StringWriter(); GsonConfigurationLoader gsonLoader = GsonConfigurationLoader.builder() .setIndent(0) .setSink(() -> new BufferedWriter(writer)) .setHeaderMode(HeaderMode.NONE) .build(); try { gsonLoader.save(value); } catch (IOException e) { throw new ObjectMappingException(e); } return Sponge.getDataManager().deserialize(Text.class, DataContainer.createNew().set(Queries.JSON, writer.toString())).get(); }
public BlockTypeFilter(WebHook hook, ConfigurationNode config) { super(hook, config); try { types = config.getList(TypeToken.of(BlockType.class)); } catch (ObjectMappingException e) { e.printStackTrace(); } }
@Override public TextFormat deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { TextColor color = TextColors.NONE; GameRegistry registry = Sponge.getRegistry(); String colorId = value.getNode(NODE_COLOR).getString(); if (colorId != null) { color = registry.getType(TextColor.class, colorId).orElseThrow(() -> new ObjectMappingException("Color not found: " + colorId)); } TextStyle style = TextStyles.NONE; ConfigurationNode styleNode = value.getNode(NODE_STYLE); for (TextStyle.Base component : registry.getAllOf(TextStyle.Base.class)) { if (styleNode.getNode(component.getId().toLowerCase()).getBoolean()) { style = style.and(component); } } return TextFormat.NONE.color(color).style(style); }
@Override public void onDisable() { super.onDisable(); try { config.getNode("variables").setValue(new TypeToken<Map<String, Map<String, String>>>() {}, variableStore); } catch (ObjectMappingException e) { e.printStackTrace(); } if (variableCommandMapping != null) { Sponge.getCommandManager().removeMapping(variableCommandMapping); } }
/** * Create a new instance of an object of the appropriate type. This method is not * responsible for any population. * * @return The new object instance * @throws ObjectMappingException If constructing a new instance was not possible */ protected T constructObject() throws ObjectMappingException { if (constructor == null) { throw new ObjectMappingException("No zero-arg constructor is available for class " + clazz + " but is required to construct new instances!"); } try { return constructor.newInstance(); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new ObjectMappingException("Unable to create instance of target class " + clazz, e); } }
public static <T extends BaseConfig> T load(Path path, T defaultConfig) { ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder() .setPath(path) .build(); CommentedConfigurationNode node; try { node = loader.load(); } catch (IOException e) { e.printStackTrace(); node = loader.createEmptyNode(); } T config = null; try { config = (T)node.getValue(TypeToken.of(defaultConfig.getClass())); } catch (ObjectMappingException e) { e.printStackTrace(); } if (config == null) { config = defaultConfig; } config.setLoader(loader); config.setNode(node); config.save(); return config; } }
/** * Create a new instance of an object of the appropriate type. This method is not * responsible for any population. * * @return The new object instance * @throws ObjectMappingException If constructing a new instance was not possible */ protected T constructObject() throws ObjectMappingException { if (constructor == null) { throw new ObjectMappingException("No zero-arg constructor is available for class " + clazz + " but is required to construct new instances!"); } try { return constructor.newInstance(); } catch (InstantiationException e) { // JDK6 compat throw new ObjectMappingException("Unable to create instance of target class " + clazz, e); } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to create instance of target class " + clazz, e); } catch (InvocationTargetException e) { throw new ObjectMappingException("Unable to create instance of target class " + clazz, e); } }
/** * Create a new instance of an object of the appropriate type. This method is not * responsible for any population. * * @return The new object instance * @throws ObjectMappingException If constructing a new instance was not possible */ protected T constructObject() throws ObjectMappingException { if (constructor == null) { throw new ObjectMappingException("No zero-arg constructor is available for class " + clazz + " but is required to construct new instances!"); } try { return constructor.newInstance(); } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new ObjectMappingException("Unable to create instance of target class " + clazz, e); } }
@Override @SuppressWarnings("unchecked") public <T> ObjectMapper<T> getMapper(Class<T> type) throws ObjectMappingException { Preconditions.checkNotNull(type, "type"); try { return (ObjectMapper<T>) mapperCache.get(type); } catch (ExecutionException e) { if (e.getCause() instanceof ObjectMappingException) { throw (ObjectMappingException) e.getCause(); } else { throw new ObjectMappingException(e); } } }
private Class<?> getInstantiableType(TypeToken<?> type, String configuredName) throws ObjectMappingException { Class<?> retClass; if (type.getRawType().isInterface() || Modifier.isAbstract(type.getRawType().getModifiers())) { if (configuredName == null) { throw new ObjectMappingException("No available configured type for instances of " + type); } else { try { retClass = Class.forName(configuredName); } catch (ClassNotFoundException e) { throw new ObjectMappingException("Unknown class of object " + configuredName, e); } } } else { retClass = type.getRawType(); } return retClass; }
@Override public Pattern deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { try { return Pattern.compile(value.getString()); } catch (PatternSyntaxException ex) { throw new ObjectMappingException(ex); } }
@Override public UUID deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { try { return UUID.fromString(value.getString()); } catch (IllegalArgumentException ex) { throw new ObjectMappingException("Value not a UUID", ex); } }
private Class<?> getInstantiableType(TypeToken<?> type, String configuredName) throws ObjectMappingException { Class<?> retClass; if (type.getRawType().isInterface() || Modifier.isAbstract(type.getRawType().getModifiers())) { if (configuredName == null) { throw new ObjectMappingException("No available configured type for instances of " + type); } else { try { retClass = Class.forName(configuredName); } catch (ClassNotFoundException e) { throw new ObjectMappingException("Unknown class of object " + configuredName, e); } if (!type.getRawType().isAssignableFrom(retClass)) { throw new ObjectMappingException("Configured type " + configuredName + " does not extend " + type.getRawType().getCanonicalName()); } } } else { retClass = type.getRawType(); } return retClass; }
private Class<?> getInstantiableType(TypeToken<?> type, String configuredName) throws ObjectMappingException { Class<?> retClass; if (type.getRawType().isInterface() || Modifier.isAbstract(type.getRawType().getModifiers())) { if (configuredName == null) { throw new ObjectMappingException("No available configured type for instances of " + type); } else { try { retClass = Class.forName(configuredName); } catch (ClassNotFoundException e) { throw new ObjectMappingException("Unknown class of object " + configuredName, e); } if (!type.getRawType().isAssignableFrom(retClass)) { throw new ObjectMappingException("Configured type " + configuredName + " does not extend " + type.getRawType().getCanonicalName()); } } } else { retClass = type.getRawType(); } return retClass; }
@Override public URI deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String plainUri = value.getString(); if (plainUri == null) { throw new ObjectMappingException("No value present in node " + value); } URI uri; try { uri = new URI(plainUri); } catch (URISyntaxException e) { throw new ObjectMappingException("Invalid URI string provided for " + value.getKey() + ": got " + plainUri); } return uri; }
@Override public URL deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { String plainUrl = value.getString(); if (plainUrl == null) { throw new ObjectMappingException("No value present in node " + value); } URL url; try { url = new URL(plainUrl); } catch (MalformedURLException e) { throw new ObjectMappingException("Invalid URL string provided for " + value.getKey() + ": got " + plainUrl); } return url; }