limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +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 | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 20 | import com.google.inject.commands.BindScopeCommand; |
| 21 | import com.google.inject.internal.Annotations; |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 22 | import com.google.inject.internal.ErrorHandler; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 23 | import com.google.inject.internal.ErrorMessage; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 24 | import com.google.inject.internal.StackTraceElements; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 25 | import java.lang.annotation.Annotation; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * Handles {@link Binder#bindScope} commands. |
| 30 | * |
| 31 | * @author crazybob@google.com (Bob Lee) |
| 32 | * @author jessewilson@google.com (Jesse Wilson) |
| 33 | */ |
| 34 | class ScopesCommandProcessor extends CommandProcessor { |
| 35 | |
| 36 | private final Map<Class<? extends Annotation>, Scope> scopes; |
| 37 | |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 38 | ScopesCommandProcessor(ErrorHandler errorHandler, |
| 39 | Map<Class<? extends Annotation>, Scope> scopes) { |
| 40 | super(errorHandler); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 41 | this.scopes = scopes; |
| 42 | } |
| 43 | |
| 44 | @Override public Boolean visitBindScope(BindScopeCommand command) { |
| 45 | Scope scope = command.getScope(); |
| 46 | Class<? extends Annotation> annotationType = command.getAnnotationType(); |
| 47 | |
| 48 | if (!Scopes.isScopeAnnotation(annotationType)) { |
| 49 | addError(StackTraceElements.forType(annotationType), |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 50 | ErrorMessage.missingScopeAnnotation()); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 51 | // Go ahead and bind anyway so we don't get collateral errors. |
| 52 | } |
| 53 | |
| 54 | if (!Annotations.isRetainedAtRuntime(annotationType)) { |
| 55 | addError(StackTraceElements.forType(annotationType), |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 56 | ErrorMessage.missingRuntimeRetention(command.getSource())); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 57 | // Go ahead and bind anyway so we don't get collateral errors. |
| 58 | } |
| 59 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 60 | Scope existing = scopes.get(checkNotNull(annotationType, "annotation type")); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 61 | if (existing != null) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 62 | addError(command.getSource(), ErrorMessage.duplicateScopes(existing, annotationType, scope)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 63 | } else { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 64 | scopes.put(annotationType, checkNotNull(scope, "scope")); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | } |