[ 
https://issues.apache.org/jira/browse/LANG-1597?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17234309#comment-17234309
 ] 

Richard Eckart de Castilho commented on LANG-1597:
--------------------------------------------------

As a quick measure, I can only provide the code where I wanted to rely on the 
method but had to work around the bug:

{code}
            boolean isFeatureStructureValue = FeatureStructure.class
                    .isAssignableFrom(aValue.getClass());
            boolean isFeatureStructureVarArgValue = aValue.getClass().isArray()
                    && FeatureStructure.class
                            
.isAssignableFrom(aValue.getClass().getComponentType());

            // Workaround for https://issues.apache.org/jira/browse/LANG-1597
            if (isFeatureStructureValue || isFeatureStructureVarArgValue) {
                // MethodUtils.invokeStaticMethod has a "bug" that vararg 
methods are only
                // properly found/matched if the parameter value type or the 
supertype of the
                // parameter value matches the varargs type. So here we have 
FeatureStructure...
                // as the varargs definition, but we may want to set a value of 
type Annotation
                // and from Annotation to FeatureStructure is a long way 
through the inheritance
                // hierarchy.
                // Note: this bug exists at least up to commons-lang3 3.11 - it 
might be fixed
                // in later versions.
                MethodUtils.invokeStaticMethod(FSUtil.class, "setFeature",
                        new Object[] { aFS, aName, aValue },
                        new Class[] { FeatureStructure.class, String.class,
                                FeatureStructure[].class });
                return;
            }
{code}

> Invocation fails because matching varargs method found but then discarded
> -------------------------------------------------------------------------
>
>                 Key: LANG-1597
>                 URL: https://issues.apache.org/jira/browse/LANG-1597
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.reflect.*
>    Affects Versions: 3.11
>            Reporter: Richard Eckart de Castilho
>            Priority: Major
>
> {{getMatchingAccessibleMethod(Class<?>, String, Class<?>...)}} is able to 
> find proper match for a method to call but then discards it.
> Consider the situation you have a varargs method like {{Baz.foo(X... X)}}. We 
> call {{getMatchingAccessibleMethod(Baz.class, "foo", z}} where {{z instance 
> of Z extends Y extends X}}. {{getMatchingAccessibleMethod}} finds {{foo}} as 
> the candidate, but then discards it because for varargs, it requires either 
> the argument is directly is of type X or that the supertype of the argument 
> X. It does not properly check the entire inheritance hierarchy in this code 
> (3.11):
> {code}
>         if (bestMatch != null && bestMatch.isVarArgs() && 
> bestMatch.getParameterTypes().length > 0 && parameterTypes.length > 0) {
>             final Class<?>[] methodParameterTypes = 
> bestMatch.getParameterTypes();
>             final Class<?> methodParameterComponentType = 
> methodParameterTypes[methodParameterTypes.length - 1].getComponentType();
>             final String methodParameterComponentTypeName = 
> ClassUtils.primitiveToWrapper(methodParameterComponentType).getName();
>             final Class<?> lastParameterType = 
> parameterTypes[parameterTypes.length - 1];
>             final String parameterTypeName = (lastParameterType==null) ? null 
> : lastParameterType.getName();
>             final String parameterTypeSuperClassName = 
> (lastParameterType==null) ? null : 
> lastParameterType.getSuperclass().getName();
>             if (parameterTypeName!= null && parameterTypeSuperClassName != 
> null && !methodParameterComponentTypeName.equals(parameterTypeName)
>                     && 
> !methodParameterComponentTypeName.equals(parameterTypeSuperClassName)) {
>                 return null;
>             }
>         }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to