From a35f008dc807c0c502d8b84ac3420e76d0f1f45c Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Sat, 30 Apr 2022 17:39:18 +0200 Subject: [PATCH] Make read and write access to modified properties synchronized, as it is both access from background and foreground threads. Only synchronization assures that the changes are visible immediately. --- .../model/ConfigurationLocation.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/infernus/idea/checkstyle/model/ConfigurationLocation.java b/src/main/java/org/infernus/idea/checkstyle/model/ConfigurationLocation.java index 4db6e224..8db5a9a8 100644 --- a/src/main/java/org/infernus/idea/checkstyle/model/ConfigurationLocation.java +++ b/src/main/java/org/infernus/idea/checkstyle/model/ConfigurationLocation.java @@ -51,7 +51,7 @@ public abstract class ConfigurationLocation implements Cloneable, Comparable getNamedScope() { + public synchronized Optional getNamedScope() { return Optional.ofNullable(this.namedScope); } - public void setLocation(final String location) { + public synchronized void setLocation(final String location) { if (isBlank(location)) { throw new IllegalArgumentException("A non-blank location is required"); } @@ -116,11 +116,11 @@ public void setLocation(final String location) { this.propertiesCheckedThisSession = false; } - public String getDescription() { + public synchronized String getDescription() { return description; } - public void setDescription(@Nullable final String description) { + public synchronized void setDescription(@Nullable final String description) { if (description == null) { this.description = location; } else { @@ -128,15 +128,15 @@ public void setDescription(@Nullable final String description) { } } - public void setNamedScope(NamedScope namedScope) { + public synchronized void setNamedScope(NamedScope namedScope) { this.namedScope = namedScope; } - public Map getProperties() { + public synchronized Map getProperties() { return new HashMap<>(properties); } - public void setProperties(final Map newProperties) { + public synchronized void setProperties(final Map newProperties) { properties.clear(); if (newProperties == null) { @@ -148,11 +148,11 @@ public void setProperties(final Map newProperties) { this.propertiesCheckedThisSession = false; } - public boolean isRemovable() { + public synchronized boolean isRemovable() { return true; } - public void reset() { + public synchronized void reset() { propertiesCheckedThisSession = false; unblock(); } @@ -216,7 +216,7 @@ private void extractPropertyNames(final Element element, final List prop } @SuppressWarnings("EmptyTryBlock") - public void ensurePropertiesAreUpToDate(@NotNull final ClassLoader checkstyleClassLoader) throws IOException { + public synchronized void ensurePropertiesAreUpToDate(@NotNull final ClassLoader checkstyleClassLoader) throws IOException { if (!propertiesCheckedThisSession) { try (InputStream ignored = resolve(checkstyleClassLoader)) { // ignored @@ -224,7 +224,7 @@ public void ensurePropertiesAreUpToDate(@NotNull final ClassLoader checkstyleCla } } - public InputStream resolve(@NotNull final ClassLoader checkstyleClassLoader) throws IOException { + public synchronized InputStream resolve(@NotNull final ClassLoader checkstyleClassLoader) throws IOException { InputStream is = resolveFile(checkstyleClassLoader); if (!propertiesCheckedThisSession) { @@ -251,7 +251,7 @@ public InputStream resolve(@NotNull final ClassLoader checkstyleClassLoader) thr } @Nullable - public String resolveAssociatedFile(@Nullable final String filename, + public synchronized String resolveAssociatedFile(@Nullable final String filename, @Nullable final Module module, @NotNull final ClassLoader checkstyleClassLoader) throws IOException { if (filename == null) { @@ -357,7 +357,7 @@ private File checkModuleContentRoots(final Module module, final String fileName) return null; } - public final boolean hasChangedFrom(final ConfigurationLocation configurationLocation) { + public synchronized final boolean hasChangedFrom(final ConfigurationLocation configurationLocation) { return !equals(configurationLocation) || propertiesHaveChanged(configurationLocation); } @@ -369,7 +369,7 @@ private boolean propertiesHaveChanged(final ConfigurationLocation configurationL return !getProperties().equals(configurationLocation.getProperties()); } - public String getDescriptor() { + public synchronized String getDescriptor() { assert location != null; assert description != null; assert namedScope != null; @@ -474,19 +474,19 @@ private int compareStrings(@Nullable final String pStr1, @Nullable final String } - public boolean isBlocked() { - return blockedUtil > currentTimeMillis(); + public synchronized boolean isBlocked() { + return blockedUntil > currentTimeMillis(); } - public long blockedForSeconds() { - return Math.max((blockedUtil - currentTimeMillis()) / 1000, 0); + public synchronized long blockedForSeconds() { + return Math.max((blockedUntil - currentTimeMillis()) / 1000, 0); } - public void block() { - blockedUtil = currentTimeMillis() + BLOCK_TIME_MS; + public synchronized void block() { + blockedUntil = currentTimeMillis() + BLOCK_TIME_MS; } - public void unblock() { - blockedUtil = 0L; + public synchronized void unblock() { + blockedUntil = 0L; } }