Do not rely on Modifier.toString in this way. In the past, modifiers for classes, fields, and methods received unique values, so you could interpret them without looking at the type of object that has a modifier, as this method suggests.
But while Java evolved, more modifier bits were added, and it was not possible to save this property. In particular, when passing the modifier bits of the Modifier.toString method unmodified, you will get the following amazing behavior:
- the bridge method will be printed as
volatile - varargs method will be printed as
transient
Therefore, you must filter out the bit. Java 7 introduced a method that provides the correct mask so you can use Modifier.toString(m.getModifiers()&Modifier.methodModifiers()) .
But this only works because the old Java keywords are mapped to unique modifier bits, and the new modifier bits are not associated with keywords. With newer versions of Java, even this may not be enough.
For the default keyword, this is even simpler: there is no modifier bit associated with the keyword. If the public , non abstract , non static method appears in the interface , it must be the default method. How Method.isDefault() determines whether a method is a default method. Modifier.toString(…) has no way of knowing if the declaration class is interface and therefore will never print default .
source share