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;
018import static java.util.Objects.requireNonNull;
019
020import com.google.common.collect.FluentIterable;
021import com.google.common.collect.ImmutableList;
022import java.lang.annotation.Annotation;
023import java.lang.reflect.AnnotatedElement;
024import java.lang.reflect.AnnotatedType;
025import javax.annotation.CheckForNull;
026import org.checkerframework.checker.nullness.qual.Nullable;
027
028/**
029 * Represents a method or constructor parameter.
030 *
031 * @author Ben Yu
032 * @since 14.0
033 */
034@ElementTypesAreNonnullByDefault
035public final class Parameter implements AnnotatedElement {
036
037  private final Invokable<?, ?> declaration;
038  private final int position;
039  private final TypeToken<?> type;
040  private final ImmutableList<Annotation> annotations;
041
042  /**
043   * An {@link AnnotatedType} instance, or {@code null} under Android VMs (possible only when using
044   * the Android flavor of Guava). The field is declared with a type of {@code Object} to avoid
045   * compatibility problems on Android VMs. The corresponding accessor method, however, can have the
046   * more specific return type as long as users are careful to guard calls to it with version checks
047   * or reflection: Android VMs ignore the types of elements that aren't used.
048   */
049  private final @Nullable Object annotatedType;
050
051  Parameter(
052      Invokable<?, ?> declaration,
053      int position,
054      TypeToken<?> type,
055      Annotation[] annotations,
056      @Nullable Object annotatedType) {
057    this.declaration = declaration;
058    this.position = position;
059    this.type = type;
060    this.annotations = ImmutableList.copyOf(annotations);
061    this.annotatedType = annotatedType;
062  }
063
064  /** Returns the type of the parameter. */
065  public TypeToken<?> getType() {
066    return type;
067  }
068
069  /** Returns the {@link Invokable} that declares this parameter. */
070  public Invokable<?, ?> getDeclaringInvokable() {
071    return declaration;
072  }
073
074  @Override
075  public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
076    return getAnnotation(annotationType) != null;
077  }
078
079  @Override
080  @CheckForNull
081  public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
082    checkNotNull(annotationType);
083    for (Annotation annotation : annotations) {
084      if (annotationType.isInstance(annotation)) {
085        return annotationType.cast(annotation);
086      }
087    }
088    return null;
089  }
090
091  @Override
092  public Annotation[] getAnnotations() {
093    return getDeclaredAnnotations();
094  }
095
096  /** @since 18.0 */
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  /** @since 18.0 */
109  @Override
110  @CheckForNull
111  public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) {
112    checkNotNull(annotationType);
113    return FluentIterable.from(annotations).filter(annotationType).first().orNull();
114  }
115
116  /** @since 18.0 */
117  @Override
118  public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) {
119    @Nullable
120    A[] result = FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
121    @SuppressWarnings("nullness") // safe because the input list contains no nulls
122    A[] cast = (A[]) result;
123    return cast;
124  }
125
126  /**
127   * Returns the {@link AnnotatedType} of the parameter.
128   *
129   * <p>This method will fail if run under an Android VM.
130   *
131   * @since 25.1 for guava-jre (available since 32.0.0 in guava-android)
132   */
133  @SuppressWarnings({"Java7ApiChecker", "AndroidJdkLibsChecker"})
134  @IgnoreJRERequirement
135  public AnnotatedType getAnnotatedType() {
136    return requireNonNull((AnnotatedType) annotatedType);
137  }
138
139  @Override
140  public boolean equals(@CheckForNull Object obj) {
141    if (obj instanceof Parameter) {
142      Parameter that = (Parameter) obj;
143      return position == that.position && declaration.equals(that.declaration);
144    }
145    return false;
146  }
147
148  @Override
149  public int hashCode() {
150    return position;
151  }
152
153  @Override
154  public String toString() {
155    return type + " arg" + position;
156  }
157}