blob: c2a2894bf9d355b2cb81f9ef6596e8bfaca50c63 [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;
limpbizkitad94bcb2008-04-28 00:22:43 +000022import com.google.inject.internal.ErrorHandler;
limpbizkitdf98fcd2008-06-14 05:02:15 +000023import com.google.inject.internal.ErrorMessage;
limpbizkit3d58d6b2008-03-08 16:11:47 +000024import com.google.inject.internal.StackTraceElements;
limpbizkit3d58d6b2008-03-08 16:11:47 +000025import java.lang.annotation.Annotation;
26import 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 */
34class ScopesCommandProcessor extends CommandProcessor {
35
36 private final Map<Class<? extends Annotation>, Scope> scopes;
37
limpbizkitad94bcb2008-04-28 00:22:43 +000038 ScopesCommandProcessor(ErrorHandler errorHandler,
39 Map<Class<? extends Annotation>, Scope> scopes) {
40 super(errorHandler);
limpbizkit3d58d6b2008-03-08 16:11:47 +000041 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),
limpbizkitdf98fcd2008-06-14 05:02:15 +000050 ErrorMessage.missingScopeAnnotation());
limpbizkit3d58d6b2008-03-08 16:11:47 +000051 // Go ahead and bind anyway so we don't get collateral errors.
52 }
53
54 if (!Annotations.isRetainedAtRuntime(annotationType)) {
55 addError(StackTraceElements.forType(annotationType),
limpbizkitdf98fcd2008-06-14 05:02:15 +000056 ErrorMessage.missingRuntimeRetention(command.getSource()));
limpbizkit3d58d6b2008-03-08 16:11:47 +000057 // Go ahead and bind anyway so we don't get collateral errors.
58 }
59
kevinb9n1601ae52008-06-03 22:21:04 +000060 Scope existing = scopes.get(checkNotNull(annotationType, "annotation type"));
limpbizkit3d58d6b2008-03-08 16:11:47 +000061 if (existing != null) {
limpbizkitdf98fcd2008-06-14 05:02:15 +000062 addError(command.getSource(), ErrorMessage.duplicateScopes(existing, annotationType, scope));
limpbizkit3d58d6b2008-03-08 16:11:47 +000063 } else {
kevinb9n1601ae52008-06-03 22:21:04 +000064 scopes.put(annotationType, checkNotNull(scope, "scope"));
limpbizkit3d58d6b2008-03-08 16:11:47 +000065 }
66
67 return true;
68 }
69}