java - How can I check if a method is static using reflection? -
i want discover @ run-time static methods of class, how can this? or, how differentiate between static , non-static methods.
use modifier.isstatic(method.getmodifiers())
.
/** * returns public static methods of class or interface, * including declared in super classes , interfaces. */ public static list<method> getstaticmethods(class<?> clazz) { list<method> methods = new arraylist<method>(); (method method : clazz.getmethods()) { if (modifier.isstatic(method.getmodifiers())) { methods.add(method); } } return collections.unmodifiablelist(methods); }
note: method dangerous security standpoint. class.getmethods "bypass[es] securitymanager checks depending on immediate caller's class loader" (see section 6 of java secure coding guidelines).
disclaimer: not tested or compiler.
note modifier
should used care. flags represented ints not type safe. common mistake test modifier flag on type of reflection object not apply to. may case flag in same position set denote other information.
Comments
Post a Comment