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.collect.FluentIterable; 020import com.google.common.collect.ImmutableList; 021import java.lang.annotation.Annotation; 022import java.lang.reflect.AnnotatedElement; 023import javax.annotation.CheckForNull; 024import org.checkerframework.checker.nullness.qual.Nullable; 025 026/** 027 * Represents a method or constructor parameter. 028 * 029 * @author Ben Yu 030 * @since 14.0 031 */ 032public final class Parameter implements AnnotatedElement { 033 034 private final Invokable<?, ?> declaration; 035 private final int position; 036 private final TypeToken<?> type; 037 private final ImmutableList<Annotation> annotations; 038 039 /** 040 * An {@code AnnotatedType} instance, or {@code null} under Android VMs (possible only when using 041 * the Android flavor of Guava). The field is declared with a type of {@code Object} to avoid 042 * compatibility problems on Android VMs. The corresponding accessor method, however, can have the 043 * more specific return type as long as users are careful to guard calls to it with version checks 044 * or reflection: Android VMs ignore the types of elements that aren't used. 045 */ 046 private final @Nullable Object annotatedType; 047 048 Parameter( 049 Invokable<?, ?> declaration, 050 int position, 051 TypeToken<?> type, 052 Annotation[] annotations, 053 @Nullable Object annotatedType) { 054 this.declaration = declaration; 055 this.position = position; 056 this.type = type; 057 this.annotations = ImmutableList.copyOf(annotations); 058 this.annotatedType = annotatedType; 059 } 060 061 /** Returns the type of the parameter. */ 062 public TypeToken<?> getType() { 063 return type; 064 } 065 066 /** Returns the {@link Invokable} that declares this parameter. */ 067 public Invokable<?, ?> getDeclaringInvokable() { 068 return declaration; 069 } 070 071 @Override 072 public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 073 return getAnnotation(annotationType) != null; 074 } 075 076 @Override 077 @CheckForNull 078 public <A extends Annotation> A getAnnotation(Class<A> annotationType) { 079 checkNotNull(annotationType); 080 for (Annotation annotation : annotations) { 081 if (annotationType.isInstance(annotation)) { 082 return annotationType.cast(annotation); 083 } 084 } 085 return null; 086 } 087 088 @Override 089 public Annotation[] getAnnotations() { 090 return getDeclaredAnnotations(); 091 } 092 093 /** 094 * @since 18.0 095 */ 096 @Override 097 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) { 098 return getDeclaredAnnotationsByType(annotationType); 099 } 100 101 /** @since 18.0 */ 102 @Override 103 public Annotation[] getDeclaredAnnotations() { 104 return annotations.toArray(new Annotation[0]); 105 } 106 107 /** 108 * @since 18.0 109 */ 110 @Override 111 @CheckForNull 112 public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) { 113 checkNotNull(annotationType); 114 return FluentIterable.from(annotations).filter(annotationType).first().orNull(); 115 } 116 117 /** 118 * @since 18.0 119 */ 120 @Override 121 public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) { 122 @Nullable 123 A[] result = FluentIterable.from(annotations).filter(annotationType).toArray(annotationType); 124 @SuppressWarnings("nullness") // safe because the input list contains no nulls 125 A[] cast = (A[]) result; 126 return cast; 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}