All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.internal.ScopeNotFoundError Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package com.google.inject.internal;

import com.google.common.collect.Lists;
import com.google.inject.spi.ErrorDetail;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;

/** Error reported by Guice when a scope annotation is not bound to any scope implementation. */
final class ScopeNotFoundError extends InternalErrorDetail {

  private final Class scopeAnnotation;

  ScopeNotFoundError(Class scopeAnnotation, List sources) {
    super(
        ErrorId.SCOPE_NOT_FOUND,
        String.format("No scope is bound to %s.", Messages.convert(scopeAnnotation)),
        sources,
        null);
    this.scopeAnnotation = scopeAnnotation;
  }

  @Override
  public boolean isMergeable(ErrorDetail other) {
    return other instanceof ScopeNotFoundError
        && ((ScopeNotFoundError) other).scopeAnnotation.equals(scopeAnnotation);
  }

  @Override
  protected void formatDetail(List> mergeableErrors, Formatter formatter) {
    List> sourcesSet = new ArrayList<>();
    sourcesSet.add(getSources());
    mergeableErrors.stream().map(ErrorDetail::getSources).forEach(sourcesSet::add);

    formatter.format("%n%s%n", "Used at:");
    int sourceListIndex = 1;
    for (List sources : sourcesSet) {
      ErrorFormatter.formatSources(sourceListIndex++, Lists.reverse(sources), formatter);
    }
  }

  @Override
  public ScopeNotFoundError withSources(List newSources) {
    return new ScopeNotFoundError(scopeAnnotation, newSources);
  }
}