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; |
| 18 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.Annotations; |
| 20 | import com.google.inject.internal.Errors; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 21 | import static com.google.inject.internal.Preconditions.checkNotNull; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.ScopeBinding; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 23 | import java.lang.annotation.Annotation; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Handles {@link Binder#bindScope} commands. |
| 27 | * |
| 28 | * @author crazybob@google.com (Bob Lee) |
| 29 | * @author jessewilson@google.com (Jesse Wilson) |
| 30 | */ |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 31 | class ScopeBindingProcessor extends AbstractProcessor { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 32 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 33 | ScopeBindingProcessor(Errors errors) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 34 | super(errors); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 35 | } |
| 36 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 37 | @Override public Boolean visit(ScopeBinding command) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 38 | Scope scope = command.getScope(); |
| 39 | Class<? extends Annotation> annotationType = command.getAnnotationType(); |
| 40 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 41 | if (!Annotations.isScopeAnnotation(annotationType)) { |
limpbizkit | 4ce9cfa | 2008-08-14 03:35:16 +0000 | [diff] [blame] | 42 | errors.withSource(annotationType).missingScopeAnnotation(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 43 | // Go ahead and bind anyway so we don't get collateral errors. |
| 44 | } |
| 45 | |
| 46 | if (!Annotations.isRetainedAtRuntime(annotationType)) { |
limpbizkit | 4ce9cfa | 2008-08-14 03:35:16 +0000 | [diff] [blame] | 47 | errors.withSource(annotationType) |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 48 | .missingRuntimeRetention(command.getSource()); |
| 49 | // Go ahead and bind anyway so we don't get collateral errors. |
| 50 | } |
| 51 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 52 | Scope existing = injector.state.getScope(checkNotNull(annotationType, "annotation type")); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 53 | if (existing != null) { |
| 54 | errors.duplicateScopes(existing, annotationType, scope); |
| 55 | } else { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 56 | injector.state.putAnnotation(annotationType, checkNotNull(scope, "scope")); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | } |