mbien commented on code in PR #5013: URL: https://github.com/apache/netbeans/pull/5013#discussion_r1034111025
########## java/java.editor.base/src/org/netbeans/modules/java/editor/base/semantic/SemanticHighlighterBase.java: ########## @@ -84,17 +90,78 @@ import org.netbeans.modules.parsing.spi.SchedulerEvent; import org.netbeans.modules.parsing.spi.TaskIndexingMode; import org.openide.filesystems.FileUtil; +import org.openide.util.NbPreferences; import org.openide.util.Pair; +import org.openide.util.WeakListeners; /** * * @author Jan Lahoda */ public abstract class SemanticHighlighterBase extends JavaParserResultTask { - + + public static final String JAVA_INLINE_HINT_PARAMETER_NAME = "javaInlineHintParameterName"; //NOI18N + public static final String JAVA_INLINE_HINT_CHAINED_TYPES = "javaInlineHintChainedTypes"; //NOI18N + public static final String JAVA_INLINE_HINT_VAR_TYPE = "javaInlineHintVarType"; //NOI18N + + private static final Map<String, Boolean> DEFAULT_VALUES; + + static { + Map<String, Boolean> defaultValuesBuilder = new HashMap<>(); + defaultValuesBuilder.put(JAVA_INLINE_HINT_PARAMETER_NAME, true); + defaultValuesBuilder.put(JAVA_INLINE_HINT_CHAINED_TYPES, false); + defaultValuesBuilder.put(JAVA_INLINE_HINT_VAR_TYPE, false); + DEFAULT_VALUES = Collections.unmodifiableMap(defaultValuesBuilder); + } + + private static final AtomicBoolean inited = new AtomicBoolean(); + private static Preferences preferences; + private static final PreferenceChangeListener preferencesTracker = new PreferenceChangeListener() { + @Override + public void preferenceChange(PreferenceChangeEvent evt) { + String settingName = evt == null ? null : evt.getKey(); + if (settingName == null || JAVA_INLINE_HINT_PARAMETER_NAME.equals(settingName)) { + javaInlineHintParameterName = preferences.getBoolean(JAVA_INLINE_HINT_PARAMETER_NAME, DEFAULT_VALUES.get(JAVA_INLINE_HINT_PARAMETER_NAME)); + } + if (settingName == null || JAVA_INLINE_HINT_CHAINED_TYPES.equals(settingName)) { + javaInlineHintChainedTypes = preferences.getBoolean(JAVA_INLINE_HINT_CHAINED_TYPES, DEFAULT_VALUES.get(JAVA_INLINE_HINT_CHAINED_TYPES)); + } + if (settingName == null || JAVA_INLINE_HINT_VAR_TYPE.equals(settingName)) { + javaInlineHintVarType = preferences.getBoolean(JAVA_INLINE_HINT_VAR_TYPE, DEFAULT_VALUES.get(JAVA_INLINE_HINT_VAR_TYPE)); + } + } + }; + + private static boolean javaInlineHintParameterName; + private static boolean javaInlineHintChainedTypes; + private static boolean javaInlineHintVarType; + + private static boolean isJavaInlineHintParameterName() { + lazyInit(); + return javaInlineHintParameterName; + } + + private static boolean isJavaInlineHintChainedTypes() { + lazyInit(); + return javaInlineHintChainedTypes; + } + + private static boolean isJavaInlineHintVarType() { + lazyInit(); + return javaInlineHintVarType; + } + + private static void lazyInit() { + if (inited.compareAndSet(false, true)) { + preferences = NbPreferences.root().node("/org/netbeans/modules/java/editor/InlineHints/default"); + preferences.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, preferencesTracker, preferences)); + preferencesTracker.preferenceChange(null); + } Review Comment: this isn't thread safe unfortunately. Since `inited` is set right away without waiting for the block to complete. A second thread could see everything uninitialized while the first is still working on it. This essentially needs to be a critical section while `inited` is converted back to a normal boolean and read and written inside it or: a trick I sometimes use in situations like this is to use a static initializer of a not exposed inner class and let the classloader do the work lazily. `get()` would give you the initialized Preferences singleton, `init()`/`get()` would both make sure the listener was called accordingly. ```java // inner class private class PreferencesHolder { private static final Preferences preferences; static { preferences = NbPreferences.root().node("/org/netbeans/modules/java/editor/InlineHints/default"); preferences.addPreferenceChangeListener(WeakListeners.create(PreferenceChangeListener.class, preferencesTracker, preferences)); preferencesTracker.preferenceChange(null); } private static void init(){} private static Preferences get() { return preferences; } } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org For additional commands, e-mail: notifications-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists