use of io.atomix.utils.config.TypedConfig in project atomix by atomix.
the class PolymorphicConfigMapper method newInstance.
@Override
@SuppressWarnings("unchecked")
protected <T> T newInstance(Config config, String key, Class<T> clazz) {
T instance;
// If the class is a polymorphic type, look up the type mapper and get the concrete type.
if (isPolymorphicType(clazz)) {
PolymorphicTypeMapper typeMapper = polymorphicTypes.stream().filter(mapper -> mapper.getConfigClass().isAssignableFrom(clazz)).filter(mapper -> (mapper.getTypePath() != null && config.hasPath(mapper.getTypePath())) || mapper.getTypePath() == null).findFirst().orElse(null);
if (typeMapper == null) {
throw new ConfigurationException("Cannot instantiate abstract type " + clazz.getName());
}
String typeName = typeMapper.getTypePath() != null ? config.getString(typeMapper.getTypePath()) : key;
Class<? extends TypedConfig<?>> concreteClass = typeMapper.getConcreteClass(registry, typeName);
if (concreteClass == null) {
throw new ConfigurationException("Unknown " + key + " type '" + typeName + "'");
}
try {
instance = (T) concreteClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new ConfigurationException(concreteClass.getName() + " needs a public no-args constructor to be used as a bean", e);
}
} else {
try {
instance = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new ConfigurationException(clazz.getName() + " needs a public no-args constructor to be used as a bean", e);
}
}
return instance;
}
Aggregations