1 16 17 package com.google.inject; 18 19 import com.google.inject.Key.AnnotationStrategy; 20 import com.google.inject.binder.ConstantBindingBuilder; 21 import com.google.inject.binder.AnnotatedConstantBindingBuilder; 22 import com.google.inject.spi.SourceProviders; 23 import com.google.inject.util.Annotations; 24 import com.google.inject.util.StackTraceElements; 25 import com.google.inject.util.Objects; 26 import java.lang.annotation.Annotation ; 27 28 31 class ConstantBindingBuilderImpl implements AnnotatedConstantBindingBuilder, 32 ConstantBindingBuilder { 33 34 BindingInfo<?> bindingInfo; 35 AnnotationStrategy annotationStrategy; 36 final Object source; 37 private BinderImpl binder; 38 39 ConstantBindingBuilderImpl(BinderImpl binder, Object source) { 40 this.binder = binder; 41 this.source = source; 42 } 43 44 public ConstantBindingBuilder annotatedWith( 45 Class <? extends Annotation > annotationType) { 46 Objects.nonNull(annotationType, "annotation type"); 47 validateAnnotation(annotationType); 48 annotationStrategy = Key.strategyFor(annotationType); 49 return this; 50 } 51 52 public ConstantBindingBuilder annotatedWith(Annotation annotation) { 53 Objects.nonNull(annotation, "annotation"); 54 validateAnnotation(annotation.annotationType()); 55 annotationStrategy = Key.strategyFor(annotation); 56 return this; 57 } 58 59 void validateAnnotation(Class <? extends Annotation > annotationType) { 60 if (annotationStrategy != null) { 61 binder.addError(source, ErrorMessages.ANNOTATION_ALREADY_SPECIFIED); 62 return; 63 } 64 65 boolean retainedAtRuntime = 66 Annotations.isRetainedAtRuntime(annotationType); 67 boolean bindingAnnotation = Key.isBindingAnnotation(annotationType); 68 69 if (!retainedAtRuntime) { 70 binder.addError(StackTraceElements.forType(annotationType), 71 ErrorMessages.MISSING_RUNTIME_RETENTION, source); 72 } 73 74 if (!bindingAnnotation) { 75 binder.addError(StackTraceElements.forType(annotationType), 76 ErrorMessages.MISSING_BINDING_ANNOTATION, source); 77 } 78 } 79 80 boolean hasValue() { 81 return bindingInfo != null; 82 } 83 84 Object getSource() { 85 return source; 86 } 87 88 public void to(String value) { 89 to(String .class, value); 90 } 91 92 public void to(int value) { 93 to(int.class, value); 94 } 95 96 public void to(long value) { 97 to(long.class, value); 98 } 99 100 public void to(boolean value) { 101 to(boolean.class, value); 102 } 103 104 public void to(double value) { 105 to(double.class, value); 106 } 107 108 public void to(float value) { 109 to(float.class, value); 110 } 111 112 public void to(short value) { 113 to(short.class, value); 114 } 115 116 public void to(char value) { 117 to(char.class, value); 118 } 119 120 public void to(Class <?> value) { 121 to(Class .class, value); 122 } 123 124 public <E extends Enum <E>> void to(E value) { 125 to(value.getDeclaringClass(), value); 126 } 127 128 131 <T> void to(final Class <T> type, final T value) { 132 if (this.bindingInfo != null) { 133 binder.addError(source, ErrorMessages.CONSTANT_VALUE_ALREADY_SET); 134 } else { 135 this.bindingInfo 136 = new BindingInfo<T>(type, value, annotationStrategy, source); 137 } 138 } 139 140 BindingImpl<?> createBinding(InjectorImpl injector) { 141 return bindingInfo.createBinding(injector); 142 } 143 144 private static class BindingInfo<T> { 145 146 final Class <T> type; 147 final T value; 148 final AnnotationStrategy annotationStrategy; 149 final Object source; 150 151 BindingInfo(Class <T> type, T value, 152 AnnotationStrategy annotationStrategy, Object source) { 153 this.type = type; 154 this.value = value; 155 this.annotationStrategy = annotationStrategy; 156 this.source = source; 157 } 158 159 BindingImpl<T> createBinding(InjectorImpl injector) { 160 Key<T> key = Key.get(type, annotationStrategy); 161 ConstantFactory<T> factory = new ConstantFactory<T>(value); 162 return BindingImpl.newInstance(injector, key, source, factory); 163 } 164 } 165 } 166 | Popular Tags |