Home | History | Annotate | Download | only in internal
      1 /*
      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 
     17 package com.google.inject.internal;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import com.google.inject.Scope;
     22 import com.google.inject.spi.ScopeBinding;
     23 import java.lang.annotation.Annotation;
     24 
     25 /**
     26  * Handles {@code Binder.bindScope} commands.
     27  *
     28  * @author crazybob (at) google.com (Bob Lee)
     29  * @author jessewilson (at) google.com (Jesse Wilson)
     30  */
     31 final class ScopeBindingProcessor extends AbstractProcessor {
     32 
     33   ScopeBindingProcessor(Errors errors) {
     34     super(errors);
     35   }
     36 
     37   @Override
     38   public Boolean visit(ScopeBinding command) {
     39     Scope scope = checkNotNull(command.getScope(), "scope");
     40     Class<? extends Annotation> annotationType =
     41         checkNotNull(command.getAnnotationType(), "annotation type");
     42 
     43     if (!Annotations.isScopeAnnotation(annotationType)) {
     44       errors.missingScopeAnnotation(annotationType);
     45       // Go ahead and bind anyway so we don't get collateral errors.
     46     }
     47 
     48     if (!Annotations.isRetainedAtRuntime(annotationType)) {
     49       errors.missingRuntimeRetention(annotationType);
     50       // Go ahead and bind anyway so we don't get collateral errors.
     51     }
     52 
     53     ScopeBinding existing = injector.state.getScopeBinding(annotationType);
     54     if (existing != null) {
     55       if (!scope.equals(existing.getScope())) {
     56         errors.duplicateScopes(existing, annotationType, scope);
     57       }
     58     } else {
     59       injector.state.putScopeBinding(annotationType, command);
     60     }
     61 
     62     return true;
     63   }
     64 }
     65