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