1 package org.apache.commons.digester3.annotations.reflect;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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 * Class to supply the missing Java {@code AnnotatedElement} for method arguments.
29 *
30 * @since 2.1
31 */
32 public final class MethodArgument
33 implements AnnotatedElement
34 {
35
36 /**
37 * The method argument index.
38 */
39 private final int index;
40
41 /**
42 * The method argument type.
43 */
44 private final Class<?> parameterType;
45
46 /**
47 * The method argument annotations.
48 */
49 private final Annotation[] annotations;
50
51 /**
52 * Creates a new method argument as {@code AnnotatedElement}.
53 *
54 * @param index the method argument index.
55 * @param parameterType the method argument type.
56 * @param annotations the method argument annotations.
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 * Returns the method argument index.
78 *
79 * @return the method argument index.
80 */
81 public int getIndex()
82 {
83 return this.index;
84 }
85
86 /**
87 * Returns the method argument type.
88 *
89 * @return the method argument type.
90 */
91 public Class<?> getParameterType()
92 {
93 return this.parameterType;
94 }
95
96 /**
97 * {@inheritDoc}
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 * {@inheritDoc}
113 */
114 public Annotation[] getAnnotations()
115 {
116 return this.getAnnotationsArrayCopy();
117 }
118
119 /**
120 * {@inheritDoc}
121 */
122 public Annotation[] getDeclaredAnnotations()
123 {
124 return this.getAnnotationsArrayCopy();
125 }
126
127 /**
128 * Returns an annotations array, copy of the declared annotations in this method argument.
129 *
130 * @return an annotations array, copy of the declared annotations in this method argument.
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 * {@inheritDoc}
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 }