001package io.avaje.inject.spi; 002 003import io.avaje.inject.BeanScope; 004import io.avaje.inject.RequestScopeProvider; 005import jakarta.inject.Provider; 006 007import java.util.List; 008import java.util.Optional; 009import java.util.Set; 010import java.util.function.Consumer; 011 012/** 013 * Mutable builder object used when building a bean scope. 014 */ 015public interface Builder { 016 017 /** 018 * Create the root level Builder. 019 * 020 * @param suppliedBeans The list of beans (typically test doubles) supplied when building the context. 021 * @param enrichBeans The list of classes we want to have with mockito spy enhancement 022 */ 023 @SuppressWarnings("rawtypes") 024 static Builder newBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enrichBeans) { 025 if (suppliedBeans.isEmpty() && enrichBeans.isEmpty()) { 026 // simple case, no mocks or spies 027 return new DBuilder(); 028 } 029 return new DBuilderExtn(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 a request scoped bean provider. 055 * 056 * @param type The type of the bean being provided 057 * @param provider The provider 058 */ 059 <T> void requestScope(Class<T> type, RequestScopeProvider<T> provider); 060 061 /** 062 * Register a request scoped bean provider. 063 * 064 * @param name The qualifier name of the bean being provided 065 * @param type The type of the bean being provided 066 * @param provider The provider 067 */ 068 <T> void requestScope(Class<T> type, RequestScopeProvider<T> provider, String name, Class<?>... interfaceTypes); 069 070 /** 071 * Register the bean instance into the context. 072 * 073 * @param bean The bean instance that has been created. 074 */ 075 <T> T register(T bean); 076 077 /** 078 * Register the bean as a Primary bean. 079 */ 080 <T> T registerPrimary(T bean); 081 082 /** 083 * Register the bean as a secondary bean. 084 */ 085 <T> T registerSecondary(T bean); 086 087 /** 088 * Add lifecycle PostConstruct method. 089 */ 090 void addPostConstruct(Runnable runnable); 091 092 /** 093 * Add lifecycle PreDestroy method. 094 */ 095 void addPreDestroy(AutoCloseable closeable); 096 097 /** 098 * Add field and method injection. 099 */ 100 void addInjector(Consumer<Builder> injector); 101 102 /** 103 * Get an optional dependency. 104 */ 105 <T> Optional<T> getOptional(Class<T> cls); 106 107 /** 108 * Get an optional named dependency. 109 */ 110 <T> Optional<T> getOptional(Class<T> cls, String name); 111 112 /** 113 * Get an optional dependency potentially returning null. 114 */ 115 <T> T getNullable(Class<T> cls); 116 117 /** 118 * Get an optional named dependency potentially returning null. 119 */ 120 <T> T getNullable(Class<T> cls, String name); 121 122 /** 123 * Return Provider of T given the type. 124 */ 125 <T> Provider<T> getProvider(Class<T> cls); 126 127 /** 128 * Return Provider of T given the type and name. 129 */ 130 <T> Provider<T> getProvider(Class<T> cls, String name); 131 132 /** 133 * Get a dependency. 134 */ 135 <T> T get(Class<T> cls); 136 137 /** 138 * Get a named dependency. 139 */ 140 <T> T get(Class<T> cls, String name); 141 142 /** 143 * Get a list of dependencies for the interface type . 144 */ 145 <T> List<T> list(Class<T> interfaceType); 146 147 /** 148 * Get a set of dependencies for the interface type . 149 */ 150 <T> Set<T> set(Class<T> interfaceType); 151 152 /** 153 * Build and return the bean scope. 154 */ 155 BeanScope build(boolean withShutdownHook); 156}