apache / netbeans

Apache NetBeans
https://netbeans.apache.org/
Apache License 2.0
2.67k stars 855 forks source link

NetBeans doesn't recognize annotation on function return type with source formatting #7535

Open PavelTurk opened 4 months ago

PavelTurk commented 4 months ago

Apache NetBeans version

Apache NetBeans 22

What happened

NB formats code in a wrong way when annotation is used for function return type.

Language / Project Type / NetBeans Component

Java Maven Project using Java Modules (JPMS)

How to reproduce

Create NewClass:

import org.jetbrains.annotations.NotNull;

public class NewClass {

  public @NotNull String doItNow() {
    return null;
  }
}

And press ALT + SHIFT + F to format the code. You will get this result:

import org.jetbrains.annotations.NotNull;

public class NewClass {

    public @NotNull
    String doItNow() {
        return null;
    }
}

The same happens for class fields:

private final @Nullable List<String> foos;

Did this work correctly in an earlier version?

No / Don't know

Operating System

Ubuntu 20.04.3 LTS

JDK

Java: 21.0.2; OpenJDK 64-Bit Server VM 21.0.2+13-58

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

No response

Are you willing to submit a pull request?

No

neilcsmith-net commented 4 months ago

Try Editor / Formatting / Language: Java / Category: Wrapping and change the value of Annotations from Always to Never. Does this work for you? Annotations is one area I find the automatic formatting settings to be a problem by default, partly because they're not contextual.

PavelTurk commented 4 months ago

@neilcsmith-net I tried you suggestion. This is source code:

import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NewClass {

  private @Nullable List<String> foos;

  public @NotNull String doItNow() {
    return null;
  }
}

And this is the result code:

import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NewClass {

    private @Nullable List<String> foos;

    public @NotNull
    String doItNow() {
        return null;
    }
}

As you see it worked for field, but not for method

neilcsmith-net commented 4 months ago

Yes, can reproduce. Seems to be OK for method and method parameter annotations, but not on the return type. That definitely seems to be a bug. Then there's an open question about what the default should be.