Bug 267670 - Private enum constant incorrectly marked as never read locally
Summary: Private enum constant incorrectly marked as never read locally
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.5 M7   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-03-09 12:30 EDT by Matthew Hall CLA
Modified: 2009-04-27 09:38 EDT (History)
3 users (show)

See Also:


Attachments
Proposed patch & tests (4.50 KB, patch)
2009-03-25 07:36 EDT, Srikanth Sankaran CLA
no flags Details | Diff
Revised patch (4.50 KB, patch)
2009-03-30 02:20 EDT, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Matthew Hall CLA 2009-03-09 12:30:55 EDT
public class PrivateEnumNeverReadLocally {
  private static enum PrivateEnum {
    HELLO;
  }

  public static void main( String[] args ) {
    for ( PrivateEnum e : PrivateEnum.values() )
      System.out.println( e );
  }
}

The above class is flagged with the warning "The field PrivateEnumNeverReadLocally.PrivateEnum.HELLO is never read locally."  This is not correct since the enum is being accessed indirectly through the method PrivateEnum.values().
Comment 1 Srikanth Sankaran CLA 2009-03-25 07:36:06 EDT
Created attachment 129830 [details]
Proposed patch & tests


This patch inhibits the emission of unused warning for enumerator fields.
As the submitted test case points out, enumeration constants can be
accessed in non-obvious ways that makes it difficult to reliably track
their usage. In addition to the the synthesized values() method, 
valueOf(String) method and Enum.valueOf(Class<T>, String) method could
also be used to materialize enumerators indirectly.

Initially I attempted tracking the usage of these methods and flagging the
symbolic constants as being referenced, should these indirect means be seen
to employed - this is not again reliable as the valueOf() methods materialize
enumerators out of thin air (from Strings which could be variables which
the compiler has no means of peeking into at compile time).

Since this unused local reporting could be configured into an ERROR, this
has the potential to break builds, hence this patch simply inhibits the
warning/error for private enums.
Comment 2 Matthew Hall CLA 2009-03-25 10:28:07 EDT
I'd have to do a test, but I don't think that Enum.valueOf will work for a private enum.  So in theory the usage test could depend on invocations of PrivateEnum.valueOf or PrivateEnum.values() (if you want to go to that much trouble)
Comment 3 Srikanth Sankaran CLA 2009-03-26 06:39:58 EDT
(In reply to comment #2)
> I'd have to do a test, but I don't think that Enum.valueOf will work for a
> private enum.  So in theory the usage test could depend on invocations of
> PrivateEnum.valueOf or PrivateEnum.values() (if you want to go to that much
> trouble)

The attached patch does have a contrived test that tests the use of
Enum.valueOf(Class<T>, String).

PrivateEnum.valueOf method and the Enum.valueOf(Class<T>, String) are
no different really from in general using reflection to materialize
handles to methods and fields. So this problem can appear in non-enum
scenarios too (see bug #266132)

So, one plausible approach is to ignore the valueOf method pair and track
the usage of only values() method and flag all enumerator constants as
being used if values() method was invoked.

Kent, what is your opinion on this ? 

Do you think it is worthwhile tracking use of values() and flagging the
fields as being in use ? 

If so, is it worthwhile to dedicate a bit at the enum *type* level
e.g say:

    ExtraCompilerModifiers.AccIndirectlyUsed

so the whole enum type could be flagged at one stroke ? I am just
worried about iterating over the potentially numerous fields for
every usage of values().

Much ado ?



Comment 4 Kent Johnson CLA 2009-03-26 11:26:22 EDT
> Do you think it is worthwhile tracking use of values() and flagging the
> fields as being in use ?

No I don't.

I like the patch as is: Warn against the unused private enum type, but skip the constants.
Comment 5 Matthew Hall CLA 2009-03-26 11:30:16 EDT
Maybe mark it as unused if the enum class itself is never referenced?
Comment 6 Matthew Hall CLA 2009-03-26 11:32:39 EDT
(In reply to comment #4)
> I like the patch as is: Warn against the unused private enum type, but skip the
> constants.

(In reply to comment #5)
> Maybe mark it as unused if the enum class itself is never referenced?

Erm, I didn't read comment #4 too carefully before posting, I think we're on the same page.
Comment 7 Srikanth Sankaran CLA 2009-03-30 02:20:37 EDT
Created attachment 130205 [details]
Revised patch

Changed patch to make the "Enum.valueOf(Class<T>, String)"
test scenario more direct.
Comment 8 Srikanth Sankaran CLA 2009-03-30 02:24:52 EDT
Released in HEAD for 3.5M7
Comment 9 David Audel CLA 2009-04-27 09:38:28 EDT
Verified for 3.5M7 using I20090426-2000