Skip to content

Commit

Permalink
Issue #5324 - Jetty XML <Get> should support nested elements.
Browse files Browse the repository at this point in the history
<Get> "name" attribute is not mandatory anymore.
Added handling of nested elements, including <Name>.
Added support for calling isXxx() methods.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Sep 29, 2020
1 parent 9c00826 commit 921121a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
48 changes: 35 additions & 13 deletions jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -798,20 +798,33 @@ private void put(Object obj, XmlParser.Node node) throws Exception
*/
private Object get(Object obj, XmlParser.Node node) throws Exception
{
Class<?> oClass = nodeClass(node);
if (oClass != null)
AttrOrElementNode aoeNode = new AttrOrElementNode(obj, node, "Id", "Name", "Class");
String id = aoeNode.getString("Id");
String name = aoeNode.getString("Name");
String clazz = aoeNode.getString("Class");

Class<?> oClass;
if (clazz != null)
{
// static call
oClass = Loader.loadClass(clazz);
obj = null;
else
}
else if (obj != null)
{
oClass = obj.getClass();
}
else
{
throw new IllegalArgumentException(node.toString());
}

String name = node.getAttribute("name");
String id = node.getAttribute("id");
if (LOG.isDebugEnabled())
LOG.debug("XML get {}", name);

try
{
// Handle getClass explicitly
// Handle getClass() explicitly.
if ("class".equalsIgnoreCase(name))
{
obj = oClass;
Expand All @@ -824,20 +837,27 @@ private Object get(Object obj, XmlParser.Node node) throws Exception
}
if (id != null)
_configuration.getIdMap().put(id, obj);
configure(obj, node, 0);
configure(obj, node, aoeNode.getNext());
}
catch (NoSuchMethodException nsme)
catch (NoSuchMethodException nsme1)
{
try
{
// Try calling a isXxx() method.
Method method = oClass.getMethod("is" + name.substring(0, 1).toUpperCase(Locale.ENGLISH) + name.substring(1));
obj = invokeMethod(method, obj);
if (id != null)
_configuration.getIdMap().put(id, obj);
configure(obj, node, aoeNode.getNext());
}
catch (NoSuchMethodException nsme2)
{
// Try the field.
Field field = oClass.getField(name);
obj = getField(field, obj);
configure(obj, node, 0);
}
catch (NoSuchFieldException nsfe)
{
throw nsme;
if (id != null)
_configuration.getIdMap().put(id, obj);
configure(obj, node, aoeNode.getNext());
}
}
return obj;
Expand Down Expand Up @@ -873,7 +893,9 @@ else if (obj != null)
oClass = obj.getClass();
}
else
{
throw new IllegalArgumentException(node.toString());
}

if (LOG.isDebugEnabled())
LOG.debug("XML call {}", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ which act on the object returned by the get call.
A Get with a class attribute is treated as a static get method or field.
-->
<!ELEMENT Get (Id?,Class?,(%CONFIG;)*) >
<!ATTLIST Get %ID_ATTR; %CLASS_ATTR; %NAME_ATTR_REQUIRED; >
<!ELEMENT Get (Id?,Name?,Class?,(%CONFIG;)*) >
<!ATTLIST Get %ID_ATTR; %NAME_ATTR; %CLASS_ATTR; >


<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@
</Call>
</Call>

<Get name="String">
<Call name="toString"/>
<Get>
<Name>String</Name>
<Call>
<Name>toString</Name>
</Call>
</Get>

<Call>
Expand Down

0 comments on commit 921121a

Please sign in to comment.