blob: 79bce81971b2e67aa898e04545127228caea0e54 [file] [log] [blame]
limpbizkit477f9f92008-07-28 07:05:14 +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
limpbizkit477f9f92008-07-28 07:05:14 +000019import com.google.inject.internal.Annotations;
20import com.google.inject.internal.Errors;
limpbizkit53664a72009-02-21 00:25:27 +000021import static com.google.inject.internal.Preconditions.checkNotNull;
limpbizkit00ca9f72008-08-02 17:56:17 +000022import com.google.inject.spi.ScopeBinding;
limpbizkit477f9f92008-07-28 07:05:14 +000023import java.lang.annotation.Annotation;
limpbizkit477f9f92008-07-28 07:05:14 +000024
25/**
26 * Handles {@link Binder#bindScope} commands.
27 *
28 * @author crazybob@google.com (Bob Lee)
29 * @author jessewilson@google.com (Jesse Wilson)
30 */
limpbizkit00ca9f72008-08-02 17:56:17 +000031class ScopeBindingProcessor extends AbstractProcessor {
limpbizkit477f9f92008-07-28 07:05:14 +000032
limpbizkitfcbdf992008-11-26 02:37:35 +000033 ScopeBindingProcessor(Errors errors) {
limpbizkit477f9f92008-07-28 07:05:14 +000034 super(errors);
limpbizkit477f9f92008-07-28 07:05:14 +000035 }
36
limpbizkit03b81a62009-03-18 05:34:39 +000037 @Override public Boolean visit(ScopeBinding command) {
limpbizkit477f9f92008-07-28 07:05:14 +000038 Scope scope = command.getScope();
39 Class<? extends Annotation> annotationType = command.getAnnotationType();
40
limpbizkitb3a8f0b2008-09-05 22:30:40 +000041 if (!Annotations.isScopeAnnotation(annotationType)) {
limpbizkit4ce9cfa2008-08-14 03:35:16 +000042 errors.withSource(annotationType).missingScopeAnnotation();
limpbizkit477f9f92008-07-28 07:05:14 +000043 // Go ahead and bind anyway so we don't get collateral errors.
44 }
45
46 if (!Annotations.isRetainedAtRuntime(annotationType)) {
limpbizkit4ce9cfa2008-08-14 03:35:16 +000047 errors.withSource(annotationType)
limpbizkit477f9f92008-07-28 07:05:14 +000048 .missingRuntimeRetention(command.getSource());
49 // Go ahead and bind anyway so we don't get collateral errors.
50 }
51
limpbizkitfcbdf992008-11-26 02:37:35 +000052 Scope existing = injector.state.getScope(checkNotNull(annotationType, "annotation type"));
limpbizkit477f9f92008-07-28 07:05:14 +000053 if (existing != null) {
54 errors.duplicateScopes(existing, annotationType, scope);
55 } else {
limpbizkitfcbdf992008-11-26 02:37:35 +000056 injector.state.putAnnotation(annotationType, checkNotNull(scope, "scope"));
limpbizkit477f9f92008-07-28 07:05:14 +000057 }
58
59 return true;
60 }
61}