1 16 17 package com.google.inject; 18 19 import com.google.inject.binder.ConstantBindingBuilder; 20 import com.google.inject.binder.AnnotatedBindingBuilder; 21 import com.google.inject.binder.LinkedBindingBuilder; 22 import com.google.inject.binder.AnnotatedConstantBindingBuilder; 23 import com.google.inject.matcher.Matcher; 24 import com.google.inject.spi.SourceProviders; 25 import com.google.inject.util.Objects; 26 import java.lang.annotation.Annotation ; 27 import java.lang.reflect.Method ; 28 import org.aopalliance.intercept.MethodInterceptor; 29 30 51 public abstract class AbstractModule implements Module { 52 53 static { 54 SourceProviders.skip(AbstractModule.class); 55 } 56 57 Binder builder; 58 59 public final synchronized void configure(Binder builder) { 60 try { 61 if (this.builder != null) { 62 throw new IllegalStateException ("Re-entry is not allowed."); 63 } 64 this.builder = Objects.nonNull(builder, "builder"); 65 66 configure(); 67 68 } 69 finally { 70 this.builder = null; 71 } 72 } 73 74 77 protected abstract void configure(); 78 79 82 protected Binder binder() { 83 return builder; 84 } 85 86 89 protected void bindScope(Class <? extends Annotation > scopeAnnotation, 90 Scope scope) { 91 builder.bindScope(scopeAnnotation, scope); 92 } 93 94 97 protected <T> LinkedBindingBuilder<T> bind(Key<T> key) { 98 return builder.bind(key); 99 } 100 101 104 protected <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { 105 return builder.bind(typeLiteral); 106 } 107 108 111 protected <T> AnnotatedBindingBuilder<T> bind(Class <T> clazz) { 112 return builder.bind(clazz); 113 } 114 115 118 protected AnnotatedConstantBindingBuilder bindConstant() { 119 return builder.bindConstant(); 120 } 121 122 125 protected void install(Module module) { 126 builder.install(module); 127 } 128 129 132 protected void addError(String message, Object ... arguments) { 133 builder.addError(message, arguments); 134 } 135 136 139 protected void addError(Throwable t) { 140 builder.addError(t); 141 } 142 143 146 protected void requestStaticInjection(Class <?>... types) { 147 builder.requestStaticInjection(types); 148 } 149 150 155 protected void bindInterceptor(Matcher<? super Class <?>> classMatcher, 156 Matcher<? super Method > methodMatcher, 157 MethodInterceptor... interceptors) { 158 builder.bindInterceptor(classMatcher, methodMatcher, interceptors); 159 } 160 } 161 | Popular Tags |