Created
March 21, 2019 23:22
-
-
Save hlfbt/01fbb0efb6f621ef9b3e9025bb87e7e2 to your computer and use it in GitHub Desktop.
Get a human readable list of all modifiers on a given Method or Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This isn't supposed to be pretty or ever be used in production code (don't even think about it), but has proven useful for quick debugging when working with reflections. | |
*/ | |
// Example Method with some more interesting modifiers | |
Method meth = Arrays.stream(data.getClass().getDeclaredMethods()).filter(m -> "getChronology".equals(m.getName())).toArray()[0]; | |
// Should return "isSynthetic, isPublic, isVolatile" | |
String modifiers = Arrays.stream(Modifier.class.getDeclaredMethods()) | |
.map(m -> { | |
boolean isAccessible = m.isAccessible(); | |
try { | |
m.setAccessible(true); | |
if (m.getName().startsWith("is") && (Boolean) m.invoke(null, meth.getModifiers())) { | |
return m.getName(); | |
} else { | |
return null; | |
} | |
} catch (InvocationTargetException|IllegalAccessException e) {} | |
m.setAccessible(isAccessible); | |
return null; | |
}) | |
.filter(Objects::nonNull) | |
.collect(Collectors.joining(", ")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment