<commit_before>package net.wizardsoflua.lua.extension;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.util.ServiceConfigurationError;
import java.util.function.Consumer;

import net.sandius.rembulan.Table;
import net.wizardsoflua.lua.Converters;
import net.wizardsoflua.lua.extension.spi.ConverterExtension;
import net.wizardsoflua.lua.extension.spi.LuaExtension;

public class SpellExtensionLoader
    implements net.wizardsoflua.lua.extension.api.service.LuaExtensionLoader {
  private final ClassIndex extensions = new ClassIndex();
  private final Table env;
  private final ServiceInjector injector;
  private final Converters converters;

  public SpellExtensionLoader(Table env, ServiceInjector injector, Converters converters) {
    this.env = checkNotNull(env, "env == null!");
    this.injector = checkNotNull(injector, "injector == null!");
    this.converters = requireNonNull(converters, "converters == null!");
  }

  public ServiceInjector getInjector() {
    return injector;
  }

  public void installExtensions() {
    ServiceLoader.load(LuaExtension.class).forEach(this::getLuaExtension);
    ServiceLoader.load(ConverterExtension.class).forEach(this::getConverterExtension);
  }

  @Override
  public <E extends LuaExtension> E getLuaExtension(Class<E> extensionClass) {
    return getExtension(extensionClass, extension -> {
      injector.injectServicesInto(extension);
      extension.installInto(env);
    });
  }

  public <E extends ConverterExtension<?, ?>> E getConverterExtension(Class<E> extensionClass) {
    return getExtension(extensionClass, extension -> {
      injector.injectServicesInto(extension);
      converters.addConverterExtension(extension);
    });
  }

  private <E> E getExtension(Class<E> extensionClass, Consumer<E> initializer)
      throws ServiceConfigurationError {
    E extension = extensions.get(extensionClass);
    if (extension == null) {
      extension = newInstance(extensionClass);
      extensions.add(extension);
      initializer.accept(extension);
    }
    return extension;
  }

  private static <P> P newInstance(Class<P> cls) throws ServiceConfigurationError {
    try {
      return cls.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
      String message = "Provider " + cls + " could not be instantiated";<commit_msg>Fix initialization of hybrid extensions<commit_after>package net.wizardsoflua.lua.extension;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.util.ServiceConfigurationError;

import net.sandius.rembulan.Table;
import net.wizardsoflua.lua.Converters;
import net.wizardsoflua.lua.extension.spi.ConverterExtension;
import net.wizardsoflua.lua.extension.spi.LuaExtension;

public class SpellExtensionLoader
    implements net.wizardsoflua.lua.extension.api.service.LuaExtensionLoader {
  private final ClassIndex extensions = new ClassIndex();
  private final ClassIndex converterExtensions = new ClassIndex();
  private final ClassIndex luaExtensions = new ClassIndex();
  private final Table env;
  private final ServiceInjector injector;
  private final Converters converters;

  public SpellExtensionLoader(Table env, ServiceInjector injector, Converters converters) {
    this.env = checkNotNull(env, "env == null!");
    this.injector = checkNotNull(injector, "injector == null!");
    this.converters = requireNonNull(converters, "converters == null!");
  }

  public ServiceInjector getInjector() {
    return injector;
  }

  public void installExtensions() {
    ServiceLoader.load(ConverterExtension.class).forEach(this::getConverterExtension);
    ServiceLoader.load(LuaExtension.class).forEach(this::getLuaExtension);
  }

  public <E extends ConverterExtension<?, ?>> E getConverterExtension(Class<E> extensionClass) {
    E extension = converterExtensions.get(extensionClass);
    if (extension == null) {
      extension = getExtension(extensionClass);
      converterExtensions.add(extension);
      converters.addConverterExtension(extension);
    }
    return extension;
  }

  @Override
  public <E extends LuaExtension> E getLuaExtension(Class<E> extensionClass) {
    E extension = luaExtensions.get(extensionClass);
    if (extension == null) {
      extension = getExtension(extensionClass);
      luaExtensions.add(extension);
      extension.installInto(env);
    }
    return extension;
  }

  private <E> E getExtension(Class<E> extensionClass) throws ServiceConfigurationError {
    E extension = extensions.get(extensionClass);
    if (extension == null) {
      extension = newInstance(extensionClass);
      extensions.add(extension);
      injector.injectServicesInto(extension);
    }
    return extension;
  }

  private static <P> P newInstance(Class<P> cls) throws ServiceConfigurationError {
    try {
      return cls.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
      String message = "Provider " + cls + " could not be instantiated";
{'fraction_non_alphanumeric': 0.09760408483896307, 'fraction_numerical': 0.0, 'mean_word_length': 4.76131221719457, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5045344', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1402, 'n_words': 307}