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