Skip to content

Commit

Permalink
Don't use "file.encoding", use Platform.getNativeEncoding() instead
Browse files Browse the repository at this point in the history
With https://openjdk.java.net/jeps/400 implemented in Java 18,
"file.encoding" system property became meaningless and can't be used
anymore to determine system native encoding.

Instead, use new Platform.getNativeEncoding() API that tries to provide
a suitable replacement compatible with Java 18+ and previous Java
versions.

See eclipse-platform#154
  • Loading branch information
iloveeclipse committed Jun 10, 2022
1 parent d914d91 commit 75d498a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bundles/org.eclipse.core.resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Export-Package: org.eclipse.core.internal.dtree;x-internal:=true,
Require-Bundle: org.eclipse.ant.core;bundle-version="[3.1.0,4.0.0)";resolution:=optional,
org.eclipse.core.expressions;bundle-version="[3.2.0,4.0.0)",
org.eclipse.core.filesystem;bundle-version="[1.3.0,2.0.0)",
org.eclipse.core.runtime;bundle-version="[3.25.0,4.0.0)"
org.eclipse.core.runtime;bundle-version="[3.26.0,4.0.0)"
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/ResourceChangeListenerRegistrar.xml,
OSGI-INF/org.eclipse.core.internal.resources.CheckMissingNaturesListener.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,19 @@ public final class ResourcesPlugin extends Plugin {
public static final Object FAMILY_MANUAL_REFRESH = new Object();

/**
* Name of a preference indicating the encoding to use when reading text
* files in the workspace. The value is a string, and may
* be the default empty string, indicating that the file system encoding should
* be used instead. The file system encoding can be retrieved using
* <code>System.getProperty("file.encoding")</code>.
* There is also a convenience method <code>getEncoding</code> which returns
* the value of this preference, or the file system encoding if this
* preference is not set.
* Name of a preference indicating the encoding to use when reading text files
* in the workspace. The value is a string, and may be the default empty string,
* indicating that the file system encoding should be used instead. The file
* system encoding can be retrieved using
* <code>System.getProperty("file.encoding")</code> before Java 18, and using
* <code>System.getProperty("native.encoding") with Java 18 and higher versions</code>.
* There is also a convenience method <code>getEncoding</code> which returns the
* value of this preference, or the file system encoding if this preference is
* not set.
* <p>
* Note that there is no guarantee that the value is a supported encoding.
* Callers should be prepared to handle <code>UnsupportedEncodingException</code>
* where this encoding is used.
* Callers should be prepared to handle
* <code>UnsupportedEncodingException</code> where this encoding is used.
* </p>
*
* @see #getEncoding()
Expand Down Expand Up @@ -372,10 +373,15 @@ public final class ResourcesPlugin extends Plugin {

/**
* Returns the encoding to use when reading text files in the workspace. This is
* the value of the <code>PREF_ENCODING</code> preference, or the file system
* encoding (<code>System.getProperty("file.encoding")</code>) if the preference
* is not set, the workspace is not ready yet or any other condition where it
* could not be determined.
* the value of the <code>PREF_ENCODING</code> preference. If the preference is
* not set, the workspace is not ready yet or any other condition where it could
* not be determined, returns native file system encoding, as specified by
* <ul>
* <li><code>System.getProperty("native.encoding")</code> on Java 18 and later
* </li>
* <li><code>System.getProperty("sun.jnu.encoding")</code> on previous Java
* versions</li>
* </ul>
* <p>
* Note that this method does not check whether the result is a supported
* encoding. Callers should be prepared to handle
Expand All @@ -391,36 +397,43 @@ public final class ResourcesPlugin extends Plugin {
* ServiceTracker, Blueprint, ...) to track the workspace</li>
* <li>Calling workspace.getRoot().getDefaultCharset(false)</li>
* <li>If <code>null</code> is returned take the appropriate action, e.g fall
* back to <code>System.getProperty("file.encoding")</code> or even better
* {@link Charset#defaultCharset()}</li>
* back to {@link Platform#getNativeEncoding()}</li>
* </ol>
*
* @return the encoding to use when reading text files in the workspace
* @see java.io.UnsupportedEncodingException
* @see #getSystemEncoding()
*/
public static String getEncoding() {
ResourcesPlugin resourcesPlugin = plugin;
if (resourcesPlugin == null) {
return getSystemEncoding();
return getSystemEncoding().name();
}
Workspace workspace = resourcesPlugin.workspaceInitCustomizer.workspace;
if (workspace == null) {
return getSystemEncoding();
return getSystemEncoding().name();
}
try {
String enc = workspace.getRoot().getDefaultCharset(false);
if (enc == null) {
return getSystemEncoding();
return getSystemEncoding().name();
}
return enc;
} catch (CoreException e) {
// should never happen here... even if we fall back to system encoding...
return getSystemEncoding();
return getSystemEncoding().name();
}
}

private static String getSystemEncoding() {
return System.getProperty("file.encoding"); //$NON-NLS-1$
/**
* Retrieves the <b>native</b> system encoding ({@link Charset}) based on the
* system locale.
*
* @return the {@link Charset}, never null
* @see Platform#getNativeEncoding()
*/
private static Charset getSystemEncoding() {
return Platform.getNativeEncoding();
}

/**
Expand Down

0 comments on commit 75d498a

Please sign in to comment.