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