Skip to content

Try with resources with "var" formatted with extra space before "var" #5546

@rturner-edjuster

Description

@rturner-edjuster

Apache NetBeans version

Apache NetBeans 16

What happened

This is almost the same issue as #3720.

When Java code is automatically formatting a try-with-resources statement, if the resource is specified with the type var instead of a specific type, an additional space is injected before the var keyword.

How to reproduce

With a clean install of NetBeans 16 on MacOS 13.1, with the default Java code formatting settings (no changes to defaults after install, and no importing of previous version settings), and using a "new file", and no existing project:

  1. Create a new Java file:
  2. Replace all code with the following:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class NewClass {

    public void test() throws IOException {
        try (var br = Files.newBufferedReader(Paths.get("test"))) {

        }
    }
}
  1. Select "Format" from the "Source" menu and the code will be reformatted as:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class NewClass {

    public void test() throws IOException {
        try ( var br = Files.newBufferedReader(Paths.get("test"))) {

        }
    }
}

Note the additional space in try ( var.

If you replace var with BufferedReader, the problem does not occur.

Did this work correctly in an earlier version?

Apache NetBeans 12.6 or earlier

Operating System

Mac OS X version 13.1 running on aarch64; UTF-8; en_CA (nb)

JDK

18.0.2; OpenJDK 64-Bit Server VM 18.0.2+9-FR

Apache NetBeans packaging

Apache NetBeans provided installer

Anything else

No response

Are you willing to submit a pull request?

No

Code of Conduct

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Java[ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)kind:bugBug report or fixpending-responseAwaiting further information to be supplied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions