Skip to content

Instantly share code, notes, and snippets.

@maelvls
Created June 17, 2019 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maelvls/7280a83a196603e47c84f108695fa0f8 to your computer and use it in GitHub Desktop.
Save maelvls/7280a83a196603e47c84f108695fa0f8 to your computer and use it in GitHub Desktop.
Prettier but for Java

Prettier but for Java

Existing tools aren't really good in the Java world. Eclipse's XML-based styling rely on ...

In the following, I present examples formatted using the XML-configured Eclipse formatter. https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml

For example:

        userService.updateUser(userDTO.getFirstName(), userDTO.getLastName(), userDTO.getEmail(),
            userDTO.getLangKey(), userDTO.getImageUrl());
    userService.updateUser(
        userDTO.getFirstName(),
        userDTO.getLastName(),
        userDTO.getEmail(),
        userDTO.getLangKey(),
        userDTO.getImageUrl());
web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**").antMatchers("/app/**/*.{js,html}")
            .antMatchers("/i18n/**").antMatchers("/content/**").antMatchers("/h2-console/**")
            .antMatchers("/swagger-ui/index.html").antMatchers("/test/**");
web.ignoring()
        .antMatchers(HttpMethod.OPTIONS, "/**")
        .antMatchers("/app/**/*.{js,html}")
        .antMatchers("/i18n/**")
        .antMatchers("/content/**")
        .antMatchers("/h2-console/**")
        .antMatchers("/swagger-ui/index.html")
        .antMatchers("/test/**");

One case I could argue that the formatting is weird on long strings with many concatenations. This would be fixed with a string interpolation, which is something Java lacks right now.

return "User{" + "login='" + login + '\'' + ", firstName='" + firstName + '\''
        + ", lastName='" + lastName + '\'' + ", email='" + email + '\'' + ", imageUrl='"
        + imageUrl + '\'' + ", activated='" + activated + '\'' + ", langKey='" + langKey
        + '\'' + ", activationKey='" + activationKey + '\'' + "}";
return "User{"
    + "login='"
    + login
    + '\''
    + ", firstName='"
    + firstName
    + '\''
    + ", lastName='"
    + lastName
    + '\''
    + ", email='"
    + email
    + '\''
    + ", imageUrl='"
    + imageUrl
    + '\''
    + ", activated='"
    + activated
    + '\''
    + ", langKey='"
    + langKey
    + '\''
    + ", activationKey='"
    + activationKey
    + '\''
    + "}";
return Optional.ofNullable(securityContext.getAuthentication())
        .map(authentication -> authentication.getAuthorities().stream()
                .noneMatch(grantedAuthority -> grantedAuthority.getAuthority()
                        .equals(AuthoritiesConstants.ANONYMOUS)))
        .orElse(false);
return Optional.ofNullable(securityContext.getAuthentication())
    .map(
        authentication ->
            authentication
                .getAuthorities()
                .stream()
                .noneMatch(
                    grantedAuthority ->
                        grantedAuthority.getAuthority().equals(AuthoritiesConstants.ANONYMOUS)))
    .orElse(false);
return Optional.ofNullable(securityContext.getAuthentication())
    .map(
        authentication ->
            authentication.getAuthorities().stream()
                .noneMatch(
                    grantedAuthority ->
                        grantedAuthority.getAuthority().equals(AuthoritiesConstants.ANONYMOUS)))
    .orElse(false);

How to integrate google-java-format with a hook?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment