Tomcat scans the web application for class files (both under WEB-INF/classes and in JARs). The class files are then passed to a heavily edited, package renamed local copy of the Apache Commons Byte Code Engineering Library (BCEL). Tomcat's version of BCEL is optimised to process only those parts of the byte code Tomcat is interested in (annotations, super class if any, implemented interfaces) and to skip over the rest as fast as possible. BCEL reads the class files directly from disk.
Tomcat does some careful caching of the results from BCEL so that even for the most complex of class hierarchies each class is only ever processed once and no post-processing is required to get the full list if annotations (including those inherited from super classes) for any class.
The annotation scan also checks for matches to the @HandlesTypes annotation for SCIs.
Scanning every class for annotations is expensive however you do it (and one of the reasons I'm not a fan of this particular feature). Tomcat's implementation went through several iterations before arriving at the efficient implementation it now uses.
Processor
interface. – Luiggi Mendoza@WebServlet
for servlets or@ManagedBean
from JSF). – Luiggi Mendoza