From 8e9a576e0d81f9638ad7b59fc4d41d07fe3e1fbd Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Fri, 11 Jun 2021 15:28:56 +1000 Subject: [PATCH] Fixed #6375 XmlConfiguration always checks for setter method Fix #6375 by making XmlConfiguration Set handling always check for a matching setter, even if the property attribute is given but not set. Signed-off-by: Greg Wilkins --- .../eclipse/jetty/xml/XmlConfiguration.java | 21 ++++++++++----- .../jetty/xml/XmlConfigurationTest.java | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java index 17f5ae4d2617..b92a08e012dc 100644 --- a/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java +++ b/jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java @@ -516,9 +516,17 @@ public void configure(Object obj, XmlParser.Node cfg, int i) throws Exception private void set(Object obj, XmlParser.Node node) throws Exception { String attr = node.getAttribute("name"); + String name = "set" + attr.substring(0, 1).toUpperCase(Locale.ENGLISH) + attr.substring(1); String id = node.getAttribute("id"); String property = node.getAttribute("property"); String propertyValue = null; + + Class oClass = nodeClass(node); + if (oClass != null) + obj = null; + else + oClass = obj.getClass(); + // Look for a property value if (property != null) { @@ -526,21 +534,20 @@ private void set(Object obj, XmlParser.Node node) throws Exception propertyValue = properties.get(property); // If no property value, then do not set if (propertyValue == null) + { + // check that there is at least one setter that could have matched + if (Arrays.stream(oClass.getMethods()).noneMatch(m -> m.getName().equals(name))) + throw new NoSuchMethodException(String.format("No '%s' on %s", name, oClass.getName())); + // otherwise it is a noop return; + } } - String name = "set" + attr.substring(0, 1).toUpperCase(Locale.ENGLISH) + attr.substring(1); Object value = value(obj, node); if (value == null) value = propertyValue; Object[] arg = {value}; - Class oClass = nodeClass(node); - if (oClass != null) - obj = null; - else - oClass = obj.getClass(); - Class[] vClass = {Object.class}; if (value != null) vClass[0] = value.getClass(); diff --git a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java index 81d00277499c..6d528959d66f 100644 --- a/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java +++ b/jetty-xml/src/test/java/org/eclipse/jetty/xml/XmlConfigurationTest.java @@ -363,6 +363,32 @@ public void testSetWithNullPropertyAndValue() throws Exception assertNull(configuration.getIdMap().get("test")); } + @Test + public void testSetWithWrongNameAndProperty() throws Exception + { + XmlConfiguration configuration = asXmlConfiguration(""); + configuration.getProperties().put("prop", "This is a property value"); + TestConfiguration tc = new TestConfiguration(); + tc.setTestString("default"); + + NoSuchMethodException e = assertThrows(NoSuchMethodException.class, () -> configuration.configure(tc)); + assertThat(e.getMessage(), containsString("setWrongName")); + assertEquals("default", tc.getTestString()); + } + @Test + + public void testSetWithWrongNameAndNullProperty() throws Exception + { + XmlConfiguration configuration = asXmlConfiguration(""); + configuration.getProperties().remove("prop"); + TestConfiguration tc = new TestConfiguration(); + tc.setTestString("default"); + + NoSuchMethodException e = assertThrows(NoSuchMethodException.class, () -> configuration.configure(tc)); + assertThat(e.getMessage(), containsString("setWrongName")); + assertEquals("default", tc.getTestString()); + } + @Test public void testMeaningfullSetException() throws Exception {