limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 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 | |
| 17 | package com.google.inject.spi; |
| 18 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 19 | import com.google.inject.Binder; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 20 | import com.google.inject.Scope; |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 21 | import static com.google.common.base.Preconditions.checkNotNull; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 22 | import java.lang.annotation.Annotation; |
| 23 | |
| 24 | /** |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 25 | * Registration of a scope annotation with the scope that implements it. Instances are created |
| 26 | * explicitly in a module using {@link com.google.inject.Binder#bindScope(Class, Scope) bindScope()} |
| 27 | * statements: |
| 28 | * <pre> |
| 29 | * Scope recordScope = new RecordScope(); |
| 30 | * bindScope(RecordScoped.class, new RecordScope());</pre> |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 31 | * |
| 32 | * @author jessewilson@google.com (Jesse Wilson) |
limpbizkit | c489adf | 2008-11-18 07:01:33 +0000 | [diff] [blame] | 33 | * @since 2.0 |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 34 | */ |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 35 | public final class ScopeBinding implements Element { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 36 | private final Object source; |
| 37 | private final Class<? extends Annotation> annotationType; |
| 38 | private final Scope scope; |
| 39 | |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 40 | ScopeBinding(Object source, Class<? extends Annotation> annotationType, Scope scope) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 41 | this.source = checkNotNull(source, "source"); |
| 42 | this.annotationType = checkNotNull(annotationType, "annotationType"); |
| 43 | this.scope = checkNotNull(scope, "scope"); |
| 44 | } |
| 45 | |
| 46 | public Object getSource() { |
| 47 | return source; |
| 48 | } |
| 49 | |
| 50 | public Class<? extends Annotation> getAnnotationType() { |
| 51 | return annotationType; |
| 52 | } |
| 53 | |
| 54 | public Scope getScope() { |
| 55 | return scope; |
| 56 | } |
| 57 | |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 58 | public <T> T acceptVisitor(ElementVisitor<T> visitor) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 59 | return visitor.visit(this); |
| 60 | } |
| 61 | |
| 62 | public void applyTo(Binder binder) { |
| 63 | binder.withSource(getSource()).bindScope(annotationType, scope); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 64 | } |
| 65 | } |