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.util;
8
9 import java.lang.reflect.Constructor;
10 import java.lang.reflect.Executable;
11 import java.lang.reflect.Method;
12 import java.util.Collections;
13 import java.util.List;
14
15 import javax.validation.ParameterNameProvider;
16
17 /**
18  * Allows to obtain parameter names from {@link Executable}s in a uniform fashion. Delegates to the configured
19  * {@link ParameterNameProvider}.
20  *
21  * @author Gunnar Morling
22  */

23 public class ExecutableParameterNameProvider {
24
25     private final ParameterNameProvider delegate;
26
27     public ExecutableParameterNameProvider(ParameterNameProvider delegate) {
28         this.delegate = delegate;
29     }
30
31     public List<String> getParameterNames(Executable executable) {
32         //skip parameterless methods
33         if ( executable.getParameterCount() == 0 ) {
34             return Collections.emptyList();
35         }
36         if ( executable instanceof Method ) {
37             return delegate.getParameterNames( (Method) executable );
38         }
39         else {
40             return delegate.getParameterNames( (Constructor<?>) executable );
41         }
42     }
43
44     public ParameterNameProvider getDelegate() {
45         return delegate;
46     }
47
48     @Override
49     public String toString() {
50         return "ExecutableParameterNameProvider [delegate=" + delegate + "]";
51     }
52
53     @Override
54     public int hashCode() {
55         final int prime = 31;
56         int result = 1;
57         result = prime * result + ( ( delegate == null ) ? 0 : delegate.hashCode() );
58         return result;
59     }
60
61     /**
62      * Equality is based on identity of the delegate.
63      */

64     @Override
65     public boolean equals(Object obj) {
66         if ( this == obj ) {
67             return true;
68         }
69         if ( obj == null ) {
70             return false;
71         }
72         if ( getClass() != obj.getClass() ) {
73             return false;
74         }
75         ExecutableParameterNameProvider other = (ExecutableParameterNameProvider) obj;
76
77         return delegate == other;
78     }
79 }
80