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