use of org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException in project ignite-3 by apache.
the class ConfigurationUtil method findEx.
/**
* Finds a node or a leaf by the given path.
*
* @param path Path.
* @param rootNode Root node.
* @param <T> Arbitrary result type.
* @return Node or leaf.
*/
@Nullable
public static <T> T findEx(List<KeyPathNode> path, InnerNode rootNode) {
try {
var visitor = new ConfigurationVisitor<T>() {
private final int pathSize = path.size();
/**
* Current index of the key in the {@code path}.
*/
private int idx;
/**
* {@inheritDoc}
*/
@Override
public T visitLeafNode(String key, Serializable val) {
if (idx != pathSize) {
throw new KeyNotFoundException("Configuration value '" + joinPath(path.subList(0, idx)) + "' is a leaf");
} else {
return (T) val;
}
}
/**
* {@inheritDoc}
*/
@Override
public T visitInnerNode(String key, InnerNode node) {
if (node == null) {
throw new KeyNotFoundException("Configuration node '" + joinPath(path.subList(0, idx)) + "' is null");
} else if (idx == pathSize) {
return (T) node;
} else {
try {
KeyPathNode pathNode = path.get(idx++);
assert !pathNode.unresolvedName;
if (INTERNAL_ID.equals(pathNode.key)) {
// It's impossible to get this value with a regular traversal. Just call a method.
return (T) node.internalId();
} else if (INJECTED_NAME.equals(pathNode.key)) {
// It's impossible to get this value with a regular traversal. Just call a method.
return (T) node.getInjectedNameFieldValue();
}
return node.traverseChild(pathNode.key, this, true);
} catch (NoSuchElementException e) {
throw new KeyNotFoundException("Configuration value '" + joinPath(path.subList(0, idx)) + "' has not been found");
} catch (ConfigurationWrongPolymorphicTypeIdException e) {
assert false : e;
return null;
}
}
}
/**
* {@inheritDoc}
*/
@Override
public T visitNamedListNode(String key, NamedListNode<?> node) {
if (idx == pathSize) {
return (T) node;
} else {
KeyPathNode pathNode = path.get(idx++);
assert pathNode.namedListEntry;
String name = pathNode.unresolvedName ? pathNode.key : node.keyByInternalId(UUID.fromString(pathNode.key));
return visitInnerNode(name, node.getInnerNode(name));
}
}
};
return rootNode.accept(null, visitor);
} catch (KeyNotFoundException e) {
throw new NoSuchElementException(joinPath(path));
}
}
use of org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException in project ignite-3 by apache.
the class HoconObjectConfigurationSource method descend.
/**
* {@inheritDoc}
*/
@Override
public void descend(ConstructableTreeNode node) {
for (Map.Entry<String, ConfigValue> entry : hoconCfgObject.entrySet()) {
String key = entry.getKey();
if (key.equals(ignoredKey)) {
continue;
}
ConfigValue hoconCfgValue = entry.getValue();
try {
switch(hoconCfgValue.valueType()) {
case NULL:
node.construct(key, null, false);
break;
case OBJECT:
{
List<String> path = appendKey(this.path, key);
node.construct(key, new HoconObjectConfigurationSource(null, path, (ConfigObject) hoconCfgValue), false);
break;
}
case LIST:
{
List<String> path = appendKey(this.path, key);
node.construct(key, new HoconListConfigurationSource(path, (ConfigList) hoconCfgValue), false);
break;
}
default:
{
List<String> path = appendKey(this.path, key);
node.construct(key, new HoconPrimitiveConfigurationSource(path, hoconCfgValue), false);
}
}
} catch (NoSuchElementException e) {
if (path.isEmpty()) {
throw new IllegalArgumentException(format("'%s' configuration root doesn't exist", key), e);
} else {
throw new IllegalArgumentException(format("'%s' configuration doesn't have the '%s' sub-configuration", join(path), key), e);
}
} catch (ConfigurationWrongPolymorphicTypeIdException e) {
throw new IllegalArgumentException("Polymorphic configuration type is not correct: " + e.getMessage());
}
}
}
use of org.apache.ignite.configuration.ConfigurationWrongPolymorphicTypeIdException in project ignite-3 by apache.
the class ConfigurationAsmGenerator method addCfgImplPolymorphicInstanceConfigTypeMethod.
/**
* Add {@link DynamicConfiguration#polymorphicInstanceConfigType} method implementation to the class.
*
* <p>It looks like the following code:
* <pre><code>
* public Class polymorphicInstanceConfigType() {
* InnerNode val = this.isRemovedFromNamedList() ? this.currentValue() : this.refreshValue();
* String typeId = val.polymorphicTypeId;
* switch(typeId) {
* case "hash":
* return HashIndexConfiguration.class;
* case "sorted"
* return SortedIndexConfiguration.class;
* default:
* throw new ConfigurationWrongPolymorphicTypeIdException(typeId);
* }
* }
* </code></pre>
*
* @param classDef Class definition.
* @param schemaClass Polymorphic configuration schema (parent).
* @param polymorphicExtensions Polymorphic configuration instance schemas (children).
* @param polymorphicTypeIdFieldDef Identification field for the polymorphic configuration instance.
*/
private static void addCfgImplPolymorphicInstanceConfigTypeMethod(ClassDefinition classDef, Class<?> schemaClass, Set<Class<?>> polymorphicExtensions, FieldDefinition polymorphicTypeIdFieldDef) {
MethodDefinition polymorphicInstanceConfigTypeMtd = classDef.declareMethod(of(PUBLIC), "polymorphicInstanceConfigType", type(Class.class));
// String tmpStr;
Variable tmpStrVar = polymorphicInstanceConfigTypeMtd.getScope().createTempVariable(String.class);
StringSwitchBuilder switchBuilder = new StringSwitchBuilder(polymorphicInstanceConfigTypeMtd.getScope()).expression(tmpStrVar).defaultCase(throwException(ConfigurationWrongPolymorphicTypeIdException.class, tmpStrVar));
for (Class<?> polymorphicExtension : polymorphicExtensions) {
switchBuilder.addCase(polymorphicInstanceId(polymorphicExtension), constantClass(typeFromJavaClassName(configurationClassName(polymorphicExtension))).ret());
}
// ConfigNode
ParameterizedType nodeType = typeFromJavaClassName(nodeClassName(schemaClass));
// Object tmpObj;
Variable tmpObjVar = polymorphicInstanceConfigTypeMtd.getScope().createTempVariable(Object.class);
// this;
Variable thisVar = polymorphicInstanceConfigTypeMtd.getThis();
// tmpObj = this.isRemovedFromNamedList() ? this.currentValue() : this.refreshValue();
// tmpStr = ((ConfigNode) tmpObj).typeId;
// switch(tmpStr) ...
polymorphicInstanceConfigTypeMtd.getBody().append(tmpObjVar.set(inlineIf(thisVar.invoke(IS_REMOVED_FROM_NAMED_LIST_MTD), thisVar.invoke(CURRENT_VALUE_MTD), thisVar.invoke(REFRESH_VALUE_MTD)))).append(tmpStrVar.set(tmpObjVar.cast(nodeType).getField(polymorphicTypeIdFieldDef.getName(), String.class))).append(switchBuilder.build()).ret();
}
Aggregations