Home | History | Annotate | Download | only in codegen
      1 /*
      2  * Copyright (C) 2014 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 package dagger.internal.codegen;
     17 
     18 import com.google.auto.common.AnnotationMirrors;
     19 import com.google.common.base.Optional;
     20 import com.google.common.collect.ImmutableSet;
     21 import javax.inject.Qualifier;
     22 import javax.inject.Scope;
     23 import javax.lang.model.element.AnnotationMirror;
     24 import javax.lang.model.element.Element;
     25 
     26 import static com.google.common.base.Preconditions.checkNotNull;
     27 
     28 /**
     29  * Utilities relating to annotations defined in the {@code javax.inject} package.
     30  *
     31  * @author Gregory Kick
     32  * @since 2.0
     33  */
     34 final class InjectionAnnotations {
     35   static Optional<AnnotationMirror> getScopeAnnotation(Element e) {
     36     checkNotNull(e);
     37     ImmutableSet<? extends AnnotationMirror> scopeAnnotations = getScopes(e);
     38     switch (scopeAnnotations.size()) {
     39       case 0:
     40         return Optional.absent();
     41       case 1:
     42         return Optional.<AnnotationMirror>of(scopeAnnotations.iterator().next());
     43       default:
     44         throw new IllegalArgumentException(
     45             e + " was annotated with more than one @Scope annotation");
     46     }
     47   }
     48 
     49   static Optional<AnnotationMirror> getQualifier(Element e) {
     50     checkNotNull(e);
     51     ImmutableSet<? extends AnnotationMirror> qualifierAnnotations = getQualifiers(e);
     52     switch (qualifierAnnotations.size()) {
     53       case 0:
     54         return Optional.absent();
     55       case 1:
     56         return Optional.<AnnotationMirror>of(qualifierAnnotations.iterator().next());
     57       default:
     58         throw new IllegalArgumentException(
     59             e + " was annotated with more than one @Qualifier annotation");
     60     }
     61   }
     62 
     63   static ImmutableSet<? extends AnnotationMirror> getQualifiers(Element element) {
     64     return AnnotationMirrors.getAnnotatedAnnotations(element, Qualifier.class);
     65   }
     66 
     67   static ImmutableSet<? extends AnnotationMirror> getScopes(Element element) {
     68     return AnnotationMirrors.getAnnotatedAnnotations(element, Scope.class);
     69   }
     70 
     71   private InjectionAnnotations() {}
     72 }
     73