Given a java source code file, what is a fast and reliable way to find out if it implements a given interface?
A reliable way would be to parse the file into a syntax tr
What would you do?
I would compile the source code of the class and the interface, load both using Class.forName()
, and use Class.isAssignableFrom
to test if the class is a subtype of the interface.
Relatively fast, totally reliable ... assuming that the source code is compilable.
Note that this can all be done at runtime, and if you use a throw-away instance of a properly implemented custom classloader to do the loading, you can avoid polluting your JVM with dubious classes.