@@ -153,11 +153,11 @@ else if(methods.size() == 1)
153
153
if (m == null )
154
154
throw new IllegalArgumentException (noMethodReport (methodName ,target ,args ));
155
155
156
- if (!Modifier .isPublic (m .getDeclaringClass ().getModifiers ()))
156
+ if (!Modifier .isPublic (m .getDeclaringClass ().getModifiers ()) || ! canAccess ( m , target ) )
157
157
{
158
158
//public method of non-public class, try to find it in hierarchy
159
159
Method oldm = m ;
160
- m = getAsMethodOfPublicBase (target .getClass (), m );
160
+ m = getAsMethodOfAccessibleBase (target .getClass (), m , target );
161
161
if (m == null )
162
162
throw new IllegalArgumentException ("Can't call public method of non-public class: " +
163
163
oldm .toString ());
@@ -173,6 +173,7 @@ else if(methods.size() == 1)
173
173
174
174
}
175
175
176
+ // DEPRECATED - replaced by getAsMethodOfAccessibleBase()
176
177
public static Method getAsMethodOfPublicBase (Class c , Method m ){
177
178
for (Class iface : c .getInterfaces ())
178
179
{
@@ -197,6 +198,7 @@ public static Method getAsMethodOfPublicBase(Class c, Method m){
197
198
return getAsMethodOfPublicBase (sc , m );
198
199
}
199
200
201
+ // DEPRECATED - replaced by isAccessibleMatch()
200
202
public static boolean isMatch (Method lhs , Method rhs ) {
201
203
if (!lhs .getName ().equals (rhs .getName ())
202
204
|| !Modifier .isPublic (lhs .getDeclaringClass ().getModifiers ()))
@@ -221,6 +223,55 @@ public static boolean isMatch(Method lhs, Method rhs) {
221
223
return match ;
222
224
}
223
225
226
+ public static Method getAsMethodOfAccessibleBase (Class c , Method m , Object target ){
227
+ for (Class iface : c .getInterfaces ())
228
+ {
229
+ for (Method im : iface .getMethods ())
230
+ {
231
+ if (isAccessibleMatch (im , m , target ))
232
+ {
233
+ return im ;
234
+ }
235
+ }
236
+ }
237
+ Class sc = c .getSuperclass ();
238
+ if (sc == null )
239
+ return null ;
240
+ for (Method scm : sc .getMethods ())
241
+ {
242
+ if (isAccessibleMatch (scm , m , target ))
243
+ {
244
+ return scm ;
245
+ }
246
+ }
247
+ return getAsMethodOfAccessibleBase (sc , m , target );
248
+ }
249
+
250
+ public static boolean isAccessibleMatch (Method lhs , Method rhs , Object target ) {
251
+ if (!lhs .getName ().equals (rhs .getName ())
252
+ || !Modifier .isPublic (lhs .getDeclaringClass ().getModifiers ())
253
+ || !canAccess (lhs , target ))
254
+ {
255
+ return false ;
256
+ }
257
+
258
+ Class [] types1 = lhs .getParameterTypes ();
259
+ Class [] types2 = rhs .getParameterTypes ();
260
+ if (types1 .length != types2 .length )
261
+ return false ;
262
+
263
+ boolean match = true ;
264
+ for (int i =0 ; i <types1 .length ; ++i )
265
+ {
266
+ if (!types1 [i ].isAssignableFrom (types2 [i ]))
267
+ {
268
+ match = false ;
269
+ break ;
270
+ }
271
+ }
272
+ return match ;
273
+ }
274
+
224
275
public static Object invokeConstructor (Class c , Object [] args ) {
225
276
try
226
277
{
0 commit comments