Refactor Java generator to make porting easier.

Refactor Java code generator by separating generator logic from
emitted code literals. This makes porting the code generator to other
languages easier.