Skip to content

Commit 774cd81

Browse files
author
Jörg Kubitz
committed
[performance] Provide more general Map interface for get/setOptions()
and log any error getUnmodifiableOptions() avoids to copy the cached options on each call
1 parent 2fd9321 commit 774cd81

File tree

2 files changed

+73
-29
lines changed

2 files changed

+73
-29
lines changed

org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java

+49
Original file line numberDiff line numberDiff line change
@@ -4615,7 +4615,32 @@ public static String getOptionForConfigurableBuildPathProblemSeverity(int id) {
46154615
* @see #getDefaultOptions()
46164616
* @see JavaCorePreferenceInitializer for changing default settings
46174617
*/
4618+
// TODO @deprecated use {@link #getUnmodifiableOptions(Map)} for better performance
46184619
public static Hashtable<String, String> getOptions() {
4620+
return new Hashtable<>(JavaModelManager.getJavaModelManager().getOptions());
4621+
}
4622+
4623+
/**
4624+
* Returns a unmodifiable map of the current options. Initially, all options have their default values,
4625+
* and this method returns a table that includes all known options.
4626+
* <p>
4627+
* Helper constants have been defined on JavaCore for each of the option IDs
4628+
* (categorized in Code assist option ID, Compiler option ID and Core option ID)
4629+
* and some of their acceptable values (categorized in Option value). Some
4630+
* options accept open value sets beyond the documented constant values.
4631+
* </p>
4632+
* <p>
4633+
* Note: each release may add new options.
4634+
* </p>
4635+
* <p>Returns a default set of options even if the platform is not running.</p>
4636+
*
4637+
* @return table of current settings of all options
4638+
* (key type: <code>String</code>; value type: <code>String</code>)
4639+
* @see #getDefaultOptions()
4640+
* @see JavaCorePreferenceInitializer for changing default settings
4641+
* @since 3.41
4642+
*/
4643+
public static Map<String, String> getUnmodifiableOptions() {
46194644
return JavaModelManager.getJavaModelManager().getOptions();
46204645
}
46214646

@@ -6417,7 +6442,31 @@ public static void setComplianceOptions(String compliance, Map options) {
64176442
* @see JavaCore#getDefaultOptions()
64186443
* @see JavaCorePreferenceInitializer for changing default settings
64196444
*/
6445+
// TODO @deprecated use {@link #setOptions(Map)}
64206446
public static void setOptions(Hashtable<String, String> newOptions) {
6447+
setOptions((Map<String, String> ) newOptions);
6448+
}
6449+
/**
6450+
* Sets the current table of options. All and only the options explicitly
6451+
* included in the given table are remembered; all previous option settings
6452+
* are forgotten, including ones not explicitly mentioned.
6453+
* <p>
6454+
* Helper constants have been defined on JavaCore for each of the option IDs
6455+
* (categorized in Code assist option ID, Compiler option ID and Core option ID)
6456+
* and some of their acceptable values (categorized in Option value). Some
6457+
* options accept open value sets beyond the documented constant values.
6458+
* </p>
6459+
* Note: each release may add new options.
6460+
*
6461+
* @param newOptions
6462+
* the new options (key type: <code>String</code>; value type:
6463+
* <code>String</code>), or <code>null</code> to reset all
6464+
* options to their default values
6465+
* @see JavaCore#getDefaultOptions()
6466+
* @see JavaCorePreferenceInitializer for changing default settings
6467+
* @since 3.41
6468+
*/
6469+
public static void setOptions(Map<String, String> newOptions) {
64216470
JavaModelManager.getJavaModelManager().setOptions(newOptions);
64226471
}
64236472

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaModelManager.java

+24-29
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public void setCache(IPath path, ZipFile zipFile) {
341341
private final static int VALID_OPTION = 2;
342342
HashSet<String> optionNames = new HashSet<>(20);
343343
Map<String, String[]> deprecatedOptions = new HashMap<>();
344-
volatile Hashtable<String, String> optionsCache;
344+
private volatile Map<String, String> optionsCache;
345345

346346
// Preferences
347347
public final IEclipsePreferences[] preferencesLookup = new IEclipsePreferences[2];
@@ -1838,7 +1838,7 @@ private JavaModelManager() {
18381838
/**
18391839
* @deprecated
18401840
*/
1841-
private void addDeprecatedOptions(Hashtable<String, String> options) {
1841+
private void addDeprecatedOptions(Map<String, String> options) {
18421842
options.put(JavaCore.COMPILER_PB_INVALID_IMPORT, JavaCore.ERROR);
18431843
options.put(JavaCore.COMPILER_PB_UNREACHABLE_CODE, JavaCore.ERROR);
18441844
}
@@ -2415,20 +2415,18 @@ public int getOptionLevel(String optionName) {
24152415
return UNKNOWN_OPTION;
24162416
}
24172417

2418-
public Hashtable<String, String> getOptions() {
2419-
2420-
// return cached options if already computed
2421-
Hashtable<String, String> cachedOptions; // use a local variable to avoid race condition (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=256329 )
2422-
if ((cachedOptions = this.optionsCache) != null) {
2423-
return new Hashtable<>(cachedOptions);
2418+
public Map<String, String> getOptions() {
2419+
// use a local variable to avoid race condition (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=256329 )
2420+
Map<String, String> cachedOptions = this.optionsCache;
2421+
if (cachedOptions != null) {
2422+
return cachedOptions;
24242423
}
2424+
24252425
if (!Platform.isRunning()) {
2426-
Hashtable<String, String> defaults = getDefaultOptionsNoInitialization();
2427-
this.optionsCache = defaults;
2428-
return new Hashtable<>(defaults);
2426+
return this.optionsCache = Map.copyOf(getDefaultOptionsNoInitialization());
24292427
}
24302428
// init
2431-
Hashtable<String, String> options = new Hashtable<>(10);
2429+
Map<String, String> options = new HashMap<>(10);
24322430
IPreferencesService service = Platform.getPreferencesService();
24332431

24342432
for (String propertyName : this.optionNames) {
@@ -2458,14 +2456,11 @@ public Hashtable<String, String> getOptions() {
24582456
addDeprecatedOptions(options);
24592457

24602458
Util.fixTaskTags(options);
2461-
// store built map in cache
2462-
this.optionsCache = new Hashtable<>(options);
2463-
// return built map
2464-
return options;
2459+
return this.optionsCache = Map.copyOf(options);
24652460
}
24662461

24672462
// Do not modify without modifying getDefaultOptions()
2468-
private Hashtable<String, String> getDefaultOptionsNoInitialization() {
2463+
private Map<String, String> getDefaultOptionsNoInitialization() {
24692464
Map<String, String> defaultOptionsMap = new CompilerOptions().getMap(); // compiler defaults
24702465

24712466
// Override some compiler defaults
@@ -2520,7 +2515,7 @@ private Hashtable<String, String> getDefaultOptionsNoInitialization() {
25202515
// Time out for parameter names
25212516
defaultOptionsMap.put(JavaCore.TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC, "50"); //$NON-NLS-1$
25222517

2523-
return new Hashtable<>(defaultOptionsMap);
2518+
return defaultOptionsMap;
25242519
}
25252520

25262521
/*
@@ -5321,30 +5316,30 @@ public boolean storePreference(String optionName, String optionValue, IEclipsePr
53215316
return true;
53225317
}
53235318

5324-
public void setOptions(Hashtable<String, String> newOptions) {
5319+
public void setOptions(Map<String, String> newOptions) {
53255320
Hashtable<String, String> cachedValue = newOptions == null ? null : new Hashtable<>(newOptions);
53265321
IEclipsePreferences defaultPreferences = getDefaultPreferences();
53275322
IEclipsePreferences instancePreferences = getInstancePreferences();
53285323

5329-
if (newOptions == null){
5324+
if (newOptions == null) {
53305325
try {
53315326
instancePreferences.clear();
5332-
} catch(BackingStoreException e) {
5333-
// ignore
5327+
} catch (BackingStoreException e) {
5328+
ILog.get().log(Status.warning("Error updating java options", e)); //$NON-NLS-1$
53345329
}
53355330
} else {
5336-
Enumeration<String> keys = newOptions.keys();
5337-
while (keys.hasMoreElements()){
5338-
String key = keys.nextElement();
5331+
for (Entry<String, String> entry : newOptions.entrySet()) {
5332+
String key = entry.getKey();
53395333
int optionLevel = getOptionLevel(key);
5340-
if (optionLevel == UNKNOWN_OPTION) continue; // unrecognized option
5334+
if (optionLevel == UNKNOWN_OPTION)
5335+
continue; // unrecognized option
53415336
if (key.equals(JavaCore.CORE_ENCODING)) {
53425337
if (cachedValue != null) {
53435338
cachedValue.put(key, JavaCore.getEncoding());
53445339
}
53455340
continue; // skipped, contributed by resource prefs
53465341
}
5347-
String value = newOptions.get(key);
5342+
String value = entry.getValue();
53485343
String defaultValue = defaultPreferences.get(key, null);
53495344
// Store value in preferences
53505345
if (defaultValue != null && defaultValue.equals(value)) {
@@ -5355,8 +5350,8 @@ public void setOptions(Hashtable<String, String> newOptions) {
53555350
try {
53565351
// persist options
53575352
instancePreferences.flush();
5358-
} catch(BackingStoreException e) {
5359-
// ignore
5353+
} catch (BackingStoreException e) {
5354+
ILog.get().log(Status.warning("Error updating java options", e)); //$NON-NLS-1$
53605355
}
53615356
}
53625357
if (cachedValue == null) {

0 commit comments

Comments
 (0)