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 {@code 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  /**
097   * @since 18.0
098   */
099  @Override
100  public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
101    return getDeclaredAnnotationsByType(annotationType);
102  }
103
104  /** @since 18.0 */
105  @Override
106  public Annotation[] getDeclaredAnnotations() {
107    return annotations.toArray(new Annotation[0]);
108  }
109
110  /**
111   * @since 18.0
112   */
113  @Override
114  @CheckForNull
115  public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) {
116    checkNotNull(annotationType);
117    return FluentIterable.from(annotations).filter(annotationType).first().orNull();
118  }
119
120  /**
121   * @since 18.0
122   */
123  @Override
124  public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) {
125    @Nullable
126    A[] result = FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
127    @SuppressWarnings("nullness") // safe because the input list contains no nulls
128    A[] cast = (A[]) result;
129    return cast;
130  }
131
132  /**
133   * Returns the {@link AnnotatedType} of the parameter.
134   *
135   * @since 25.1 for guava-jre
136   */
137  @SuppressWarnings("Java7ApiChecker")
138  public AnnotatedType getAnnotatedType() {
139    return requireNonNull((AnnotatedType) annotatedType);
140  }
141
142  @Override
143  public boolean equals(@CheckForNull Object obj) {
144    if (obj instanceof Parameter) {
145      Parameter that = (Parameter) obj;
146      return position == that.position && declaration.equals(that.declaration);
147    }
148    return false;
149  }
150
151  @Override
152  public int hashCode() {
153    return position;
154  }
155
156  @Override
157  public String toString() {
158    return type + " arg" + position;
159  }
160}