001package io.avaje.inject.spi; 002 003import io.avaje.inject.BeanScope; 004import javax.inject.Provider; 005 006import java.util.List; 007import java.util.Optional; 008import java.util.Set; 009import java.util.function.Consumer; 010 011/** 012 * Mutable builder object used when building a bean scope. 013 */ 014public interface Builder { 015 016 /** 017 * Create the root level Builder. 018 * 019 * @param suppliedBeans The list of beans (typically test doubles) supplied when building the context. 020 * @param enrichBeans The list of classes we want to have with mockito spy enhancement 021 * @param parent The parent BeanScope 022 */ 023 @SuppressWarnings("rawtypes") 024 static Builder newBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans, BeanScope parent) { 025 if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) { 026 // simple case, no mocks or spies 027 return new DBuilder(parent); 028 } 029 return new DBuilderExtn(parent, suppliedBeans, enrichBeans); 030 } 031 032 /** 033 * Return true if the bean should be created and registered with the context. 034 * <p/> 035 * Returning false means there has been a supplied bean already registered and 036 * that we should skip the creation and registration for this bean. 037 * 038 * @param name The qualifier name 039 * @param types The types that the bean implements and provides 040 */ 041 boolean isAddBeanFor(String name, Class<?>... types); 042 043 /** 044 * Return true if the bean should be created and registered with the context. 045 * <p/> 046 * Returning false means there has been a supplied bean already registered and 047 * that we should skip the creation and registration for this bean. 048 * 049 * @param types The types that the bean implements and provides 050 */ 051 boolean isAddBeanFor(Class<?>... types); 052 053 /** 054 * Register the bean instance into the context. 055 * 056 * @param bean The bean instance that has been created. 057 */ 058 <T> T register(T bean); 059 060 /** 061 * Register the bean as a Primary bean. 062 */ 063 <T> T registerPrimary(T bean); 064 065 /** 066 * Register the bean as a secondary bean. 067 */ 068 <T> T registerSecondary(T bean); 069 070 /** 071 * Add lifecycle PostConstruct method. 072 */ 073 void addPostConstruct(Runnable runnable); 074 075 /** 076 * Add lifecycle PreDestroy method. 077 */ 078 void addPreDestroy(AutoCloseable closeable); 079 080 /** 081 * Add field and method injection. 082 */ 083 void addInjector(Consumer<Builder> injector); 084 085 /** 086 * Get an optional dependency. 087 */ 088 <T> Optional<T> getOptional(Class<T> cls); 089 090 /** 091 * Get an optional named dependency. 092 */ 093 <T> Optional<T> getOptional(Class<T> cls, String name); 094 095 /** 096 * Get an optional dependency potentially returning null. 097 */ 098 <T> T getNullable(Class<T> cls); 099 100 /** 101 * Get an optional named dependency potentially returning null. 102 */ 103 <T> T getNullable(Class<T> cls, String name); 104 105 /** 106 * Return Provider of T given the type. 107 */ 108 <T> Provider<T> getProvider(Class<T> cls); 109 110 /** 111 * Return Provider of T given the type and name. 112 */ 113 <T> Provider<T> getProvider(Class<T> cls, String name); 114 115 /** 116 * Get a dependency. 117 */ 118 <T> T get(Class<T> cls); 119 120 /** 121 * Get a named dependency. 122 */ 123 <T> T get(Class<T> cls, String name); 124 125 /** 126 * Get a list of dependencies for the interface type . 127 */ 128 <T> List<T> list(Class<T> interfaceType); 129 130 /** 131 * Get a set of dependencies for the interface type . 132 */ 133 <T> Set<T> set(Class<T> interfaceType); 134 135 /** 136 * Build and return the bean scope. 137 */ 138 BeanScope build(boolean withShutdownHook); 139}