001package io.avaje.inject.spi;
002
003import org.mockito.Mockito;
004
005import java.util.function.Consumer;
006
007/**
008 * Holds Spy setup consumers for dependency injection using Mockito Spy.
009 */
010public final class EnrichBean<B> {
011
012  private final Class<B> type;
013  private final String name;
014  private final Consumer<B> consumer;
015
016  public EnrichBean(Class<B> type, String name, Consumer<B> consumer) {
017    this.type = type;
018    this.name = KeyUtil.lower(name);
019    this.consumer = consumer;
020  }
021
022  /**
023   * Return the spy enhanced bean instance to use.
024   */
025  public B enrich(B bean) {
026    // should extract a SPI for this. Only enrichment is Mockito spy at this point.
027    B spy = Mockito.spy(bean);
028    if (consumer != null) {
029      consumer.accept(spy);
030    }
031    return spy;
032  }
033
034  /**
035   * Return the key for this enriched bean.
036   */
037  public String key() {
038    return KeyUtil.key(type, name);
039  }
040}