001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.reflect;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.Beta;
022import com.google.common.collect.ImmutableList;
023
024import java.lang.annotation.Annotation;
025import java.lang.reflect.AnnotatedElement;
026
027import javax.annotation.Nullable;
028
029/**
030 * Represents a method or constructor parameter.
031 *
032 * @author Ben Yu
033 * @since 14.0
034 */
035@Beta
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
043  Parameter(
044      Invokable<?, ?> declaration,
045      int position,
046      TypeToken<?> type,
047      Annotation[] annotations) {
048    this.declaration = declaration;
049    this.position = position;
050    this.type = type;
051    this.annotations = ImmutableList.copyOf(annotations);
052  }
053
054  /** Returns the type of the parameter. */
055  public TypeToken<?> getType() {
056    return type;
057  }
058
059  /** Returns the {@link Invokable} that declares this parameter. */
060  public Invokable<?, ?> getDeclaringInvokable() {
061    return declaration;
062  }
063
064  @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
065    return getAnnotation(annotationType) != null;
066  }
067
068  @Override
069  @Nullable
070  public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
071    checkNotNull(annotationType);
072    for (Annotation annotation : annotations) {
073      if (annotationType.isInstance(annotation)) {
074        return annotationType.cast(annotation);
075      }
076    }
077    return null;
078  }
079
080  @Override public Annotation[] getAnnotations() {
081    return getDeclaredAnnotations();
082  }
083
084  @Override public Annotation[] getDeclaredAnnotations() {
085    return annotations.toArray(new Annotation[annotations.size()]);
086  }
087
088  @Override public boolean equals(@Nullable Object obj) {
089    if (obj instanceof Parameter) {
090      Parameter that = (Parameter) obj;
091      return position == that.position && declaration.equals(that.declaration);
092    }
093    return false;
094  }
095
096  @Override public int hashCode() {
097    return position;
098  }
099
100  @Override public String toString() {
101    return type + " arg" + position;
102  }
103}