1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.util;
21
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.lang.reflect.Method;
27 import java.util.Arrays;
28
29
30
31
32
33 public final class MethodUtils
34 {
35
36 private static final Logger LOG = LoggerFactory.getLogger( MethodUtils.class );
37
38
39
40
41
42 private MethodUtils()
43 {
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public static Method getAssignmentCompatibleMethod( Class<?> clazz,
61 String candidateMethodName,
62 Class<?>[] candidateParameterTypes
63 ) throws NoSuchMethodException
64 {
65 if ( LOG.isDebugEnabled() )
66 {
67 StringBuilder buf = new StringBuilder();
68 buf.append( "call to getAssignmentCompatibleMethod(): \n\tclazz = " );
69 buf.append( clazz.getName() );
70 buf.append( "\n\tcandidateMethodName = " );
71 buf.append( candidateMethodName );
72 buf.append( "\n\tcandidateParameterTypes = " );
73
74 for ( Class<?> argClass : candidateParameterTypes )
75 {
76 buf.append( "\n\t\t" );
77 buf.append( argClass.getName() );
78 }
79
80 LOG.debug( buf.toString() );
81 }
82
83 try
84 {
85
86 Method exactMethod = clazz.getMethod( candidateMethodName, candidateParameterTypes );
87
88 if ( exactMethod != null )
89 {
90 return exactMethod;
91 }
92 }
93 catch ( Exception e )
94 {
95 LOG.info( "Could not find accessible exact match for candidateMethod {}", candidateMethodName, e );
96 }
97
98
99
100
101
102
103 Method[] methods = clazz.getMethods();
104
105
106 for ( int mx = 0; mx < methods.length; mx++ )
107 {
108
109 if ( !candidateMethodName.equals( methods[mx].getName() ) )
110 {
111
112 continue;
113 }
114
115
116 Class<?>[] parameterTypes = methods[mx].getParameterTypes();
117
118
119 if ( parameterTypes.length != candidateParameterTypes.length )
120 {
121
122 continue;
123 }
124
125
126 for ( int px = 0; px < parameterTypes.length; px++ )
127 {
128
129 if ( !parameterTypes[px].isAssignableFrom( candidateParameterTypes[px] ) )
130 {
131
132 break;
133 }
134 }
135
136
137 return methods[mx];
138 }
139
140 throw new NoSuchMethodException( clazz.getName() + "." + candidateMethodName
141 + "(" + Arrays.toString( candidateParameterTypes ) + ")" );
142 }
143 }