1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.properties.javabean;
8
9 import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
10
11 import java.lang.annotation.Annotation;
12 import java.lang.invoke.MethodHandles;
13 import java.lang.reflect.AnnotatedType;
14 import java.lang.reflect.Parameter;
15 import java.lang.reflect.Type;
16 import java.lang.reflect.TypeVariable;
17
18 import org.hibernate.validator.internal.util.logging.Log;
19 import org.hibernate.validator.internal.util.logging.LoggerFactory;
20
21 /**
22  * @author Guillaume Smet
23  */

24 public class JavaBeanParameter implements JavaBeanAnnotatedElement {
25
26     private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
27
28     private static final Annotation[] EMPTY_PARAMETER_ANNOTATIONS = new Annotation[0];
29
30     private final int index;
31
32     private final Parameter parameter;
33
34     private final Class<?> type;
35
36     private final Type genericType;
37
38     JavaBeanParameter(int index, Parameter parameter, Class<?> type, Type genericType) {
39         this.index = index;
40         this.parameter = parameter;
41         this.type = type;
42         this.genericType = genericType;
43     }
44
45     public int getIndex() {
46         return index;
47     }
48
49     @Override
50     public Class<?> getType() {
51         return type;
52     }
53
54     @Override
55     public AnnotatedType getAnnotatedType() {
56         return parameter.getAnnotatedType();
57     }
58
59     @Override
60     public Annotation[] getDeclaredAnnotations() {
61         try {
62             return parameter.getDeclaredAnnotations();
63         }
64         catch (ArrayIndexOutOfBoundsException ex) {
65             // This looks like a JVM bug we are trying to work around, kept as is for now
66             LOG.warn( MESSAGES.constraintOnConstructorOfNonStaticInnerClass(), ex );
67             return EMPTY_PARAMETER_ANNOTATIONS;
68         }
69     }
70
71     @Override
72     public Type getGenericType() {
73         return genericType;
74     }
75
76     @Override
77     public TypeVariable<?>[] getTypeParameters() {
78         return parameter.getType().getTypeParameters();
79     }
80
81     @Override
82     public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
83         return parameter.getAnnotation( annotationClass );
84     }
85 }
86