Skip to content

Commit 9368197

Browse files
StefanSpiekeroleg-nenashev
authored andcommitted
Replaced integer with AtomicInteger to fix spotbugs issue (#4337)
* replaced integer with AtomicInteger to fix spotbugs issue * added @restricted to prevent further usage * replaced volatile with final because of AtomicInteger usage * implemented a getter and an increment method * fixed typeo in javadoc
1 parent 171c4d7 commit 9368197

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

core/src/main/java/hudson/scm/AutoBrowserHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public RepositoryBrowser get() {
6060
cacheGeneration = -1;
6161
return cache;
6262
}
63-
int g = d.generation;
63+
int g = d.getGeneration();
6464
if(g!=cacheGeneration) {
6565
cacheGeneration = g;
6666
cache = infer();

core/src/main/java/hudson/scm/SCMDescriptor.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@
2323
*/
2424
package hudson.scm;
2525

26+
import hudson.RestrictedSince;
2627
import hudson.Util;
2728
import hudson.model.AbstractProject;
2829
import hudson.model.Descriptor;
2930
import hudson.model.Job;
31+
import org.kohsuke.accmod.Restricted;
32+
import org.kohsuke.accmod.restrictions.NoExternalUse;
33+
3034
import java.lang.reflect.Field;
3135
import java.util.Collections;
3236
import java.util.List;
3337
import static java.util.logging.Level.WARNING;
38+
39+
import java.util.concurrent.atomic.AtomicInteger;
3440
import java.util.logging.Logger;
3541

3642
/**
@@ -48,15 +54,7 @@ public abstract class SCMDescriptor<T extends SCM> extends Descriptor<SCM> {
4854
*/
4955
public transient final Class<? extends RepositoryBrowser> repositoryBrowser;
5056

51-
/**
52-
* Incremented every time a new {@link SCM} instance is created from this descriptor.
53-
* This is used to invalidate cache of {@link SCM#getEffectiveBrowser}. Due to the lack of synchronization and serialization,
54-
* this field doesn't really count the # of instances created to date,
55-
* but it's good enough for the cache invalidation.
56-
* @deprecated No longer used by default.
57-
*/
58-
@Deprecated
59-
public volatile int generation = 1;
57+
private final AtomicInteger generation = new AtomicInteger(1);
6058

6159
protected SCMDescriptor(Class<T> clazz, Class<? extends RepositoryBrowser> repositoryBrowser) {
6260
super(clazz);
@@ -74,6 +72,29 @@ protected SCMDescriptor(Class<? extends RepositoryBrowser> repositoryBrowser) {
7472
this.repositoryBrowser = repositoryBrowser;
7573
}
7674

75+
/**
76+
* Incremented every time a new {@link SCM} instance is created from this descriptor.
77+
* This is used to invalidate cache of {@link SCM#getEffectiveBrowser}. Due to the lack of synchronization and serialization,
78+
* this field doesn't really count the # of instances created to date,
79+
* but it's good enough for the cache invalidation.
80+
* @deprecated No longer used by default.
81+
*/
82+
@Deprecated
83+
@Restricted(NoExternalUse.class) @RestrictedSince("TODO")
84+
public int getGeneration() {
85+
return generation.get();
86+
}
87+
88+
/**
89+
* Increments the generation value {@link SCMDescriptor#getGeneration} by one atomically.
90+
* @deprecated No longer used by default.
91+
*/
92+
@Deprecated
93+
@Restricted(NoExternalUse.class) @RestrictedSince("TODO")
94+
public void incrementGeneration() {
95+
generation.incrementAndGet();
96+
}
97+
7798
// work around HUDSON-4514. The repositoryBrowser field was marked as non-transient until 1.325,
7899
// causing the field to be persisted and overwritten on the load method.
79100
@SuppressWarnings({"ConstantConditions"})

core/src/main/java/hudson/scm/SCMS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static SCM parseSCM(StaplerRequest req, AbstractProject target) throws Fo
6060
if (scm == null) {
6161
scm = new NullSCM(); // JENKINS-36043 workaround for AbstractMultiBranchProject.submit
6262
}
63-
scm.getDescriptor().generation++;
63+
scm.getDescriptor().incrementGeneration();
6464
return scm;
6565
}
6666

0 commit comments

Comments
 (0)