Home | History | Annotate | Download | only in reflect
      1 /*
      2  * Copyright (C) 2012 The Guava Authors
      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.common.reflect;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import com.google.common.annotations.Beta;
     22 import com.google.common.collect.FluentIterable;
     23 import com.google.common.collect.ImmutableList;
     24 
     25 import java.lang.annotation.Annotation;
     26 import java.lang.reflect.AnnotatedElement;
     27 
     28 import javax.annotation.Nullable;
     29 
     30 /**
     31  * Represents a method or constructor parameter.
     32  *
     33  * @author Ben Yu
     34  * @since 14.0
     35  */
     36 @Beta
     37 public final class Parameter implements AnnotatedElement {
     38 
     39   private final Invokable<?, ?> declaration;
     40   private final int position;
     41   private final TypeToken<?> type;
     42   private final ImmutableList<Annotation> annotations;
     43 
     44   Parameter(
     45       Invokable<?, ?> declaration,
     46       int position,
     47       TypeToken<?> type,
     48       Annotation[] annotations) {
     49     this.declaration = declaration;
     50     this.position = position;
     51     this.type = type;
     52     this.annotations = ImmutableList.copyOf(annotations);
     53   }
     54 
     55   /** Returns the type of the parameter. */
     56   public TypeToken<?> getType() {
     57     return type;
     58   }
     59 
     60   /** Returns the {@link Invokable} that declares this parameter. */
     61   public Invokable<?, ?> getDeclaringInvokable() {
     62     return declaration;
     63   }
     64 
     65   @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
     66     return getAnnotation(annotationType) != null;
     67   }
     68 
     69   @Override
     70   @Nullable
     71   public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
     72     checkNotNull(annotationType);
     73     for (Annotation annotation : annotations) {
     74       if (annotationType.isInstance(annotation)) {
     75         return annotationType.cast(annotation);
     76       }
     77     }
     78     return null;
     79   }
     80 
     81   @Override public Annotation[] getAnnotations() {
     82     return getDeclaredAnnotations();
     83   }
     84 
     85   /**
     86    * @since 18.0
     87    */
     88   // @Override on JDK8
     89   public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
     90     return getDeclaredAnnotationsByType(annotationType);
     91   }
     92 
     93   /**
     94    * @since 18.0
     95    */
     96   // @Override on JDK8
     97   @Override public Annotation[] getDeclaredAnnotations() {
     98     return annotations.toArray(new Annotation[annotations.size()]);
     99   }
    100 
    101   /**
    102    * @since 18.0
    103    */
    104   // @Override on JDK8
    105   @Nullable
    106   public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) {
    107     checkNotNull(annotationType);
    108     return FluentIterable.from(annotations)
    109         .filter(annotationType)
    110         .first()
    111         .orNull();
    112   }
    113 
    114   /**
    115    * @since 18.0
    116    */
    117   // @Override on JDK8
    118   public <A extends Annotation> A[]
    119       getDeclaredAnnotationsByType(Class<A> annotationType) {
    120     return FluentIterable.from(annotations)
    121         .filter(annotationType)
    122         .toArray(annotationType);
    123   }
    124 
    125   @Override public boolean equals(@Nullable Object obj) {
    126     if (obj instanceof Parameter) {
    127       Parameter that = (Parameter) obj;
    128       return position == that.position && declaration.equals(that.declaration);
    129     }
    130     return false;
    131   }
    132 
    133   @Override public int hashCode() {
    134     return position;
    135   }
    136 
    137   @Override public String toString() {
    138     return type + " arg" + position;
    139   }
    140 }
    141