Skip to content

The very 1st import in a new class without package declaration is added above the copyright header #9661

Open

Description

@dca00

Apache NetBeans version

Apache NetBeans 31

What happened

The very 1st and subsequent imports are being added above the copyright header, when acting upon the Alt-Enter drop-down.
If however at least 1 import exists beneath the copyright header, then the subsequent imports trail it.

Language / Project Type / NetBeans Component

Java

How to reproduce

Create a new Java class. Add some code that requires imports. Alt-Enter the lines and add imports.

Did this work correctly in an earlier version?

No / Don't know

Operating System

Linux

JDK

21

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

No response

Are you willing to submit a pull request?

No

Activity

  1. added
    kind:bugBug report or fix
    needs:triageRequires attention from one of the committers
    on Oct 4, 2026
  2. dca00 commented on Oct 4, 2026

    @dca00
    Author

    I just dragged a class from the default package to another, and the refactor added the package also above the copyright header.

  3. added
    Java[ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)
    on Oct 6, 2026
  4. mbien commented on Oct 6, 2026

    @mbien
    Member

    probably in default packages when the package declaration is missing. The import would land above the comment since there is nothing to anchor on. If the first import is manually adjusted its position will be used as reference.

    reproducer.

    /*
     * C
     */
    
    /**
     * D
     */
    class OuterFoo {
        JFrame
    }

    going to rename the title to be more precise.

  5. changed the title [-]The very 1st import in a new class is added above the copyright header[/-] [+]The very 1st import in a new class without package declaration is added above the copyright header[/+] on Oct 6, 2026
  6. added
    Contribution welcomeAn issue or feature not currently being worked on, but a contribution would be welcomed!
    on Oct 6, 2026
  7. ashwintemkar commented on Oct 8, 2026

    @ashwintemkar
    Contributor

    Interesting issue. I would like to open a draft PR for this, it looks simple at first glance.

  8. ashwintemkar commented on Oct 8, 2026

    @ashwintemkar
    Contributor

    Meanwhile I dug into this a bit and found the actual cause.

    There is a method in the comment handling code that deliberately skips attaching the copyright header to the file when there is no package declaration. That is why the header ends up attached to the class instead, and a new import gets placed above it rather than below.

    This code is old, it goes back to the original donation to Apache in 2017, and it is shared by a lot of Java source generation features, not just imports. So I want to be careful rather than changing it directly.

    I am thinking of fixing this on the import insertion side instead, making sure a new import always lands after any leading header comment, rather than touching the shared comment logic itself.

    Does that sound okay to move forward with?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Contribution welcomeAn issue or feature not currently being worked on, but a contribution would be welcomed!Java[ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)kind:bugBug report or fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions