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 java.lang.reflect.AnnotatedType; 025import javax.annotation.CheckForNull; 026import org.checkerframework.checker.nullness.qual.Nullable; 027 028/** 029 * Represents a method or constructor parameter. 030 * 031 * <p><b>Note:</b> Since Java 8 introduced {@link java.lang.reflect.Parameter} to represent method 032 * and constructor parameters, this class is no longer necessary. We intend to deprecate it in a 033 * future version. 034 * 035 * @author Ben Yu 036 * @since 14.0 037 */ 038@Beta 039@ElementTypesAreNonnullByDefault 040public final class Parameter implements AnnotatedElement { 041 042 private final Invokable<?, ?> declaration; 043 private final int position; 044 private final TypeToken<?> type; 045 private final ImmutableList<Annotation> annotations; 046 private final AnnotatedType annotatedType; 047 048 Parameter( 049 Invokable<?, ?> declaration, 050 int position, 051 TypeToken<?> type, 052 Annotation[] annotations, 053 AnnotatedType 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 /** @since 18.0 */ 094 // @Override on JDK8 095 @Override 096 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) { 097 return getDeclaredAnnotationsByType(annotationType); 098 } 099 100 /** @since 18.0 */ 101 // @Override on JDK8 102 @Override 103 public Annotation[] getDeclaredAnnotations() { 104 return annotations.toArray(new Annotation[0]); 105 } 106 107 /** @since 18.0 */ 108 // @Override on JDK8 109 @Override 110 @CheckForNull 111 public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) { 112 checkNotNull(annotationType); 113 return FluentIterable.from(annotations).filter(annotationType).first().orNull(); 114 } 115 116 /** @since 18.0 */ 117 // @Override on JDK8 118 @Override 119 public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) { 120 @Nullable 121 A[] result = FluentIterable.from(annotations).filter(annotationType).toArray(annotationType); 122 @SuppressWarnings("nullness") // safe because the input list contains no nulls 123 A[] cast = (A[]) result; 124 return cast; 125 } 126 127 /** @since 25.1 */ 128 // @Override on JDK8 129 public AnnotatedType getAnnotatedType() { 130 return annotatedType; 131 } 132 133 @Override 134 public boolean equals(@CheckForNull Object obj) { 135 if (obj instanceof Parameter) { 136 Parameter that = (Parameter) obj; 137 return position == that.position && declaration.equals(that.declaration); 138 } 139 return false; 140 } 141 142 @Override 143 public int hashCode() { 144 return position; 145 } 146 147 @Override 148 public String toString() { 149 return type + " arg" + position; 150 } 151}