private static SimpleModule resolveYamlTypeMappings()

in server/src/main/java/org/apache/cassandra/sidecar/config/yaml/SidecarConfigurationImpl.java [329:383]


    private static SimpleModule resolveYamlTypeMappings() throws IOException
    {
        String packageName = SidecarConfigurationImpl.class.getPackage().getName();
        String outerPackageName = SidecarConfiguration.class.getPackage().getName();
        SimpleModule module = new SimpleModule();
        ClassPath path = ClassPath.from(ClassLoader.getSystemClassLoader());
        Set<Class> declared = path.getTopLevelClasses(outerPackageName)
                                  .stream()
                                  .filter(c -> c.getName().endsWith("Configuration"))
                                  .map(ClassPath.ClassInfo::load)
                                  .collect(Collectors.toSet());
        Set<Class> implemented = new HashSet<>();
        ClassPath.from(ClassLoader.getSystemClassLoader()).getTopLevelClasses(packageName)
                 .stream()
                 .map(ClassPath.ClassInfo::load)
                 .forEach(clazz -> {
                     if (clazz.isInterface())
                     {
                         return;
                     }
                     // find the configuration interface it implements
                     // note: it assumes that the concrete implementation implement one and only one
                     //       configuration interface, and the name of the configuration interface ends
                     //       with "Configuration"
                     Class[] interfaces = clazz.getInterfaces();
                     Class configurationInterface = null;
                     for (Class c : interfaces)
                     {
                         if (c.getPackage().getName().equals(outerPackageName) && c.getName().endsWith("Configuration"))
                         {
                             configurationInterface = c;
                             if (!implemented.add(configurationInterface))
                             {
                                 throw new IllegalStateException("Multiple implementations found for " +
                                                                 "configuration interface: " + configurationInterface);
                             }
                             break;
                         }
                     }
                     // it does not implement any configuration interface
                     if (configurationInterface == null)
                     {
                         return;
                     }

                     module.addAbstractTypeMapping(configurationInterface, clazz);
                 });

        Set<Class> unimplemented = Sets.difference(declared, implemented);
        if (!unimplemented.isEmpty())
        {
            throw new IllegalStateException("Found unimplemented configuration class(es): " + unimplemented);
        }
        return module;
    }