[
https://issues.apache.org/jira/browse/LANG-1195?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Derek C. Ashmore updated LANG-1195:
-----------------------------------
Description:
Currently, MethodUtils is restricted to finding and invoking accessible
methods. Frequently, developers have a need to test 'private' methods. What I
see is that they escalate access to 'protected' in order to more easily provide
test coverage for these methods. From a design perspective, this is bad.
I propose to enhance MethodUtils so that it can easily invoke private methods.
I'm not suggesting that developers should do this in production code, merely
test code. Much as FieldUtils provides access to private fields via the
'forceAccess' overload on many of its methods. I've copied a utility like this
around for years. It would be much more convenient to simply include it with
Commons Lang. I propose adding the following overloads to existing methods on
MethodUtils.
public static Object invokeMethod(final Object object, final boolean
forceAccess, final String methodName)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException
public static Object invokeMethod(final Object object, final boolean
forceAccess, final String methodName,
Object... args) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException
public static Object invokeMethod(final Object object, final boolean
forceAccess, final String methodName,
Object[] args, Class<?>[] parameterTypes)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException
Furthermore, I propose the following new method for finding private methods
patterned after getAccessibleMatchingMethod:
public static Method getMatchingMethod(final Class<?> cls, final String
methodName,
final Class<?>... parameterTypes)
I have, in fact, forked your github repository and coded this enhancement as
well as unit test coverage for this. I'll add a comment to this ticket once I
get a branch in my fork created and the enhancement checked in. Every effort
has been made to adhere to your coding conventions. Thanks for looking at this.
was:
Currently, MethodUtils is restricted to finding and invoking accessible
methods. Frequently, developers have a need to test 'private' methods. What I
see is that they escalate access to 'protected' in order to more easily provide
test coverage for these methods. From a design perspective, this is bad.
I propose to enhance MethodUtils so that it can easily invoke private methods.
I'm not suggesting that developers should do this in production code, merely
test code. Much as FieldUtils provides access to private fields via the
'forceAccess' overload on many of its methods. I've copied a utility like this
around for years. It would be much more convenient to simply include it with
Commons Lang. I propose adding the following overloads to existing methods on
MethodUtils.
public static Object invokeMethod(final Object object, final boolean
forceAccess, final String methodName)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException
public static Object invokeMethod(final Object object, final boolean
forceAccess, final String methodName,
Object... args) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException
public static Object invokeMethod(final Object object, final boolean
forceAccess, final String methodName,
Object[] args, Class<?>[] parameterTypes)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException
Furthermore, I propose the following new method for finding private methods
patterned after getAccessibleMethod:
public static Method getMatchingMethod(final Class<?> cls, final String
methodName,
final Class<?>... parameterTypes)
I have, in fact, forked your github repository and coded this enhancement as
well as unit test coverage for this. I'll add a comment to this ticket once I
get a branch in my fork created and the enhancement checked in. Every effort
has been made to adhere to your coding conventions. Thanks for looking at this.
> Enhance MethodUtils to allow invocation of private methods
> -----------------------------------------------------------
>
> Key: LANG-1195
> URL: https://issues.apache.org/jira/browse/LANG-1195
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.reflect.*
> Environment: not environment specific
> Reporter: Derek C. Ashmore
> Priority: Minor
> Fix For: 3.5
>
>
> Currently, MethodUtils is restricted to finding and invoking accessible
> methods. Frequently, developers have a need to test 'private' methods. What
> I see is that they escalate access to 'protected' in order to more easily
> provide test coverage for these methods. From a design perspective, this is
> bad.
> I propose to enhance MethodUtils so that it can easily invoke private
> methods. I'm not suggesting that developers should do this in production
> code, merely test code. Much as FieldUtils provides access to private fields
> via the 'forceAccess' overload on many of its methods. I've copied a utility
> like this around for years. It would be much more convenient to simply
> include it with Commons Lang. I propose adding the following overloads to
> existing methods on MethodUtils.
> public static Object invokeMethod(final Object object, final boolean
> forceAccess, final String methodName)
> throws NoSuchMethodException, IllegalAccessException,
> InvocationTargetException
>
> public static Object invokeMethod(final Object object, final boolean
> forceAccess, final String methodName,
> Object... args) throws NoSuchMethodException,
> IllegalAccessException, InvocationTargetException
>
> public static Object invokeMethod(final Object object, final boolean
> forceAccess, final String methodName,
> Object[] args, Class<?>[] parameterTypes)
> throws NoSuchMethodException, IllegalAccessException,
> InvocationTargetException
>
> Furthermore, I propose the following new method for finding private methods
> patterned after getAccessibleMatchingMethod:
> public static Method getMatchingMethod(final Class<?> cls, final String
> methodName,
> final Class<?>... parameterTypes)
> I have, in fact, forked your github repository and coded this enhancement as
> well as unit test coverage for this. I'll add a comment to this ticket once
> I get a branch in my fork created and the enhancement checked in. Every
> effort has been made to adhere to your coding conventions. Thanks for
> looking at this.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)