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