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