1/**
2 * Copyright (C) 2009 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.inject.internal;
18
19import com.google.common.base.Preconditions;
20import com.google.inject.Binder;
21import com.google.inject.Key;
22import com.google.inject.binder.AnnotatedElementBuilder;
23
24import java.lang.annotation.Annotation;
25
26/**
27 * For private binder's expose() method.
28 */
29public class ExposureBuilder<T> implements AnnotatedElementBuilder {
30  private final Binder binder;
31  private final Object source;
32  private Key<T> key;
33
34  public ExposureBuilder(Binder binder, Object source, Key<T> key) {
35    this.binder = binder;
36    this.source = source;
37    this.key = key;
38  }
39
40  protected void checkNotAnnotated() {
41    if (key.getAnnotationType() != null) {
42      binder.addError(AbstractBindingBuilder.ANNOTATION_ALREADY_SPECIFIED);
43    }
44  }
45
46  public void annotatedWith(Class<? extends Annotation> annotationType) {
47    Preconditions.checkNotNull(annotationType, "annotationType");
48    checkNotAnnotated();
49    key = Key.get(key.getTypeLiteral(), annotationType);
50  }
51
52  public void annotatedWith(Annotation annotation) {
53    Preconditions.checkNotNull(annotation, "annotation");
54    checkNotAnnotated();
55    key = Key.get(key.getTypeLiteral(), annotation);
56  }
57
58  public Key<?> getKey() {
59    return key;
60  }
61
62  public Object getSource() {
63    return source;
64  }
65
66  @Override public String toString() {
67    return "AnnotatedElementBuilder";
68  }
69}