Repository: open-telemetry/opentelemetry-java
File: sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/DeclarativeConfigContext.java (Java)

Code before:
   * exceptionally, and return it.
   */
  <T extends Closeable> T addCloseable(T closeable) {
    closeables.add(closeable);
    return closeable;
  }

  List<Closeable> getCloseables() {
    return Collections.unmodifiableList(closeables);
  }

  @Nullable
  public MeterProvider getMeterProvider() {
    return meterProvider;
  }

  public void setMeterProvider(MeterProvider meterProvider) {
    this.meterProvider = meterProvider;
  }

  Resource getResource() {
    // called via reflection from io.opentelemetry.sdk.autoconfigure.IncubatingUtil
    return resource;
  }

  void setResource(Resource resource) {
    this.resource = resource;
  }

  SpiHelper getSpiHelper() {
    return spiHelper;
  }

  /**
   * Find a registered {@link ComponentProvider} with {@link ComponentProvider#getType()} matching
   * {@code type}, {@link ComponentProvider#getName()} matching {@code name}, and call {@link
   * ComponentProvider#create(DeclarativeConfigProperties)} with the given {@code model}.
   *
   * @throws DeclarativeConfigException if no matching providers are found, or if multiple are found
   *     (i.e. conflict), or if {@link ComponentProvider#create(DeclarativeConfigProperties)} throws
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  <T> T loadComponent(Class<T> type, String name, Object model) {
    DeclarativeConfigProperties config =
        DeclarativeConfiguration.toConfigProperties(model, spiHelper.getComponentLoader());

    // TODO(jack-berg): cache loaded component providers
    List<ComponentProvider> componentProviders = spiHelper.load(ComponentProvider.class);
    List<ComponentProvider<?>> matchedProviders =
        componentProviders.stream()
            .map(

Review comment (suggestion):
Maybe throw an exception if `getResource` is called before `setResource` to make it easier to track down ordering issues.

Code after:
   * exceptionally, and return it.
   */
  <T extends Closeable> T addCloseable(T closeable) {
    closeables.add(closeable);
    return closeable;
  }

  List<Closeable> getCloseables() {
    return Collections.unmodifiableList(closeables);
  }

  @Nullable
  public MeterProvider getMeterProvider() {
    return meterProvider;
  }

  public void setMeterProvider(MeterProvider meterProvider) {
    this.meterProvider = meterProvider;
  }

  Resource getResource() {
    // called via reflection from io.opentelemetry.sdk.autoconfigure.IncubatingUtil
    if (resource == null) {
      throw new DeclarativeConfigException("Resource has not been configured yet.");
    }
    return resource;
  }

  void setResource(Resource resource) {
    this.resource = resource;
  }

  SpiHelper getSpiHelper() {
    return spiHelper;
  }

  /**
   * Find a registered {@link ComponentProvider} with {@link ComponentProvider#getType()} matching
   * {@code type}, {@link ComponentProvider#getName()} matching {@code name}, and call {@link
   * ComponentProvider#create(DeclarativeConfigProperties)} with the given {@code model}.
   *
   * @throws DeclarativeConfigException if no matching providers are found, or if multiple are found
   *     (i.e. conflict), or if {@link ComponentProvider#create(DeclarativeConfigProperties)} throws
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  <T> T loadComponent(Class<T> type, String name, Object model) {
    DeclarativeConfigProperties config =
        DeclarativeConfiguration.toConfigProperties(model, spiHelper.getComponentLoader());

    // TODO(jack-berg): cache loaded component providers
    List<ComponentProvider> componentProviders = spiHelper.load(ComponentProvider.class);