blob: 2f9a3a407f57531abe8f9228549e6bdaeaf0dcb3 [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
limpbizkitdf98fcd2008-06-14 05:02:15 +000019import static com.google.common.base.Preconditions.checkNotNull;
limpbizkit3d58d6b2008-03-08 16:11:47 +000020import com.google.inject.commands.BindScopeCommand;
21import com.google.inject.internal.Annotations;
limpbizkit564053f2008-06-15 11:21:18 +000022import com.google.inject.internal.Errors;
limpbizkit3d58d6b2008-03-08 16:11:47 +000023import com.google.inject.internal.StackTraceElements;
limpbizkit3d58d6b2008-03-08 16:11:47 +000024import java.lang.annotation.Annotation;
25import java.util.Map;
26
27/**
28 * Handles {@link Binder#bindScope} commands.
29 *
30 * @author crazybob@google.com (Bob Lee)
31 * @author jessewilson@google.com (Jesse Wilson)
32 */
33class ScopesCommandProcessor extends CommandProcessor {
34
35 private final Map<Class<? extends Annotation>, Scope> scopes;
36
limpbizkit564053f2008-06-15 11:21:18 +000037 ScopesCommandProcessor(Errors errors,
limpbizkitad94bcb2008-04-28 00:22:43 +000038 Map<Class<? extends Annotation>, Scope> scopes) {
limpbizkit564053f2008-06-15 11:21:18 +000039 super(errors);
limpbizkit3d58d6b2008-03-08 16:11:47 +000040 this.scopes = scopes;
41 }
42
43 @Override public Boolean visitBindScope(BindScopeCommand command) {
44 Scope scope = command.getScope();
45 Class<? extends Annotation> annotationType = command.getAnnotationType();
46
47 if (!Scopes.isScopeAnnotation(annotationType)) {
limpbizkit564053f2008-06-15 11:21:18 +000048 errors.at(StackTraceElements.forType(annotationType)).missingScopeAnnotation();
limpbizkit3d58d6b2008-03-08 16:11:47 +000049 // Go ahead and bind anyway so we don't get collateral errors.
50 }
51
52 if (!Annotations.isRetainedAtRuntime(annotationType)) {
limpbizkit564053f2008-06-15 11:21:18 +000053 errors.at(StackTraceElements.forType(annotationType))
54 .missingRuntimeRetention(command.getSource());
limpbizkit3d58d6b2008-03-08 16:11:47 +000055 // Go ahead and bind anyway so we don't get collateral errors.
56 }
57
kevinb9n1601ae52008-06-03 22:21:04 +000058 Scope existing = scopes.get(checkNotNull(annotationType, "annotation type"));
limpbizkit3d58d6b2008-03-08 16:11:47 +000059 if (existing != null) {
limpbizkit163c48a2008-06-16 02:58:08 +000060 errors.duplicateScopes(existing, annotationType, scope);
limpbizkit3d58d6b2008-03-08 16:11:47 +000061 } else {
kevinb9n1601ae52008-06-03 22:21:04 +000062 scopes.put(annotationType, checkNotNull(scope, "scope"));
limpbizkit3d58d6b2008-03-08 16:11:47 +000063 }
64
65 return true;
66 }
67}