I'm looking for a java library to format/beautify Java code snippets. So for I have been using Google Java Format however it seems to work just for fully-fledged Java classes and not for code snippets. For example, the following code snippet has been formatted online without an issue:
@Bean public ConfigurableServletWebServerFactory webServerFactory() {
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
factory.setPort(9000);
factory.setContextPath("/myapp");
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
return factory;
}
But using Google Formatter, the following error is returned:
error: class, interface, or enum expected
I've used the following code to attempt parsing it:
String formattedSource = new Formatter().formatSource(code);
Any idea how to fix it?
org.eclipse.jdt.core.formatter.CodeFormatter. This plugin uses it github.com/revelc/formatter-maven-plugin/blob/main/src/main/…formatSourcemethod shouldn't change it, so you can lift out the formatted method again after formatting.