Issue #3713 - Emit warning when invoking deprecated method in Jetty XML.

Added warnings for deprecated field get/set.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2019-06-04 10:24:13 +02:00
parent 4058abd7c7
commit 2688ed55e5
3 changed files with 38 additions and 12 deletions

View File

@ -577,7 +577,7 @@ public class XmlConfiguration
Field field = oClass.getField(attr);
if (Modifier.isPublic(field.getModifiers()))
{
field.set(obj, value);
setField(field, obj, value);
return;
}
}
@ -677,7 +677,7 @@ public class XmlConfiguration
{
Object result = constructor.newInstance(args);
if (constructor.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated {} in {}", constructor, _url);
LOG.warn("Deprecated constructor {} in {}", constructor, _url);
return result;
}
@ -685,10 +685,25 @@ public class XmlConfiguration
{
Object result = method.invoke(obj, args);
if (method.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated {} in {}", method, _url);
LOG.warn("Deprecated method {} in {}", method, _url);
return result;
}
private Object getField(Field field, Object object) throws IllegalAccessException
{
Object result = field.get(object);
if (field.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated field {} in {}", field, _url);
return result;
}
private void setField(Field field, Object obj, Object arg) throws IllegalAccessException
{
field.set(obj, arg);
if (field.getAnnotation(Deprecated.class) != null)
LOG.warn("Deprecated field {} in {}", field, _url);
}
/**
* @param array the array to convert
* @param collectionType the desired collection type
@ -787,7 +802,7 @@ public class XmlConfiguration
{
// Try the field.
Field field = oClass.getField(name);
obj = field.get(obj);
obj = getField(field, obj);
configure(obj, node, 0);
}
catch (NoSuchFieldException nsfe)

View File

@ -27,6 +27,9 @@ public class AnnotatedTestConfiguration
private String third;
private String deprecated;
private AnnotatedTestConfiguration nested;
// Do not remove deprecation, used in tests.
@Deprecated
public String obsolete;
// Do not remove deprecation, used in tests.
@Deprecated

View File

@ -993,10 +993,14 @@ public class XmlConfigurationTest
@Test
public void testDeprecated() throws Exception
{
Class<?> testClass = AnnotatedTestConfiguration.class;
XmlConfiguration xmlConfiguration = new XmlConfiguration("" +
"<Configure class=\"org.eclipse.jetty.xml.AnnotatedTestConfiguration\">" +
"<Configure class=\"" + testClass.getName() + "\">" +
" <Set name=\"deprecated\">foo</Set>" +
" <Call name=\"setDeprecated\"><Arg><Get name=\"deprecated\" /></Arg></Call>" +
" <Set name=\"obsolete\">" +
" <Call name=\"setDeprecated\"><Arg><Get name=\"deprecated\" /></Arg></Call>" +
" </Set>" +
" <Get name=\"obsolete\" />" +
"</Configure>");
ByteArrayOutputStream logBytes = null;
@ -1012,14 +1016,18 @@ public class XmlConfigurationTest
if (logBytes != null)
{
List<String> warnings = Arrays.stream(logBytes.toString("UTF-8").split(System.lineSeparator()))
.filter(line -> line.contains(":WARN:") && line.contains("AnnotatedTestConfiguration"))
String[] lines = logBytes.toString("UTF-8").split(System.lineSeparator());
List<String> warnings = Arrays.stream(lines)
.filter(line -> line.contains(":WARN:"))
.filter(line -> line.contains(testClass.getSimpleName()))
.collect(Collectors.toList());
// 1. Deprecated constructor
// 2. Deprecated <Set>
// 3. Deprecated <Get>
// 4. Deprecated <Call>
assertEquals(4, warnings.size());
// 2. Deprecated <Set> method
// 3. Deprecated <Get> method
// 4. Deprecated <Call> method
// 5. Deprecated <Set> field
// 6. Deprecated <Get> field
assertEquals(6, warnings.size());
}
}
}