Lines 270-279
Link Here
|
270 |
|
270 |
|
271 |
public final static ICompilationUnit[] NO_WORKING_COPY = new ICompilationUnit[0]; |
271 |
public final static ICompilationUnit[] NO_WORKING_COPY = new ICompilationUnit[0]; |
272 |
|
272 |
|
273 |
// Preferences |
273 |
// Options |
|
|
274 |
private final static int UNKNOWN_OPTION = 0; |
275 |
private final static int DEPRECATED_OPTION = 1; |
276 |
private final static int VALID_OPTION = 2; |
274 |
HashSet optionNames = new HashSet(20); |
277 |
HashSet optionNames = new HashSet(20); |
|
|
278 |
Map deprecatedOptions = new HashMap(); |
275 |
Hashtable optionsCache; |
279 |
Hashtable optionsCache; |
276 |
|
280 |
|
|
|
281 |
// Preferences |
277 |
public final IEclipsePreferences[] preferencesLookup = new IEclipsePreferences[2]; |
282 |
public final IEclipsePreferences[] preferencesLookup = new IEclipsePreferences[2]; |
278 |
static final int PREF_INSTANCE = 0; |
283 |
static final int PREF_INSTANCE = 0; |
279 |
static final int PREF_DEFAULT = 1; |
284 |
static final int PREF_DEFAULT = 1; |
Lines 2057-2071
Link Here
|
2057 |
if (isDeprecatedOption(optionName)) { |
2062 |
if (isDeprecatedOption(optionName)) { |
2058 |
return JavaCore.ERROR; |
2063 |
return JavaCore.ERROR; |
2059 |
} |
2064 |
} |
2060 |
String propertyName = optionName; |
2065 |
int optionLevel = getOptionLevel(optionName); |
2061 |
if (this.optionNames.contains(propertyName)){ |
2066 |
if (optionLevel != UNKNOWN_OPTION){ |
2062 |
IPreferencesService service = Platform.getPreferencesService(); |
2067 |
IPreferencesService service = Platform.getPreferencesService(); |
2063 |
String value = service.get(optionName, null, this.preferencesLookup); |
2068 |
String value = service.get(optionName, null, this.preferencesLookup); |
|
|
2069 |
if (value == null && optionLevel == DEPRECATED_OPTION) { |
2070 |
// May be a deprecated option, retrieve the new value in compatible options |
2071 |
String[] compatibleOptions = (String[]) this.deprecatedOptions.get(optionName); |
2072 |
value = service.get(compatibleOptions[0], null, this.preferencesLookup); |
2073 |
} |
2064 |
return value==null ? null : value.trim(); |
2074 |
return value==null ? null : value.trim(); |
2065 |
} |
2075 |
} |
2066 |
return null; |
2076 |
return null; |
2067 |
} |
2077 |
} |
2068 |
|
2078 |
|
|
|
2079 |
/** |
2080 |
* Returns the value of the given option for the given Eclipse preferences. |
2081 |
* If no value was already set, then inherits from the global options if specified. |
2082 |
* |
2083 |
* @param optionName The name of the option |
2084 |
* @param inheritJavaCoreOptions Tells whether the value can be inherited from global JavaCore options |
2085 |
* @param projectPreferences The eclipse preferences from which to get the value |
2086 |
* @return The value of the option. May be <code>null</code> |
2087 |
*/ |
2088 |
public String getOption(String optionName, boolean inheritJavaCoreOptions, IEclipsePreferences projectPreferences) { |
2089 |
// Return the option value depending on its level |
2090 |
switch (getOptionLevel(optionName)) { |
2091 |
case VALID_OPTION: |
2092 |
// Valid option, return the preference value |
2093 |
String javaCoreDefault = inheritJavaCoreOptions ? JavaCore.getOption(optionName) : null; |
2094 |
if (projectPreferences == null) return javaCoreDefault; |
2095 |
String value = projectPreferences.get(optionName, javaCoreDefault); |
2096 |
return value == null ? null : value.trim(); |
2097 |
case DEPRECATED_OPTION: |
2098 |
// Return the deprecated option value if it was already set |
2099 |
String oldValue = projectPreferences.get(optionName, null); |
2100 |
if (oldValue != null) { |
2101 |
return oldValue.trim(); |
2102 |
} |
2103 |
// Get the new compatible value |
2104 |
String[] compatibleOptions = (String[]) this.deprecatedOptions.get(optionName); |
2105 |
String newDefault = inheritJavaCoreOptions ? JavaCore.getOption(compatibleOptions[0]) : null; |
2106 |
String newValue = projectPreferences.get(compatibleOptions[0], newDefault); |
2107 |
return newValue == null ? null : newValue.trim(); |
2108 |
} |
2109 |
return null; |
2110 |
} |
2111 |
|
2112 |
/** |
2113 |
* Returns whether an option name is known or not. |
2114 |
* |
2115 |
* @param optionName The name of the option |
2116 |
* @return <code>true</code> when the option name is either |
2117 |
* {@link #VALID_OPTION valid} or {@link #DEPRECATED_OPTION deprecated}, |
2118 |
* <code>false</code> otherwise. |
2119 |
*/ |
2120 |
public boolean knowsOption(String optionName) { |
2121 |
boolean knownOption = this.optionNames.contains(optionName); |
2122 |
if (!knownOption) { |
2123 |
knownOption = this.deprecatedOptions.get(optionName) != null; |
2124 |
} |
2125 |
return knownOption; |
2126 |
} |
2127 |
|
2128 |
/** |
2129 |
* Returns the level of the given option. |
2130 |
* |
2131 |
* @param optionName The name of the option |
2132 |
* @return The level of the option as an int which may have the following |
2133 |
* values: |
2134 |
* <ul> |
2135 |
* <li>{@link #UNKNOWN_OPTION}: the given option is unknown</li> |
2136 |
* <li>{@link #DEPRECATED_OPTION}: the given option is deprecated</li> |
2137 |
* <li>{@link #VALID_OPTION}: the given option is valid</li> |
2138 |
* </ul> |
2139 |
*/ |
2140 |
public int getOptionLevel(String optionName) { |
2141 |
if (this.optionNames.contains(optionName)) { |
2142 |
return VALID_OPTION; |
2143 |
} |
2144 |
if (this.deprecatedOptions.get(optionName) != null) { |
2145 |
return DEPRECATED_OPTION; |
2146 |
} |
2147 |
return UNKNOWN_OPTION; |
2148 |
} |
2149 |
|
2069 |
public Hashtable getOptions() { |
2150 |
public Hashtable getOptions() { |
2070 |
|
2151 |
|
2071 |
// return cached options if already computed |
2152 |
// return cached options if already computed |
Lines 2095-2115
Link Here
|
2095 |
} |
2176 |
} |
2096 |
if (DEBUG_302850) checkTaskTags("Options initialized from preferences", options); //$NON-NLS-1$ |
2177 |
if (DEBUG_302850) checkTaskTags("Options initialized from preferences", options); //$NON-NLS-1$ |
2097 |
|
2178 |
|
|
|
2179 |
// set deprecated options using preferences service lookup |
2180 |
Iterator deprecatedEntries = this.deprecatedOptions.entrySet().iterator(); |
2181 |
while (deprecatedEntries.hasNext()) { |
2182 |
Entry entry = (Entry) deprecatedEntries.next(); |
2183 |
String propertyName = (String) entry.getKey(); |
2184 |
String propertyValue = service.get(propertyName, null, this.preferencesLookup); |
2185 |
if (propertyValue != null) { |
2186 |
options.put(propertyName, propertyValue); |
2187 |
String[] compatibleOptions = (String[]) entry.getValue(); |
2188 |
for (int co=0, length=compatibleOptions.length; co < length; co++) { |
2189 |
String compatibleOption = compatibleOptions[co]; |
2190 |
if (!options.containsKey(compatibleOption)) |
2191 |
options.put(compatibleOption, propertyValue); |
2192 |
} |
2193 |
} |
2194 |
} |
2195 |
|
2098 |
// get encoding through resource plugin |
2196 |
// get encoding through resource plugin |
2099 |
options.put(JavaCore.CORE_ENCODING, JavaCore.getEncoding()); |
2197 |
options.put(JavaCore.CORE_ENCODING, JavaCore.getEncoding()); |
2100 |
|
2198 |
|
2101 |
// backward compatibility |
2199 |
// backward compatibility |
2102 |
addDeprecatedOptions(options); |
2200 |
addDeprecatedOptions(options); |
2103 |
try { |
|
|
2104 |
final IEclipsePreferences eclipsePreferences = this.preferencesLookup[PREF_INSTANCE]; |
2105 |
String[] instanceKeys = eclipsePreferences.keys(); |
2106 |
for (int i=0, length=instanceKeys.length; i<length; i++) { |
2107 |
String optionName = instanceKeys[i]; |
2108 |
migrateObsoleteOption(options, optionName, eclipsePreferences.get(optionName, null)); |
2109 |
} |
2110 |
} catch (BackingStoreException e) { |
2111 |
// skip |
2112 |
} |
2113 |
|
2201 |
|
2114 |
Util.fixTaskTags(options); |
2202 |
Util.fixTaskTags(options); |
2115 |
if (DEBUG_302850) checkTaskTags("Retrieved options from preferences", options); //$NON-NLS-1$ |
2203 |
if (DEBUG_302850) checkTaskTags("Retrieved options from preferences", options); //$NON-NLS-1$ |
Lines 2136-2198
Link Here
|
2136 |
} |
2224 |
} |
2137 |
} |
2225 |
} |
2138 |
|
2226 |
|
2139 |
/** |
|
|
2140 |
* Migrates an old option value to its new corresponding option name(s) |
2141 |
* when necessary. |
2142 |
* <p> |
2143 |
* Nothing is done if the given option is not obsolete or if no migration has been |
2144 |
* specified for it. |
2145 |
* </p><p> |
2146 |
* Currently, migration is only done for formatter options. |
2147 |
* </p> |
2148 |
* @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=308000" |
2149 |
* |
2150 |
* @param options The options map to update |
2151 |
* @param optionName The old option name to update |
2152 |
* @param optionValue The value of the old option name |
2153 |
*/ |
2154 |
public void migrateObsoleteOption(Map options, String optionName, String optionValue) { |
2155 |
|
2156 |
// Migrate formatter options |
2157 |
String[] compatibleConstants = getFormatterCompatibleConstants(optionName); |
2158 |
if (compatibleConstants != null) { |
2159 |
for (int i=0, length=compatibleConstants.length; i < length; i++) { |
2160 |
options.put(compatibleConstants[i], optionValue); |
2161 |
} |
2162 |
return; |
2163 |
} |
2164 |
} |
2165 |
|
2166 |
/** |
2167 |
* Return an array of compatible constants for an obsolete constant. |
2168 |
* |
2169 |
* @param name The name of the obsolete constant |
2170 |
* @return The list as a non-empty array of the compatible constants or |
2171 |
* <code>null</code> if the constant is <b>not</b> obsolete. |
2172 |
* @deprecated As using deprecated formatter constants |
2173 |
*/ |
2174 |
private static String[] getFormatterCompatibleConstants(String name) { |
2175 |
if (DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_MEMBER.equals(name)) { |
2176 |
return new String[] { |
2177 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_FIELD, |
2178 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_METHOD, |
2179 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PACKAGE, |
2180 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_TYPE |
2181 |
}; |
2182 |
} |
2183 |
if (DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION.equals(name)) { |
2184 |
return new String[] { |
2185 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_FIELD, |
2186 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_METHOD, |
2187 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PACKAGE, |
2188 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_TYPE, |
2189 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_LOCAL_VARIABLE, |
2190 |
DefaultCodeFormatterConstants.FORMATTER_INSERT_NEW_LINE_AFTER_ANNOTATION_ON_PARAMETER |
2191 |
}; |
2192 |
} |
2193 |
return null; |
2194 |
} |
2195 |
|
2196 |
// Do not modify without modifying getDefaultOptions() |
2227 |
// Do not modify without modifying getDefaultOptions() |
2197 |
private Hashtable getDefaultOptionsNoInitialization() { |
2228 |
private Hashtable getDefaultOptionsNoInitialization() { |
2198 |
Map defaultOptionsMap = new CompilerOptions().getMap(); // compiler defaults |
2229 |
Map defaultOptionsMap = new CompilerOptions().getMap(); // compiler defaults |
Lines 4750-4755
Link Here
|
4750 |
} |
4781 |
} |
4751 |
} |
4782 |
} |
4752 |
|
4783 |
|
|
|
4784 |
/** |
4785 |
* Store the preferences value for the given option name. |
4786 |
* |
4787 |
* @param optionName The name of the option |
4788 |
* @param optionValue The value of the option. If <code>null</code>, then |
4789 |
* the option will be removed from the preferences instead. |
4790 |
* @param eclipsePreferences The eclipse preferences to be updated |
4791 |
* @return <code>true</code> if the preferences have been changed, |
4792 |
* <code>false</code> otherwise. |
4793 |
*/ |
4794 |
public boolean storePreference(String optionName, String optionValue, IEclipsePreferences eclipsePreferences) { |
4795 |
int optionLevel = this.getOptionLevel(optionName); |
4796 |
if (optionLevel == UNKNOWN_OPTION) return false; // unrecognized option |
4797 |
|
4798 |
// Store option value |
4799 |
switch (optionLevel) { |
4800 |
case JavaModelManager.VALID_OPTION: |
4801 |
if (optionValue == null) { |
4802 |
eclipsePreferences.remove(optionName); |
4803 |
} else { |
4804 |
eclipsePreferences.put(optionName, optionValue); |
4805 |
} |
4806 |
break; |
4807 |
case JavaModelManager.DEPRECATED_OPTION: |
4808 |
// Try to migrate deprecated option |
4809 |
eclipsePreferences.remove(optionName); // get rid off old preference |
4810 |
String[] compatibleOptions = (String[]) this.deprecatedOptions.get(optionName); |
4811 |
for (int co=0, length=compatibleOptions.length; co < length; co++) { |
4812 |
if (optionValue == null) { |
4813 |
eclipsePreferences.remove(compatibleOptions[co]); |
4814 |
} else { |
4815 |
eclipsePreferences.put(compatibleOptions[co], optionValue); |
4816 |
} |
4817 |
} |
4818 |
break; |
4819 |
default: |
4820 |
return false; |
4821 |
} |
4822 |
return true; |
4823 |
} |
4824 |
|
4753 |
public void setOptions(Hashtable newOptions) { |
4825 |
public void setOptions(Hashtable newOptions) { |
4754 |
|
4826 |
|
4755 |
if (DEBUG_302850) { |
4827 |
if (DEBUG_302850) { |
Lines 4776-4795
Link Here
|
4776 |
Enumeration keys = newOptions.keys(); |
4848 |
Enumeration keys = newOptions.keys(); |
4777 |
while (keys.hasMoreElements()){ |
4849 |
while (keys.hasMoreElements()){ |
4778 |
String key = (String)keys.nextElement(); |
4850 |
String key = (String)keys.nextElement(); |
4779 |
if (!this.optionNames.contains(key)) continue; // unrecognized option |
4851 |
int optionLevel = getOptionLevel(key); |
|
|
4852 |
if (optionLevel == UNKNOWN_OPTION) continue; // unrecognized option |
4780 |
if (key.equals(JavaCore.CORE_ENCODING)) { |
4853 |
if (key.equals(JavaCore.CORE_ENCODING)) { |
4781 |
if (cachedValue != null) { |
4854 |
if (cachedValue != null) { |
4782 |
cachedValue.put(key, JavaCore.getEncoding()); |
4855 |
cachedValue.put(key, JavaCore.getEncoding()); |
4783 |
} |
4856 |
} |
4784 |
continue; // skipped, contributed by resource prefs |
4857 |
continue; // skipped, contributed by resource prefs |
4785 |
} |
4858 |
} |
4786 |
String value = (String)newOptions.get(key); |
4859 |
String value = (String) newOptions.get(key); |
4787 |
String defaultValue = defaultPreferences.get(key, null); |
4860 |
String defaultValue = defaultPreferences.get(key, null); |
|
|
4861 |
// Store value in preferences |
4788 |
if (defaultValue != null && defaultValue.equals(value)) { |
4862 |
if (defaultValue != null && defaultValue.equals(value)) { |
4789 |
instancePreferences.remove(key); |
4863 |
value = null; |
4790 |
} else { |
|
|
4791 |
instancePreferences.put(key, value); |
4792 |
} |
4864 |
} |
|
|
4865 |
storePreference(key, value, instancePreferences); |
4793 |
} |
4866 |
} |
4794 |
try { |
4867 |
try { |
4795 |
// persist options |
4868 |
// persist options |