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