Skip to content

Fixes #4550 XmlConfiguration named parameters#4553

Merged
sbordet merged 5 commits intojetty-9.4.xfrom
jetty-9.4.x-4550-XmlConfiguration-named-args
Feb 20, 2020
Merged

Fixes #4550 XmlConfiguration named parameters#4553
sbordet merged 5 commits intojetty-9.4.xfrom
jetty-9.4.x-4550-XmlConfiguration-named-args

Conversation

@gregw
Copy link
Copy Markdown
Contributor

@gregw gregw commented Feb 6, 2020

Fixed #4550 named parameters with a moderate refactor.
The named parameter matching was duplicated, only considering number of args and not applied to call arguments. This refactor puts all the behaviour in common methods and reorders the arguments to match parameters.

Signed-off-by: Greg Wilkins gregw@webtide.com

Fixed #4550 named parameters with a moderate refactor.
The named parameter matching was duplicated, only considering number of args and not applied to call arguments.    This refactor puts all the behaviour in common methods and reorders the arguments to match parameters.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
@gregw gregw requested review from lorban and sbordet February 6, 2020 13:13
@gregw
Copy link
Copy Markdown
Contributor Author

gregw commented Feb 6, 2020

@lorban @sbordet this turned out to be a bigger change than I thought it would be. Needs careful review if it is to go into next release.

Do not match a missing varArgs executable is there is a matching method with no varArgs.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
Objects.requireNonNull(namedArgMap, "Named Argument Map cannot be null");
Objects.requireNonNull(args, "Named list cannot be null");

constructors:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused label?

if (executable instanceof Constructor)
_class.getConstructor(types);
else
_class.getMethod(((Method)executable).getName(), types);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary cast.

// Does a non varargs method exist?
Class<?>[] types = Arrays.copyOf(executable.getParameterTypes(), count - 1);
if (executable instanceof Constructor)
_class.getConstructor(types);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch does not appear to be covered by tests, needs a varargs constructor.

_class.getConstructor(types);
else
_class.getMethod(((Method)executable).getName(), types);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the logic of this try block is that if a varargs member is probed before a non-varargs member, we want to skip the varargs one to match the non-varargs one.

I think it's way better to sort the members putting varargs methods last, and among varargs those with less parameters last.
In this way, we are guaranteed that precise match always comes before varargs match and we don't need this block. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have implemented it... feels a little fragile that applyTo depends on it's calling order, but OK

}
catch (NoSuchMethodException e)
{
// There is not a no varArgs alternative so let's try a null varArgs match
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is slightly confusing, since it's not a null varArgs match, it's an empty varArgs match. Indeed the method asEmptyVarArgs() is named correctly and does correctly return an empty array.

{
final Class<?> _class;
final List<Object> _arguments;
final List<String> _names;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make fields private.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure... but you do know that package scope fields of a private inner class are already really private :)

}
}

private NamedArgs(List<Object> arguments, List<String> names)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all other constructors/methods are package private, either make them all private, or all package private.


// No match of wrong number of parameters
if (count != _arguments.size())
return null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not cover the case of executable(String first, String... others) with args=["one", "two", "three"], where count==2 but _arguments.size()==3.
Not sure it was covered before, but while we are at it...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can support that, at least not easily. To call executable(String first, String... others) with args=["one", "two", "three"] you will need to do:

   <Call name=executable>
     <Arg>one</Arg>
     <Arg>
       <Array type=String>
         <Item>two</Item>
         <Item>three</Item>
       </Array>
     </Arg>
   </Call>


String attr = _node.getAttribute(attrName);
if (attr != null)
private class NamedArgs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to just Args since it does both named and non-named arguments?

Signed-off-by: Greg Wilkins <gregw@webtide.com>
…pty var args.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
@gregw gregw requested a review from sbordet February 19, 2020 18:38
Copy link
Copy Markdown
Contributor

@sbordet sbordet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the 2 nits, LGTM.

Signed-off-by: Greg Wilkins <gregw@webtide.com>
@sbordet sbordet merged commit ea5b70b into jetty-9.4.x Feb 20, 2020
@sbordet sbordet deleted the jetty-9.4.x-4550-XmlConfiguration-named-args branch February 20, 2020 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlConfiguration constructor selection based on number of arguments

2 participants