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