From dtrebbien at gmail.com Mon Jul 9 00:43:00 2018 From: dtrebbien at gmail.com (Daniel Trebbien) Date: Sun, 8 Jul 2018 20:43:00 -0400 Subject: Extending 15.28 to include toString() on an enum constant Message-ID: One small idea I have for enhancing the Java language is to allow toString() on enum constants to be used in constant expressions, as long as toString() is not overidden, or if it is, the implementation returns a constant expression. There are two use cases I have in mind for this: 1. When using the SLF4J logging library, if you want to embed the name of an enum constant in the log message, you should use parameterized logging to avoid incurring a wasted runtime string concatenation if logging at the specified level is disabled. See SLF4J's FAQ for "What is the fastest way of (not) logging?": https://www.slf4j.org/faq.html#logging_performance As an example of this in the wild, I submitted a pull request in which I converted a log statement involving an enum constant to parameterized logging: https://github.com/Netflix/eureka/commit/ad81a1140f351ec19165cb94d5aa668cfc08a932 There are other cases where an SLF4J logger call involving an enum constant correctly used parameterized logging, but I am not able to find an example right now. Although using parameterized logging solves the performance issue, it is desirable to keep down the number of parameters to an SLF4J logger call because the SLF4J Logger interface has more efficient variants of the trace(), debug(), info(), warn(), and error() calls that take zero, exactly one, and exactly two parameters. If a logger call passes at most two parameters, then the construction of an Object array to package the parameters into varargs is avoided. Needing to "waste" a parameter slot on an enum constant is undesirable. 2. When using Spring Security, it is possible to define an enum that lists the roles and other categories of permissions that are used by a Spring application. For example: package com.myapp.security; import org.springframework.security.core.GrantedAuthority; public enum AppAuthority implements GrantedAuthority { ROLE_USER, ROLE_ADMINISTRATOR; @Override public String getAuthority() { return toString(); } } When using the org.springframework.security.access.annotation.Secured annotation to secure controllers or actions, it would be nice to be able to use an enum constant in the annotation's value, as in @Secured({ ROLE_ADMINISTRATOR.toString() }); however, this is currently not valid Java code because ROLE_ADMINISTRATOR.toString() is not a constant expression. Instead, the name of the role must be duplicated: @Secured({ " ROLE_ADMINISTRATER" }). This is undesirable because it is error-prone and makes refactoring more difficult. It is also more difficult to find usages of a particular authority. If toString() on an enum constant were allowed within a constant expression, then finding usages of a particular authority would be a "Find Usages" operation in an IDE.