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.CheckForNull; 025import org.checkerframework.checker.nullness.qual.Nullable; 026 027/** 028 * Represents a method or constructor parameter. 029 * 030 * <p><b>Note:</b> Since Java 8 introduced {@link java.lang.reflect.Parameter} to represent method 031 * and constructor parameters, this class is no longer necessary. We intend to deprecate it in a 032 * future version. 033 * 034 * @author Ben Yu 035 * @since 14.0 036 */ 037@Beta 038@ElementTypesAreNonnullByDefault 039public final class Parameter implements AnnotatedElement { 040 041 private final Invokable<?, ?> declaration; 042 private final int position; 043 private final TypeToken<?> type; 044 private final ImmutableList<Annotation> annotations; 045 046 Parameter( 047 Invokable<?, ?> declaration, int position, TypeToken<?> type, 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 065 public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 066 return getAnnotation(annotationType) != null; 067 } 068 069 @Override 070 @CheckForNull 071 public <A extends Annotation> A getAnnotation(Class<A> annotationType) { 072 checkNotNull(annotationType); 073 for (Annotation annotation : annotations) { 074 if (annotationType.isInstance(annotation)) { 075 return annotationType.cast(annotation); 076 } 077 } 078 return null; 079 } 080 081 @Override 082 public Annotation[] getAnnotations() { 083 return getDeclaredAnnotations(); 084 } 085 086 /** @since 18.0 */ 087 // @Override on JDK8 088 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) { 089 return getDeclaredAnnotationsByType(annotationType); 090 } 091 092 /** @since 18.0 */ 093 // @Override on JDK8 094 @Override 095 public Annotation[] getDeclaredAnnotations() { 096 return annotations.toArray(new Annotation[0]); 097 } 098 099 /** @since 18.0 */ 100 // @Override on JDK8 101 @CheckForNull 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 /** @since 18.0 */ 108 // @Override on JDK8 109 public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationType) { 110 @Nullable 111 A[] result = FluentIterable.from(annotations).filter(annotationType).toArray(annotationType); 112 @SuppressWarnings("nullness") // safe because the input list contains no nulls 113 A[] cast = (A[]) result; 114 return cast; 115 } 116 117 @Override 118 public boolean equals(@CheckForNull Object obj) { 119 if (obj instanceof Parameter) { 120 Parameter that = (Parameter) obj; 121 return position == that.position && declaration.equals(that.declaration); 122 } 123 return false; 124 } 125 126 @Override 127 public int hashCode() { 128 return position; 129 } 130 131 @Override 132 public String toString() { 133 return type + " arg" + position; 134 } 135}