Adding constant for line.separator and friends
Joe Darcy
Joe.Darcy at Sun.COM
Tue Nov 10 04:14:07 UTC 2009
Martin Buchholz wrote:
> (In response to Joe, who once asked me for little things to add
> to the core libraries)
>
> Lots of classes need to use System.getProperty("line.separator").
> Many don't do it right because you need to use
> a doPrivileged block whenever you read a system property.
> Yet it is no secret - you can divine the line separator
> even if you have no trust with the security manager.
>
> Here's a strawman proposal:
>
> http://cr.openjdk.java.net/~martin/webrevs/openjdk7/line.separator/
>
> diff --git a/src/share/classes/java/lang/System.java
> b/src/share/classes/java/lang/System.java
> --- a/src/share/classes/java/lang/System.java
> +++ b/src/share/classes/java/lang/System.java
> @@ -620,6 +620,32 @@
> }
>
> /**
> + * Defines some standard system properties as constant strings.
> + */
> + public static class StandardProperties {
> + public final static String FILE_SEPARATOR;
> + public final static String PATH_SEPARATOR;
> + public final static String LINE_SEPARATOR;
> +
> + static {
> + String[] props =
> + AccessController.doPrivileged
> + (new PrivilegedAction<String[]>() {
> + public String[] run() {
> + return new String[] {
> + getProperty("file.separator"),
> + getProperty("path.separator"),
> + getProperty("line.separator")
> + };
> + }
> + });
> + FILE_SEPARATOR = props[0];
> + PATH_SEPARATOR = props[1];
> + LINE_SEPARATOR = props[2];
> + }
> + }
> +
> + /**
> * Sets the system properties to the <code>Properties</code>
> * argument.
> * <p>
>
Hi Martin,
Given that these values are not true constants since they vary across
platforms, I think it is misleading to make them look like constant by
having them be "public static final" fields with ALL CAPS names.
I would prefer to see these values returned by wrapper methods that did
the necessary security checks and caching.
-Joe
More information about the core-libs-dev
mailing list