blob: e107c70548e3246415bc5deb359df40d3fa75367 [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit477f9f92008-07-28 07:05:14 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Scope;
limpbizkit53664a72009-02-21 00:25:27 +000020import static com.google.inject.internal.Preconditions.checkNotNull;
limpbizkit00ca9f72008-08-02 17:56:17 +000021import com.google.inject.spi.ScopeBinding;
limpbizkit477f9f92008-07-28 07:05:14 +000022import java.lang.annotation.Annotation;
limpbizkit477f9f92008-07-28 07:05:14 +000023
24/**
limpbizkit5ae41eb2009-06-06 17:51:27 +000025 * Handles {@code Binder.bindScope} commands.
limpbizkit477f9f92008-07-28 07:05:14 +000026 *
27 * @author crazybob@google.com (Bob Lee)
28 * @author jessewilson@google.com (Jesse Wilson)
29 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000030final class ScopeBindingProcessor extends AbstractProcessor {
limpbizkit477f9f92008-07-28 07:05:14 +000031
limpbizkitfcbdf992008-11-26 02:37:35 +000032 ScopeBindingProcessor(Errors errors) {
limpbizkit477f9f92008-07-28 07:05:14 +000033 super(errors);
limpbizkit477f9f92008-07-28 07:05:14 +000034 }
35
limpbizkit03b81a62009-03-18 05:34:39 +000036 @Override public Boolean visit(ScopeBinding command) {
limpbizkit477f9f92008-07-28 07:05:14 +000037 Scope scope = command.getScope();
38 Class<? extends Annotation> annotationType = command.getAnnotationType();
39
limpbizkitb3a8f0b2008-09-05 22:30:40 +000040 if (!Annotations.isScopeAnnotation(annotationType)) {
limpbizkit4ce9cfa2008-08-14 03:35:16 +000041 errors.withSource(annotationType).missingScopeAnnotation();
limpbizkit477f9f92008-07-28 07:05:14 +000042 // Go ahead and bind anyway so we don't get collateral errors.
43 }
44
45 if (!Annotations.isRetainedAtRuntime(annotationType)) {
limpbizkit4ce9cfa2008-08-14 03:35:16 +000046 errors.withSource(annotationType)
limpbizkit477f9f92008-07-28 07:05:14 +000047 .missingRuntimeRetention(command.getSource());
48 // Go ahead and bind anyway so we don't get collateral errors.
49 }
50
limpbizkitfcbdf992008-11-26 02:37:35 +000051 Scope existing = injector.state.getScope(checkNotNull(annotationType, "annotation type"));
limpbizkit477f9f92008-07-28 07:05:14 +000052 if (existing != null) {
53 errors.duplicateScopes(existing, annotationType, scope);
54 } else {
limpbizkitfcbdf992008-11-26 02:37:35 +000055 injector.state.putAnnotation(annotationType, checkNotNull(scope, "scope"));
limpbizkit477f9f92008-07-28 07:05:14 +000056 }
57
58 return true;
59 }
60}