netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

Mark occurrences of enum constructors

Open nmatt opened this issue 1 month ago • 3 comments

Description

Unlike constructors of regular classes, the "mark occurrences" mechanism doesn't mark usages of enum constructors. Example:

class Example
{
    @SuppressWarnings("unused")
    static class Class
    {
        static final Class A = new Class();
        static final Class B = new Class();
        static final Class C = new Class();
        
        Class() { }
    }
    
    enum Enum
    {    
        A(),
        B(),
        C();
    
        Enum() { }
    }
}

Place the cursor on the Class() constructor => The three new Class() invocations are highlighted and shown in the right-hand editor sidebar. Place the cursor on the Enum() constructor => The three invocations from the enum constants are not highlighted.

Use case/motivation

In large enum classes, where the enum constants have implementations that may span many lines, an/or where the constants may have extensive Javadoc comments, highlighting the constants (which effectively are constructor invocations) would be useful.

Related issues

#8985

Are you willing to submit a pull request?

No

nmatt avatar Nov 05 '25 17:11 nmatt

While I see the use-case, this is not comparable. For the class (I renamed it to Demo) the type usage is highlighted, not the instances:

Image

For the enum, there is not type in front of its instances.

matthiasblaesing avatar Nov 05 '25 20:11 matthiasblaesing

@matthiasblaesing: This is not about the class name, but about the constructor invocation. This may be more obvious when the Enum constructor has arguments. The enum constants serve a double purpose: they are constants, but they are also constructor invocations.

What I mean is the equivalent of

Image

for enum classes. In other words, when the cursor is on the enum constructor, I would be expecting this:

Image

Note that this would be consistent with Call Hierarchy and Find Usages on the enum constructor:

Image


Image

(The latter displays a weird subtree for some reason.)

nmatt avatar Nov 06 '25 00:11 nmatt

enums need many special cases since they work differently to most other java code. Constructor invocation name != class name etc. This looks like a nice-to-have to me but is probably low priority.

mbien avatar Nov 06 '25 14:11 mbien