Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enforce a standard style for Java code? [closed]

Tags:

java

gradle

I'm trying to introduce a tool to standardise the format of our java code. The requirements are:

  • Required format should be the Oracle standard (or something very very similar)
  • Multiline statements should be respected, i.e. if the user adds linebreaks to split a statement over multiple lines in the interest of readability, these should not be removed (see above)
  • Gradle integration. If code is submitted that does not conform to the rules, the build should fail.
  • Autofixing - reformatting code to conform to the rules via a (Gradle) command
  • IDE integration - reformatting code to conform to the rules within IntelliJ, VSCode, etc. EditorConfig might be useful for this.

I've tried using Spotless, but the way it formats multiline statements is very unsatisfactory. For example, the following code

var token = Jwts.builder()
        .signWith(Keys.hmacShaKeyFor(signingKey), SignatureAlgorithm.HS512)
        .setHeaderParam("typ", SecurityConstants.TOKEN_TYPE)
        .setIssuer(SecurityConstants.TOKEN_ISSUER)
        .claim("rol", roles)
        .setAudience(SecurityConstants.TOKEN_AUDIENCE)
        .setSubject(user.getUsername())
        .setExpiration(Timestamp.valueOf(tomorrow))
        .addClaims(user.getClaims())
        .compact();

Will fail the Spotless checks and if ./gradlew spotlessApply is run, it will be reformatted to this hideous mess

var token = Jwts.builder().signWith(Keys.hmacShaKeyFor(signingKey), SignatureAlgorithm.HS512)
        .setHeaderParam("typ", SecurityConstants.TOKEN_TYPE).setIssuer(SecurityConstants.TOKEN_ISSUER)
        .claim("rol", roles).setAudience(SecurityConstants.TOKEN_AUDIENCE).setSubject(user.getUsername())
        .setExpiration(Timestamp.valueOf(tomorrow)).addClaims(user.getClaims()).compact();

So effectively, Spotless is behaving almost like an obfuscator. Is there a tool available that can satisfy most/all of the above requirements?

like image 516
Antonio Dragos Avatar asked Oct 22 '25 20:10

Antonio Dragos


1 Answers

I've tried using Spotless, but the way it formats multiline statements is very unsatisfactory.

Spotless allows you specify custom rules to fulfill your requirements: https://github.com/diffplug/spotless/tree/main/plugin-gradle#custom

Checkstyle is another a great tool to enforce formatting. It is used by the Spring Framework team. It also has an excellent plugin for IntelliJ.

So suggestion is either:

  1. Try writing custom rules for Spotless to fulfill your needs.
  2. If (1) is not satisfactory, explore using Checkstyle with custom checks.

If code is submitted that does not conform to the rules, the build should fail.

There are a couple ways you can handle this:

Whichever format plugin you use, there is a associated check task. So for Spotless that would be spotlessCheck and Checkstyle is just check.

You can configure the build task to have a task dependency on those tasks.

Another way is to have a check in your pipeline that invokes which check task you configure.

like image 107
Francisco Mateo Avatar answered Oct 24 '25 08:10

Francisco Mateo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!