-
Notifications
You must be signed in to change notification settings - Fork 777
Open
Description
Any non-override, non-private method in an anonymous class is at least "dodgy" and might fool a dead code detector.
Non-override methods are hard to reach from outside the class body itself. There are two kinds of anonymous class:
- In a "class instance creation expression with class body", a non-override method is only reachable one of two ways:
- calling it directly with the anonymous class expression itself as the receiver (
new Object() { void foo() {} }.foo()) - if the anonymous class expression is assigned to a local variable that uses
varinstead of an explicit type
- calling it directly with the anonymous class expression itself as the receiver (
- In an "enum constant with class body", a non-override method is not usable from outside that body at all.
If such a method is marked public, that might prevent a dead-code detector from recognizing it can be cleaned up. It would probably be a good practice to always make it private. Unfortunately, those two weird cases for non-enum anonymous classes should probably be exempted.
No idea if this is worth investigating further, just realized this as another strange wart in the language.
tbroyer, cushon, Stephan202 and jbduncan
Metadata
Metadata
Assignees
Labels
No labels