YAMLKeyValue (@NotNull YAMLDocument document, @NotNull List<String> key) { assert key.size() != 0; YAMLMapping mapping = ObjectUtils.tryCast(document.getTopLevelValue(), YAMLMapping.class); for (int i = 0; i < key.size(); i++) { if (mapping == null) { return null; } YAMLKeyValue keyValue = mapping.getKeyValueByKey(String.join(".", key.subList(i, key.size()))); if (keyValue != null) { return keyValue; } keyValue = mapping.getKeyValueByKey(key.get(i)); if (keyValue == null || i + 1 == key.size()) { return keyValue; } mapping = ObjectUtils.tryCast(keyValue.getValue(), YAMLMapping.class); } throw new IllegalStateException("Should have returned from the loop"); }