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  /**
102   * @since 18.0
103   */
104  @Override
105  public Annotation[] getDeclaredAnnotations() {
106    return annotations.toArray(new Annotation[0]);
107  }
108
109  /**
110   * @since 18.0
111   */
112  @Override
113  public <A extends Annotation> @Nullable 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 A[] result =
124        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  /**
131   * Returns the {@link AnnotatedType} of the parameter.
132   *
133   * @since 25.1 for guava-jre
134   */
135  @SuppressWarnings("Java7ApiChecker")
136  public AnnotatedType getAnnotatedType() {
137    return requireNonNull((AnnotatedType) annotatedType);
138  }
139
140  @Override
141  public boolean equals(@Nullable Object obj) {
142    if (obj instanceof Parameter) {
143      Parameter that = (Parameter) obj;
144      return position == that.position && declaration.equals(that.declaration);
145    }
146    return false;
147  }
148
149  @Override
150  public int hashCode() {
151    return position;
152  }
153
154  @Override
155  public String toString() {
156    return type + " arg" + position;
157  }
158}