hmmm, I don't know how they implement it, but I know IntelliJ IDEA has a code highlighting feature that does just what you're asking:
IntelliJ IDEA's On-the-Fly Code Analysis
Highlighting unused imports, throws declarations and other unused symbols
IDEA will point out all redundant import statements, throws declarations and other unused symbols (like variables and methods), and will highlight them in the editor with the corresponding links on the right gutter area for easier navigation. More information on other code analysis features can be found here.