1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.reflect;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.annotations.Beta;
22 import com.google.common.collect.FluentIterable;
23 import com.google.common.collect.ImmutableList;
24
25 import java.lang.annotation.Annotation;
26 import java.lang.reflect.AnnotatedElement;
27
28 import javax.annotation.Nullable;
29
30
31
32
33
34
35
36 @Beta
37 public final class Parameter implements AnnotatedElement {
38
39 private final Invokable<?, ?> declaration;
40 private final int position;
41 private final TypeToken<?> type;
42 private final ImmutableList<Annotation> annotations;
43
44 Parameter(
45 Invokable<?, ?> declaration,
46 int position,
47 TypeToken<?> type,
48 Annotation[] annotations) {
49 this.declaration = declaration;
50 this.position = position;
51 this.type = type;
52 this.annotations = ImmutableList.copyOf(annotations);
53 }
54
55
56 public TypeToken<?> getType() {
57 return type;
58 }
59
60
61 public Invokable<?, ?> getDeclaringInvokable() {
62 return declaration;
63 }
64
65 @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
66 return getAnnotation(annotationType) != null;
67 }
68
69 @Override
70 @Nullable
71 public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
72 checkNotNull(annotationType);
73 for (Annotation annotation : annotations) {
74 if (annotationType.isInstance(annotation)) {
75 return annotationType.cast(annotation);
76 }
77 }
78 return null;
79 }
80
81 @Override public Annotation[] getAnnotations() {
82 return getDeclaredAnnotations();
83 }
84
85
86
87
88
89 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
90 return getDeclaredAnnotationsByType(annotationType);
91 }
92
93
94
95
96
97 @Override public Annotation[] getDeclaredAnnotations() {
98 return annotations.toArray(new Annotation[annotations.size()]);
99 }
100
101
102
103
104
105 @Nullable
106 public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationType) {
107 checkNotNull(annotationType);
108 return FluentIterable.from(annotations)
109 .filter(annotationType)
110 .first()
111 .orNull();
112 }
113
114
115
116
117
118 public <A extends Annotation> A[]
119 getDeclaredAnnotationsByType(Class<A> annotationType) {
120 return FluentIterable.from(annotations)
121 .filter(annotationType)
122 .toArray(annotationType);
123 }
124
125 @Override public boolean equals(@Nullable Object obj) {
126 if (obj instanceof Parameter) {
127 Parameter that = (Parameter) obj;
128 return position == that.position && declaration.equals(that.declaration);
129 }
130 return false;
131 }
132
133 @Override public int hashCode() {
134 return position;
135 }
136
137 @Override public String toString() {
138 return type + " arg" + position;
139 }
140 }