Here is a Semantic lexer based indent region function for formatting code in Emacs. It should work for Java and javascript. Feel free to use it if you like the output or customize it to suit your needs.

Screenshots: https://lifeofpenguin.blogspot.com/2021/05/semantic-lexer-based-indent-region.html

Code: semantic-indent-region

 Features

Regards,
Anand