Search in sources :

Example 1 with TypedConfig

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;
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) AtomixRegistry(io.atomix.core.AtomixRegistry) Config(com.typesafe.config.Config) TypedConfig(io.atomix.utils.config.TypedConfig) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Collectors(java.util.stream.Collectors) ConfigurationException(io.atomix.utils.config.ConfigurationException) ConfigMapper(io.atomix.utils.config.ConfigMapper) Objects(java.util.Objects) List(java.util.List) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) ConfigurationException(io.atomix.utils.config.ConfigurationException)

Aggregations

Joiner (com.google.common.base.Joiner)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Config (com.typesafe.config.Config)1 AtomixRegistry (io.atomix.core.AtomixRegistry)1 ConfigMapper (io.atomix.utils.config.ConfigMapper)1 ConfigurationException (io.atomix.utils.config.ConfigurationException)1 TypedConfig (io.atomix.utils.config.TypedConfig)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1