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  public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
089    return getDeclaredAnnotationsByType(annotationType);
090  }
091
092  /** @since 18.0 */
093  // @Override on JDK8
094  @Override
095  public Annotation[] getDeclaredAnnotations() {
096    return annotations.toArray(new Annotation[annotations.size()]);
097  }
098
099  /** @since 18.0 */
100  // @Override on JDK8
101  public <A extends Annotation> @Nullable A getDeclaredAnnotation(Class<A> annotationType) {
102    checkNotNull(annotationType);
103    return FluentIterable.from(annotations).filter(annotationType).first().orNull();
104  }
105
106  /** @since 18.0 */
107  // @Override on JDK8
108  public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) {
109    return FluentIterable.from(annotations).filter(annotationType).toArray(annotationType);
110  }
111
112  /** @since 25.1 */
113  // @Override on JDK8
114  public AnnotatedType getAnnotatedType() {
115    return annotatedType;
116  }
117
118  @Override
119  public boolean equals(@Nullable Object obj) {
120    if (obj instanceof Parameter) {
121      Parameter that = (Parameter) obj;
122      return position == that.position && declaration.equals(that.declaration);
123    }
124    return false;
125  }
126
127  @Override
128  public int hashCode() {
129    return position;
130  }
131
132  @Override
133  public String toString() {
134    return type + " arg" + position;
135  }
136}