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;
018
019import com.google.common.annotations.Beta;
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.checkerframework.checker.nullness.qual.Nullable;
026
027/**
028 * Represents a method or constructor parameter.
029 *
030 * @author Ben Yu
031 * @since 14.0
032 */
033@Beta
034public final class Parameter implements AnnotatedElement {
035
036  private final Invokable<?, ?> declaration;
037  private final int position;
038  private final TypeToken<?> type;
039  private final ImmutableList<Annotation> annotations;
040  private final AnnotatedType annotatedType;
041
042  Parameter(
043      Invokable<?, ?> declaration,
044      int position,
045      TypeToken<?> type,
046      Annotation[] annotations,
047      AnnotatedType annotatedType) {
048    this.declaration = declaration;
049    this.position = position;
050    this.type = type;
051    this.annotations = ImmutableList.copyOf(annotations);
052    this.annotatedType = annotatedType;
053  }
054
055  /** Returns the type of the parameter. */
056  public TypeToken<?> getType() {
057    return type;
058  }
059
060  /** Returns the {@link Invokable} that declares this parameter. */
061  public Invokable<?, ?> getDeclaringInvokable() {
062    return declaration;
063  }
064
065  @Override
066  public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
067    return getAnnotation(annotationType) != null;
068  }
069
070  @Override
071  public <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationType) {
072    checkNotNull(annotationType);
073    for (Annotation annotation : annotations) {
074      if (annotationType.isInstance(annotation)) {
075        return annotationType.cast(annotation);
076      }
077    }
078    return null;
079  }
080
081  @Override
082  public Annotation[] getAnnotations() {
083    return getDeclaredAnnotations();
084  }
085
086  /** @since 18.0 */
087  // @Override on JDK8
088  @Override
089  public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
090    return getDeclaredAnnotationsByType(annotationType);
091  }
092
093  /** @since 18.0 */
094  // @Override on JDK8
095  @Override
096  public Annotation[] getDeclaredAnnotations() {
097    return annotations.toArray(new Annotation[annotations.size()]);
098  }
099
100  /** @since 18.0 */
101  // @Override on JDK8
102  @Override
103  public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationType) {
104    checkNotNull(annotationType);
105    return FluentIterable.from(annotations).filter(annotationType).first().orNull();
106  }
107
108  /** @since 18.0 */
109  // @Override on JDK8
110  @Override
111  public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) {
112    return FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
113  }
114
115  /** @since 25.1 */
116  // @Override on JDK8
117  public AnnotatedType getAnnotatedType() {
118    return annotatedType;
119  }
120
121  @Override
122  public boolean equals(@Nullable Object obj) {
123    if (obj instanceof Parameter) {
124      Parameter that = (Parameter) obj;
125      return position == that.position && declaration.equals(that.declaration);
126    }
127    return false;
128  }
129
130  @Override
131  public int hashCode() {
132    return position;
133  }
134
135  @Override
136  public String toString() {
137    return type + " arg" + position;
138  }
139}