Lines 267-276
Link Here
|
267 |
|
267 |
|
268 |
public final static ICompilationUnit[] NO_WORKING_COPY = new ICompilationUnit[0]; |
268 |
public final static ICompilationUnit[] NO_WORKING_COPY = new ICompilationUnit[0]; |
269 |
|
269 |
|
270 |
// Preferences |
270 |
// Options |
|
|
271 |
private final static int UNKNOWN_OPTION = 0; |
272 |
private final static int DEPRECATED_OPTION = 1; |
273 |
private final static int VALID_OPTION = 2; |
271 |
HashSet optionNames = new HashSet(20); |
274 |
HashSet optionNames = new HashSet(20); |
|
|
275 |
Map deprecatedOptions = new HashMap(); |
272 |
Hashtable optionsCache; |
276 |
Hashtable optionsCache; |
273 |
|
277 |
|
|
|
278 |
// Preferences |
274 |
public final IEclipsePreferences[] preferencesLookup = new IEclipsePreferences[2]; |
279 |
public final IEclipsePreferences[] preferencesLookup = new IEclipsePreferences[2]; |
275 |
static final int PREF_INSTANCE = 0; |
280 |
static final int PREF_INSTANCE = 0; |
276 |
static final int PREF_DEFAULT = 1; |
281 |
static final int PREF_DEFAULT = 1; |
Lines 2038-2052
Link Here
|
2038 |
if (isDeprecatedOption(optionName)) { |
2043 |
if (isDeprecatedOption(optionName)) { |
2039 |
return JavaCore.ERROR; |
2044 |
return JavaCore.ERROR; |
2040 |
} |
2045 |
} |
2041 |
String propertyName = optionName; |
2046 |
int optionLevel = getOptionLevel(optionName); |
2042 |
if (this.optionNames.contains(propertyName)){ |
2047 |
if (optionLevel != UNKNOWN_OPTION){ |
2043 |
IPreferencesService service = Platform.getPreferencesService(); |
2048 |
IPreferencesService service = Platform.getPreferencesService(); |
2044 |
String value = service.get(optionName, null, this.preferencesLookup); |
2049 |
String value = service.get(optionName, null, this.preferencesLookup); |
|
|
2050 |
if (value == null && optionLevel == DEPRECATED_OPTION) { |
2051 |
// May be a deprecated option, retrieve the new value in compatible options |
2052 |
String[] compatibleOptions = (String[]) this.deprecatedOptions.get(optionName); |
2053 |
value = service.get(compatibleOptions[0], null, this.preferencesLookup); |
2054 |
} |
2045 |
return value==null ? null : value.trim(); |
2055 |
return value==null ? null : value.trim(); |
2046 |
} |
2056 |
} |
2047 |
return null; |
2057 |
return null; |
2048 |
} |
2058 |
} |
2049 |
|
2059 |
|
|
|
2060 |
/** |
2061 |
* Returns the value of the given option for the given Eclipse preferences. |
2062 |
* If no value was already set, then inherits from the global options if specified. |
2063 |
* |
2064 |
* @param optionName The name of the option |
2065 |
* @param inheritJavaCoreOptions Tells whether the value can be inherited from global JavaCore options |
2066 |
* @param projectPreferences The eclipse preferences from which to get the value |
2067 |
* @return The value of the option. May be <code>null</code> |
2068 |
*/ |
2069 |
public String getOption(String optionName, boolean inheritJavaCoreOptions, IEclipsePreferences projectPreferences) { |
2070 |
// Return the option value depending on its level |
2071 |
switch (getOptionLevel(optionName)) { |
2072 |
case VALID_OPTION: |
2073 |
// Valid option, return the preference value |
2074 |
String javaCoreDefault = inheritJavaCoreOptions ? JavaCore.getOption(optionName) : null; |
2075 |
if (projectPreferences == null) return javaCoreDefault; |
2076 |
String value = projectPreferences.get(optionName, javaCoreDefault); |
2077 |
return value == null ? null : value.trim(); |
2078 |
case DEPRECATED_OPTION: |
2079 |
// Return the deprecated option value if it was already set |
2080 |
String oldValue = projectPreferences.get(optionName, null); |
2081 |
if (oldValue != null) { |
2082 |
return oldValue.trim(); |
2083 |
} |
2084 |
// Get the new compatible value |
2085 |
String[] compatibleOptions = (String[]) this.deprecatedOptions.get(optionName); |
2086 |
String newDefault = inheritJavaCoreOptions ? JavaCore.getOption(compatibleOptions[0]) : null; |
2087 |
String newValue = projectPreferences.get(compatibleOptions[0], newDefault); |
2088 |
return newValue == null ? null : newValue.trim(); |
2089 |
} |
2090 |
return null; |
2091 |
} |
2092 |
|
2093 |
/** |
2094 |
* Returns whether an option name is known or not. |
2095 |
* |
2096 |
* @param optionName The name of the option |
2097 |
* @return <code>true</code> when the option name is either |
2098 |
* {@link #VALID_OPTION valid} or {@link #DEPRECATED_OPTION deprecated}, |
2099 |
* <code>false</code> otherwise. |
2100 |
*/ |
2101 |
public boolean knowsOption(String optionName) { |
2102 |
boolean knownOption = this.optionNames.contains(optionName); |
2103 |
if (!knownOption) { |
2104 |
knownOption = this.deprecatedOptions.get(optionName) != null; |
2105 |
} |
2106 |
return knownOption; |
2107 |
} |
2108 |
|
2109 |
/** |
2110 |
* Returns the level of the given option. |
2111 |
* |
2112 |
* @param optionName The name of the option |
2113 |
* @return The level of the option as an int which may have the following |
2114 |
* values: |
2115 |
* <ul> |
2116 |
* <li>{@link #UNKNOWN_OPTION}: the given option is unknown</li> |
2117 |
* <li>{@link #DEPRECATED_OPTION}: the given option is deprecated</li> |
2118 |
* <li>{@link #VALID_OPTION}: the given option is valid</li> |
2119 |
* </ul> |
2120 |
*/ |
2121 |
public int getOptionLevel(String optionName) { |
2122 |
if (this.optionNames.contains(optionName)) { |
2123 |
return VALID_OPTION; |
2124 |
} |
2125 |
if (this.deprecatedOptions.get(optionName) != null) { |
2126 |
return DEPRECATED_OPTION; |
2127 |
} |
2128 |
return UNKNOWN_OPTION; |
2129 |
} |
2130 |
|
2050 |
public Hashtable getOptions() { |
2131 |
public Hashtable getOptions() { |
2051 |
|
2132 |
|
2052 |
// return cached options if already computed |
2133 |
// return cached options if already computed |
Lines 2070-2090
Link Here
|
2070 |
} |
2151 |
} |
2071 |
} |
2152 |
} |
2072 |
|
2153 |
|
|
|
2154 |
// set deprecated options using preferences service lookup |
2155 |
Iterator deprecatedEntries = this.deprecatedOptions.entrySet().iterator(); |
2156 |
while (deprecatedEntries.hasNext()) { |
2157 |
Entry entry = (Entry) deprecatedEntries.next(); |
2158 |
String propertyName = (String) entry.getKey(); |
2159 |
String propertyValue = service.get(propertyName, null, this.preferencesLookup); |
2160 |
if (propertyValue != null) { |
2161 |
options.put(propertyName, propertyValue); |
2162 |
String[] compatibleOptions = (String[]) entry.getValue(); |
2163 |
for (int co=0, length=compatibleOptions.length; co < length; co++) { |
2164 |
options.put(compatibleOptions[co], propertyValue); |
2165 |
} |
2166 |
} |
2167 |
} |
2168 |
|
2073 |
// get encoding through resource plugin |
2169 |
// get encoding through resource plugin |
2074 |
options.put(JavaCore.CORE_ENCODING, JavaCore.getEncoding()); |
2170 |
options.put(JavaCore.CORE_ENCODING, JavaCore.getEncoding()); |
2075 |
|
2171 |
|
2076 |
// backward compatibility |
2172 |
// backward compatibility |
2077 |
addDeprecatedOptions(options); |
2173 |
addDeprecatedOptions(options); |
2078 |
try { |
|
|
2079 |
final IEclipsePreferences eclipsePreferences = this.preferencesLookup[PREF_INSTANCE]; |
2080 |
String[] instanceKeys = eclipsePreferences.keys(); |
2081 |
for (int i=0, length=instanceKeys.length; i<length; i++) { |
2082 |
String optionName = instanceKeys[i]; |
2083 |
migrateObsoleteOption(options, optionName, eclipsePreferences.get(optionName, null)); |
2084 |
} |
2085 |
} catch (BackingStoreException e) { |
2086 |
// skip |
2087 |
} |
2088 |
|
2174 |
|
2089 |
Util.fixTaskTags(options); |
2175 |
Util.fixTaskTags(options); |
2090 |
// store built map in cache |
2176 |
// store built map in cache |
Lines 2094-2156
Link Here
|
2094 |
return options; |
2180 |
return options; |
2095 |
} |
2181 |
} |
2096 |
|
2182 |
|
2097 |
/** |
|
|
2098 |
* Migrates an old option value to its new corresponding option name(s) |
2099 |
* when necessary. |
2100 |
* <p> |
2101 |
* Nothing is done if the given option is not obsolete or if no migration has been |
2102 |
* specified for it. |
2103 |
* </p><p> |
2104 |
* Currently, migration is only done for formatter options. |
2105 |
* </p> |
2106 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=308000" |
2107 |
* |
2108 |
* @param options The options map to update |
2109 |
* @param optionName The old option name to update |
2110 |
* @param optionValue The value of the old option name |
2111 |
*/ |
2112 |
public void migrateObsoleteOption(Map options, String optionName, String optionValue) { |
2113 |
|
2114 |
// Migrate formatter options |
2115 |
String[] compatibleConstants = getFormatterCompatibleConstants(optionName); |
2116 |
if (compatibleConstants != null) { |
2117 |
for (int i=0, length=compatibleConstants.length; i < length; i++) { |
2118 |
options.put(compatibleConstants[i], optionValue); |
2119 |
} |
2120 |
return; |
2121 |
} |
2122 |
} |
2123 |
|
2124 |
/** |
2125 |
* Return an array of compatible constants for an obsolete constant. |
2126 |
* |
2127 |
* @param name The name of the obsolete constant |
2128 |
* @return The list as a non-empty array of the compatible constants or |
2129 |
* <code>null</code> if the constant is <b>not</b> obsolete. |
2130 |
* @deprecated As using deprecated formatter constants |
2131 |
*/ |
2132 |
private static String[] getFormatterCompatibleConstants(String name) { |
2133 |
if (DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_MEMBER.equals(name)) { |
2134 |
return new String[] { |
2135 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_FIELD, |
2136 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_METHOD, |
2137 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PACKAGE, |
2138 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_TYPE |
2139 |
}; |
2140 |
} |
2141 |
if (DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION.equals(name)) { |
2142 |
return new String[] { |
2143 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_FIELD, |
2144 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_METHOD, |
2145 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PACKAGE, |
2146 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_TYPE, |
2147 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_LOCAL_VARIABLE, |
2148 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PARAMETER |
2149 |
}; |
2150 |
} |
2151 |
return null; |
2152 |
} |
2153 |
|
2154 |
// Do not modify without modifying getDefaultOptions() |
2183 |
// Do not modify without modifying getDefaultOptions() |
2155 |
private Hashtable getDefaultOptionsNoInitialization() { |
2184 |
private Hashtable getDefaultOptionsNoInitialization() { |
2156 |
Map defaultOptionsMap = new CompilerOptions().getMap(); // compiler defaults |
2185 |
Map defaultOptionsMap = new CompilerOptions().getMap(); // compiler defaults |
Lines 4692-4697
Link Here
|
4692 |
} |
4721 |
} |
4693 |
} |
4722 |
} |
4694 |
|
4723 |
|
|
|
4724 |
/** |
4725 |
* Store the preferences value for the given option name. |
4726 |
* |
4727 |
* @param optionName The name of the option |
4728 |
* @param optionValue The value of the option. If <code>null</code>, then |
4729 |
* the option will be removed from the preferences instead. |
4730 |
* @param eclipsePreferences The eclipse preferences to be updated |
4731 |
* @return <code>true</code> if the preferences have been changed, |
4732 |
* <code>false</code> otherwise. |
4733 |
*/ |
4734 |
public boolean storePreference(String optionName, String optionValue, IEclipsePreferences eclipsePreferences) { |
4735 |
int optionLevel = this.getOptionLevel(optionName); |
4736 |
if (optionLevel == UNKNOWN_OPTION) return false; // unrecognized option |
4737 |
|
4738 |
// Store option value |
4739 |
switch (optionLevel) { |
4740 |
case JavaModelManager.VALID_OPTION: |
4741 |
if (optionValue == null) { |
4742 |
eclipsePreferences.remove(optionName); |
4743 |
} else { |
4744 |
eclipsePreferences.put(optionName, optionValue); |
4745 |
} |
4746 |
break; |
4747 |
case JavaModelManager.DEPRECATED_OPTION: |
4748 |
// Try to migrate deprecated option |
4749 |
eclipsePreferences.remove(optionName); // get rid off old preference |
4750 |
String[] compatibleOptions = (String[]) this.deprecatedOptions.get(optionName); |
4751 |
for (int co=0, length=compatibleOptions.length; co < length; co++) { |
4752 |
if (optionValue == null) { |
4753 |
eclipsePreferences.remove(compatibleOptions[co]); |
4754 |
} else { |
4755 |
eclipsePreferences.put(compatibleOptions[co], optionValue); |
4756 |
} |
4757 |
} |
4758 |
break; |
4759 |
default: |
4760 |
return false; |
4761 |
} |
4762 |
return true; |
4763 |
} |
4764 |
|
4695 |
public void setOptions(Hashtable newOptions) { |
4765 |
public void setOptions(Hashtable newOptions) { |
4696 |
|
4766 |
|
4697 |
if (DEBUG_302850) { |
4767 |
if (DEBUG_302850) { |
Lines 4718-4737
Link Here
|
4718 |
Enumeration keys = newOptions.keys(); |
4788 |
Enumeration keys = newOptions.keys(); |
4719 |
while (keys.hasMoreElements()){ |
4789 |
while (keys.hasMoreElements()){ |
4720 |
String key = (String)keys.nextElement(); |
4790 |
String key = (String)keys.nextElement(); |
4721 |
if (!this.optionNames.contains(key)) continue; // unrecognized option |
4791 |
int optionLevel = getOptionLevel(key); |
|
|
4792 |
if (optionLevel == UNKNOWN_OPTION) continue; // unrecognized option |
4722 |
if (key.equals(JavaCore.CORE_ENCODING)) { |
4793 |
if (key.equals(JavaCore.CORE_ENCODING)) { |
4723 |
if (cachedValue != null) { |
4794 |
if (cachedValue != null) { |
4724 |
cachedValue.put(key, JavaCore.getEncoding()); |
4795 |
cachedValue.put(key, JavaCore.getEncoding()); |
4725 |
} |
4796 |
} |
4726 |
continue; // skipped, contributed by resource prefs |
4797 |
continue; // skipped, contributed by resource prefs |
4727 |
} |
4798 |
} |
4728 |
String value = (String)newOptions.get(key); |
4799 |
String value = (String) newOptions.get(key); |
4729 |
String defaultValue = defaultPreferences.get(key, null); |
4800 |
String defaultValue = defaultPreferences.get(key, null); |
|
|
4801 |
// Store value in preferences |
4730 |
if (defaultValue != null && defaultValue.equals(value)) { |
4802 |
if (defaultValue != null && defaultValue.equals(value)) { |
4731 |
instancePreferences.remove(key); |
4803 |
value = null; |
4732 |
} else { |
|
|
4733 |
instancePreferences.put(key, value); |
4734 |
} |
4804 |
} |
|
|
4805 |
storePreference(key, value, instancePreferences); |
4735 |
} |
4806 |
} |
4736 |
try { |
4807 |
try { |
4737 |
// persist options |
4808 |
// persist options |