1 package org.apache.commons.digester3.annotations.reflect;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static java.lang.System.arraycopy;
23
24 import java.lang.annotation.Annotation;
25 import java.lang.reflect.AnnotatedElement;
26
27
28
29
30
31
32 public final class MethodArgument
33 implements AnnotatedElement
34 {
35
36
37
38
39 private final int index;
40
41
42
43
44 private final Class<?> parameterType;
45
46
47
48
49 private final Annotation[] annotations;
50
51
52
53
54
55
56
57
58 public MethodArgument( int index, Class<?> parameterType, Annotation[] annotations )
59 {
60 if ( parameterType == null )
61 {
62 throw new IllegalArgumentException( "Argument 'parameterType' must be not null" );
63 }
64 if ( annotations == null )
65 {
66 throw new IllegalArgumentException( "Argument 'annotations' must be not null" );
67 }
68
69 this.index = index;
70 this.parameterType = parameterType;
71 this.annotations = new Annotation[annotations.length];
72
73 arraycopy( annotations, 0, this.annotations, 0, annotations.length );
74 }
75
76
77
78
79
80
81 public int getIndex()
82 {
83 return this.index;
84 }
85
86
87
88
89
90
91 public Class<?> getParameterType()
92 {
93 return this.parameterType;
94 }
95
96
97
98
99 public <T extends Annotation> T getAnnotation( Class<T> annotationType )
100 {
101 for ( Annotation annotation : this.annotations )
102 {
103 if ( annotationType == annotation.annotationType() )
104 {
105 return annotationType.cast( annotation );
106 }
107 }
108 return null;
109 }
110
111
112
113
114 public Annotation[] getAnnotations()
115 {
116 return this.getAnnotationsArrayCopy();
117 }
118
119
120
121
122 public Annotation[] getDeclaredAnnotations()
123 {
124 return this.getAnnotationsArrayCopy();
125 }
126
127
128
129
130
131
132 private Annotation[] getAnnotationsArrayCopy()
133 {
134 Annotation[] annotations = new Annotation[this.annotations.length];
135 System.arraycopy( this.annotations, 0, annotations, 0, annotations.length );
136 return annotations;
137 }
138
139
140
141
142 public boolean isAnnotationPresent( Class<? extends Annotation> annotationType )
143 {
144 for ( Annotation annotation : this.annotations )
145 {
146 if ( annotationType == annotation.annotationType() )
147 {
148 return true;
149 }
150 }
151 return false;
152 }
153
154 }