001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.reflect;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import com.google.common.collect.FluentIterable;
020import com.google.common.collect.ImmutableList;
021import java.lang.annotation.Annotation;
022import java.lang.reflect.AnnotatedElement;
023import javax.annotation.CheckForNull;
024import org.checkerframework.checker.nullness.qual.Nullable;
025
026/**
027 * Represents a method or constructor parameter.
028 *
029 * @author Ben Yu
030 * @since 14.0
031 */
032@ElementTypesAreNonnullByDefault
033public final class Parameter implements AnnotatedElement {
034
035  private final Invokable<?, ?> declaration;
036  private final int position;
037  private final TypeToken<?> type;
038  private final ImmutableList<Annotation> annotations;
039
040  /**
041   * An {@code AnnotatedType} instance, or {@code null} under Android VMs (possible only when using
042   * the Android flavor of Guava). The field is declared with a type of {@code Object} to avoid
043   * compatibility problems on Android VMs. The corresponding accessor method, however, can have the
044   * more specific return type as long as users are careful to guard calls to it with version checks
045   * or reflection: Android VMs ignore the types of elements that aren't used.
046   */
047  private final @Nullable Object annotatedType;
048
049  Parameter(
050      Invokable<?, ?> declaration,
051      int position,
052      TypeToken<?> type,
053      Annotation[] annotations,
054      @Nullable Object annotatedType) {
055    this.declaration = declaration;
056    this.position = position;
057    this.type = type;
058    this.annotations = ImmutableList.copyOf(annotations);
059    this.annotatedType = annotatedType;
060  }
061
062  /** Returns the type of the parameter. */
063  public TypeToken<?> getType() {
064    return type;
065  }
066
067  /** Returns the {@link Invokable} that declares this parameter. */
068  public Invokable<?, ?> getDeclaringInvokable() {
069    return declaration;
070  }
071
072  @Override
073  public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
074    return getAnnotation(annotationType) != null;
075  }
076
077  @Override
078  @CheckForNull
079  public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
080    checkNotNull(annotationType);
081    for (Annotation annotation : annotations) {
082      if (annotationType.isInstance(annotation)) {
083        return annotationType.cast(annotation);
084      }
085    }
086    return null;
087  }
088
089  @Override
090  public Annotation[] getAnnotations() {
091    return getDeclaredAnnotations();
092  }
093
094  /**
095   * @since 18.0
096   */
097  @Override
098  public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
099    return getDeclaredAnnotationsByType(annotationType);
100  }
101
102  /** @since 18.0 */
103  @Override
104  public Annotation[] getDeclaredAnnotations() {
105    return annotations.toArray(new Annotation[0]);
106  }
107
108  /**
109   * @since 18.0
110   */
111  @Override
112  @CheckForNull
113  public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) {
114    checkNotNull(annotationType);
115    return FluentIterable.from(annotations).filter(annotationType).first().orNull();
116  }
117
118  /**
119   * @since 18.0
120   */
121  @Override
122  public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) {
123    @Nullable
124    A[] result = FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
125    @SuppressWarnings("nullness") // safe because the input list contains no nulls
126    A[] cast = (A[]) result;
127    return cast;
128  }
129
130  @Override
131  public boolean equals(@CheckForNull Object obj) {
132    if (obj instanceof Parameter) {
133      Parameter that = (Parameter) obj;
134      return position == that.position && declaration.equals(that.declaration);
135    }
136    return false;
137  }
138
139  @Override
140  public int hashCode() {
141    return position;
142  }
143
144  @Override
145  public String toString() {
146    return type + " arg" + position;
147  }
148}