This question is not looking for a solution to the problem, but explains why the problem arose (or not!).
Javadocs for Class.getMethods()say:
The elements of the returned array are not sorted and are not in a specific order.
The fact is, we used a neat little Java template library called JMTE , perhaps a couple of years later, with no problem. This uses a JSTL-like syntax to inject model values into templates. In particular, we used something similar to render the price:
${vendor.currency.symbol} ${order.amount}
The first of them is translated as follows:
vendor.getCurrency().getSymbol()
where getCurrency()returns the java.util.Currency object . A currency has two methods for getting a currency symbol - one that accepts a particular language and one that uses the default value.
public String getSymbol()
public String getSymbol(Locale locale)
Over the past 18 months or so, everything has been working fine, with currency codes / symbols appearing in emails. Then, 5 days ago, we started getting random IllegalArgumentExceptionabandoned while trying to replace${vendor.currency.symbol}
After a little debugging, I found the reason, inside JMTE:
for (Method method : declaredMethods) {
if (Modifier.isPublic(method.getModifiers())
&& (method.getName().equals("get" + suffix) ||
method.getName().equals("is" + suffix))) {
value = method.invoke(o, (Object[]) null);
....
}
}
ie Whether it is called getSymbol()or getSymbol(Locale)wholly in the power of the return order Class.getMethods()(not in any particular order). I added a test to ensure that the method has 0 parameters and my problem has been resolved.
, , - , .
, 18 - , .
, 50% , ( , ), (), 18 10 ^ 5 . (, , , ).
, , -, - Java, . , - , , - , , , - .
, : - , , Class.getMethods()?