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 org.jspecify.annotations.Nullable;
026
027/**
028 * Represents a method or constructor parameter.
029 *
030 * @author Ben Yu
031 * @since 14.0
032 */
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  public <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationType) {
079    checkNotNull(annotationType);
080    for (Annotation annotation : annotations) {
081      if (annotationType.isInstance(annotation)) {
082        return annotationType.cast(annotation);
083      }
084    }
085    return null;
086  }
087
088  @Override
089  public Annotation[] getAnnotations() {
090    return getDeclaredAnnotations();
091  }
092
093  /**
094   * @since 18.0
095   */
096  @Override
097  public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
098    return getDeclaredAnnotationsByType(annotationType);
099  }
100
101  /** @since 18.0 */
102  @Override
103  public Annotation[] getDeclaredAnnotations() {
104    return annotations.toArray(new Annotation[0]);
105  }
106
107  /**
108   * @since 18.0
109   */
110  @Override
111  public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationType) {
112    checkNotNull(annotationType);
113    return FluentIterable.from(annotations).filter(annotationType).first().orNull();
114  }
115
116  /**
117   * @since 18.0
118   */
119  @Override
120  public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) {
121    @Nullable
122    A[] result = FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
123    @SuppressWarnings("nullness") // safe because the input list contains no nulls
124    A[] cast = (A[]) result;
125    return cast;
126  }
127
128  /**
129   * Returns the {@link AnnotatedType} of the parameter.
130   *
131   * @since 25.1 for guava-jre
132   */
133  @SuppressWarnings("Java7ApiChecker")
134  public AnnotatedType getAnnotatedType() {
135    return requireNonNull((AnnotatedType) annotatedType);
136  }
137
138  @Override
139  public boolean equals(@Nullable Object obj) {
140    if (obj instanceof Parameter) {
141      Parameter that = (Parameter) obj;
142      return position == that.position && declaration.equals(that.declaration);
143    }
144    return false;
145  }
146
147  @Override
148  public int hashCode() {
149    return position;
150  }
151
152  @Override
153  public String toString() {
154    return type + " arg" + position;
155  }
156}