From 75d498ac5a4b77f55e7d6a25e86d97b15b9a3e85 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Fri, 10 Jun 2022 13:47:02 +0200 Subject: [PATCH] Don't use "file.encoding", use Platform.getNativeEncoding() instead 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 https://github.com/eclipse-platform/eclipse.platform.resources/issues/154 --- .../META-INF/MANIFEST.MF | 2 +- .../core/resources/ResourcesPlugin.java | 57 ++++++++++++------- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/bundles/org.eclipse.core.resources/META-INF/MANIFEST.MF b/bundles/org.eclipse.core.resources/META-INF/MANIFEST.MF index 429369a26..f4f54e35f 100644 --- a/bundles/org.eclipse.core.resources/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.core.resources/META-INF/MANIFEST.MF @@ -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 diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java index 97743f6d6..3d70bf72e 100644 --- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java +++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/resources/ResourcesPlugin.java @@ -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 - * System.getProperty("file.encoding"). - * There is also a convenience method getEncoding 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 + * System.getProperty("file.encoding") before Java 18, and using + * System.getProperty("native.encoding") with Java 18 and higher versions. + * There is also a convenience method getEncoding which returns the + * value of this preference, or the file system encoding if this preference is + * not set. *

* Note that there is no guarantee that the value is a supported encoding. - * Callers should be prepared to handle UnsupportedEncodingException - * where this encoding is used. + * Callers should be prepared to handle + * UnsupportedEncodingException where this encoding is used. *

* * @see #getEncoding() @@ -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 PREF_ENCODING preference, or the file system - * encoding (System.getProperty("file.encoding")) 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 PREF_ENCODING 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 + * *

* Note that this method does not check whether the result is a supported * encoding. Callers should be prepared to handle @@ -391,36 +397,43 @@ public final class ResourcesPlugin extends Plugin { * ServiceTracker, Blueprint, ...) to track the workspace *

  • Calling workspace.getRoot().getDefaultCharset(false)
  • *
  • If null is returned take the appropriate action, e.g fall - * back to System.getProperty("file.encoding") or even better - * {@link Charset#defaultCharset()}
  • + * back to {@link Platform#getNativeEncoding()} * * * @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 native 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(); } /**