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