blob: a19fd05fc191d34bcae4ea1acd87ace63215ee3e [file] [log] [blame]
limpbizkit3d58d6b2008-03-08 16:11:47 +00001/**
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
17package com.google.inject;
18
19import com.google.inject.commands.BindScopeCommand;
20import com.google.inject.internal.Annotations;
limpbizkitad94bcb2008-04-28 00:22:43 +000021import com.google.inject.internal.ErrorHandler;
22import com.google.inject.internal.ErrorMessages;
limpbizkit3d58d6b2008-03-08 16:11:47 +000023import static com.google.inject.internal.Objects.nonNull;
24import com.google.inject.internal.StackTraceElements;
limpbizkit3d58d6b2008-03-08 16:11:47 +000025
26import java.lang.annotation.Annotation;
27import 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 */
35class ScopesCommandProcessor extends CommandProcessor {
36
37 private final Map<Class<? extends Annotation>, Scope> scopes;
38
limpbizkitad94bcb2008-04-28 00:22:43 +000039 ScopesCommandProcessor(ErrorHandler errorHandler,
40 Map<Class<? extends Annotation>, Scope> scopes) {
41 super(errorHandler);
limpbizkit3d58d6b2008-03-08 16:11:47 +000042 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
61 Scope existing = scopes.get(nonNull(annotationType, "annotation type"));
62 if (existing != null) {
63 addError(command.getSource(), ErrorMessages.DUPLICATE_SCOPES, existing,
64 annotationType, scope);
65 } else {
66 scopes.put(annotationType, nonNull(scope, "scope"));
67 }
68
69 return true;
70 }
71}