Bug 87998 - [1.5][compiler] Enum constants generate warnings about synthetic constructor access
Summary: [1.5][compiler] Enum constants generate warnings about synthetic constructor ...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: All All
: P3 minor (vote)
Target Milestone: 3.1 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-03-14 19:33 EST by Trevor Robinson CLA
Modified: 2005-03-31 10:36 EST (History)
0 users

See Also:


Attachments
Apply on ProblemReporter in HEAD (1.16 KB, patch)
2005-03-14 19:51 EST, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Trevor Robinson CLA 2005-03-14 19:33:49 EST
Enum constants with class bodies generate a synthetic constructor access warning
when using the default constructor. In the following class, the opening brace of
each enum constant class body is flagged with this warning: "Access to enclosing
constructor Direction() is emulated by a synthetic accessor method. Increasing
its visibility will improve your performance"

Perhaps the warning is accurate, but it seems like it should be suppressed in
this case, since the constructor is implicit. Also, any performance impact is
negligible since enum constants are singletons.

public enum Direction
{
    INPUT
    {
        public Direction getReverse()
        {
            return OUTPUT;
        }
    },
    OUTPUT
    {
        public Direction getReverse()
        {
            return INPUT;
        }
    },
    INOUT
    {
        public Direction getReverse()
        {
            return INOUT;
        }
    };

    public abstract Direction getReverse();
}
Comment 1 Olivier Thomann CLA 2005-03-14 19:51:31 EST
Created attachment 18781 [details]
Apply on ProblemReporter in HEAD

I propose the following patch. We simply need not to report the warning, but we
still need the synthetic method.
Comment 2 Philipe Mulet CLA 2005-03-15 06:28:24 EST
I am rather questionning whether the default constructor should be public to
match the type. 

If the emulation is real, then the warning is sound.
Comment 3 Trevor Robinson CLA 2005-03-15 12:23:04 EST
Perhaps my argument is with the JLS. From the JLS 3 draft:

"If the enum type has no constructor declarations, a parameterless default
constructor is provided (which matches the implicit empty argument list). This
default constructor is private."

In no other case is use of an implicit constructor cause for a performance
warning. It seems like the compiler should promote the implicit constructor to
default access if any enum constants have bodies, analogous to this JLS rule:

"An enum type is implicitly final unless it contains at least one enum constant
that has a class body."

I don't know whether you feel you have the discretion to do this promotion
(since javac probably doesn't), or feel it wise to (optionally?) suppress the
warning, but I just wanted to make you aware of it, since it seems odd to get a
warning for something implicit.
Comment 4 Philipe Mulet CLA 2005-03-15 13:04:40 EST
Oh, I see what you mean now. Need to think about it. It is indeed true that the
current behavior is mandated by the spec, and maybe a situation where we need to
customize our warning support.
Comment 5 Philipe Mulet CLA 2005-03-21 04:34:53 EST
The warning intent is to signal a performance issue user may not realize.
However it suggests to change the visibility, which isn't an option here due to
enum spec.
Loosing the warning altogether in this situation is a bit annoying, as the
emulation would occur silently (which is against the purpose of this diagnostic).
Comment 6 Philipe Mulet CLA 2005-03-21 04:45:44 EST
Actually, I just realized that when adding a constructor, with default access
modifiers, then the warning goes away. We incorrectly forget to consider the
constructor has private by default.
Comment 7 Philipe Mulet CLA 2005-03-21 04:58:29 EST
Fixed private modifier by default, and silenced offending warning.
Added EnumTest#test083-084.

Fixed
Comment 8 David Audel CLA 2005-03-31 10:36:56 EST
Verified in I20050330-0500