Fail on warning count > N

I already know I can use -Werror to fail on any warnings at all, but aiming to fix thousands of warnings in a single commit isn’t very realistic, so is there a way to detect whether the number of warnings has exceeded some threshold? This way, we could gradually decrease the number to 0 while stopping people adding new ones.

Failing that, is there a way to capture the output of javac so that we could parse the output ourselves?