I would recommend you look into parser generators, eg. JavaCC. JavaCC lets you describe the grammar in an BNF-like style and creates Java classes according to it.
There are also already grammars available for JavaCC to parse Java code, I think even as an example or tutorial that comes with JavaCC.