JDK-8279291 : Remove restrictions from @SafeVarargs
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 17
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2021-12-25
  • Updated: 2021-12-27
  • Resolved: 2021-12-27
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Currently there exist restrictions on which methods @SafeVarargs can be used, namely final, static or private methods. This makes usage of varargs generic parameters for public non-static interface methods extremely annoying:
- The compiler issues a warning for the method declaration itself
- The compiler issues a warning for every usage of the method

Therefore it would be good to remove these @SafeVarargs restrictions and allow placing it on any varargs method. To provide additional safety, it could be enforced that every method overriding a method annotated with @SafeVarargs has to be annotated with @SafeVarargs itself as well (this is possible because the retention of @SafeVarags is RUNTIME).

The annotation already acts as a "I know what I am doing" indicator, but even with the current implementation it can be used incorrectly (Javadoc of SafeVarargs has an example for this). Therefore there is (most likely) no big difference in allowing its usage on all methods.



Comments
As noted in JDK-7196160, "None of these executables [static methods, final method, and constructors] can be overriden, which is a constraint since annotation are only inherited along the superclass chain at a type level and *not* at the method/constructor level. Since annotations are not inherited at the method level, an overriding method could knowingly or unknowingly violate the @SafeVararags constraint of the overridden method. " In other words, since annotations are *not* inherited for methods, only methods that cannot be overridden can use @SafeVarargs. Closing this bug as not an issue.
27-12-2021

RFE to update restrictions on @SafeVarargs annotation
27-12-2021