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