From 76057897be22283603b483c53010bf30b42b63ad Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Thu, 3 Oct 2019 21:13:14 +0200 Subject: [PATCH 1/2] replaced deprecated getActiveInstance() with get() --- core/src/main/java/hudson/PluginManager.java | 8 ++++---- core/src/main/java/hudson/PluginWrapper.java | 2 +- core/src/main/java/hudson/cli/CLIAction.java | 2 +- core/src/main/java/hudson/cli/CLICommand.java | 6 +++--- .../main/java/hudson/cli/CancelQuietDownCommand.java | 2 +- core/src/main/java/hudson/cli/ClearQueueCommand.java | 2 +- core/src/main/java/hudson/cli/ConnectNodeCommand.java | 2 +- core/src/main/java/hudson/cli/CopyJobCommand.java | 2 +- core/src/main/java/hudson/cli/CreateJobCommand.java | 2 +- core/src/main/java/hudson/cli/CreateNodeCommand.java | 2 +- core/src/main/java/hudson/cli/CreateViewCommand.java | 2 +- core/src/main/java/hudson/cli/DeleteJobCommand.java | 2 +- core/src/main/java/hudson/cli/DeleteNodeCommand.java | 2 +- .../main/java/hudson/cli/DisconnectNodeCommand.java | 2 +- core/src/main/java/hudson/cli/GroovyCommand.java | 4 ++-- core/src/main/java/hudson/cli/GroovyshCommand.java | 8 ++++---- core/src/main/java/hudson/cli/HelpCommand.java | 2 +- core/src/main/java/hudson/cli/ListJobsCommand.java | 2 +- core/src/main/java/hudson/cli/OnlineNodeCommand.java | 2 +- core/src/main/java/hudson/cli/QuietDownCommand.java | 2 +- core/src/main/java/hudson/cli/ReloadJobCommand.java | 2 +- .../java/hudson/cli/handlers/ViewOptionHandler.java | 2 +- .../src/main/java/hudson/model/AsyncAperiodicWork.java | 2 +- core/src/main/java/hudson/model/AsyncPeriodicWork.java | 2 +- core/src/main/java/hudson/model/Computer.java | 2 +- core/src/main/java/hudson/model/Descriptor.java | 4 ++-- core/src/main/java/hudson/model/DownloadService.java | 4 ++-- core/src/main/java/hudson/model/Job.java | 2 +- .../main/java/hudson/model/OverallLoadStatistics.java | 2 +- core/src/main/java/jenkins/model/Jenkins.java | 2 +- .../java/jenkins/model/UnlabeledLoadStatistics.java | 2 +- .../java/jenkins/util/groovy/GroovyHookScript.java | 2 +- core/src/test/java/hudson/cli/ListJobsCommandTest.java | 1 - .../hudson/cli/handlers/ViewOptionHandlerTest.java | 1 - .../java/hudson/cli/CancelQuietDownCommandTest.java | 4 ++-- .../test/java/hudson/cli/ConnectNodeCommandTest.java | 2 +- .../java/hudson/cli/DisconnectNodeCommandTest.java | 2 +- .../test/java/hudson/cli/OfflineNodeCommandTest.java | 4 ++-- .../test/java/hudson/cli/OnlineNodeCommandTest.java | 2 +- .../src/test/java/hudson/cli/QuietDownCommandTest.java | 10 +++++----- .../test/java/hudson/model/UsageStatisticsTest.java | 4 ++-- 41 files changed, 57 insertions(+), 59 deletions(-) diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java index 281b005336..928953ad7d 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -633,7 +633,7 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas protected @Nonnull Set loadPluginsFromWar(@Nonnull String fromPath, @CheckForNull FilenameFilter filter) { Set names = new HashSet(); - ServletContext context = Jenkins.getActiveInstance().servletContext; + ServletContext context = Jenkins.get().servletContext; Set plugins = Util.fixNull(context.getResourcePaths(fromPath)); Set copiedPlugins = new HashSet<>(); Set dependencies = new HashSet<>(); @@ -697,7 +697,7 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas String dependencySpec = manifest.getMainAttributes().getValue("Plugin-Dependencies"); if (dependencySpec != null) { String[] dependencyTokens = dependencySpec.split(","); - ServletContext context = Jenkins.getActiveInstance().servletContext; + ServletContext context = Jenkins.get().servletContext; for (String dependencyToken : dependencyTokens) { if (dependencyToken.endsWith(";resolution:=optional")) { @@ -709,7 +709,7 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas String artifactId = artifactIdVersionPair[0]; VersionNumber dependencyVersion = new VersionNumber(artifactIdVersionPair[1]); - PluginManager manager = Jenkins.getActiveInstance().getPluginManager(); + PluginManager manager = Jenkins.get().getPluginManager(); VersionNumber installedVersion = manager.getPluginVersion(manager.rootDir, artifactId); if (installedVersion != null && !installedVersion.isOlderThan(dependencyVersion)) { // Do not downgrade dependencies that are already installed. @@ -1374,7 +1374,7 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas } response.add(pluginInfo); } - for (UpdateSite site : Jenkins.getActiveInstance().getUpdateCenter().getSiteList()) { + for (UpdateSite site : Jenkins.get().getUpdateCenter().getSiteList()) { for (UpdateSite.Plugin plugin: site.getAvailables()) { JSONObject pluginInfo = allPlugins.get(plugin.name); if(pluginInfo == null) { diff --git a/core/src/main/java/hudson/PluginWrapper.java b/core/src/main/java/hudson/PluginWrapper.java index 879dd3a486..529230abbc 100644 --- a/core/src/main/java/hudson/PluginWrapper.java +++ b/core/src/main/java/hudson/PluginWrapper.java @@ -1236,7 +1236,7 @@ public class PluginWrapper implements Comparable, ModelObject { @RequirePOST public HttpResponse doDoUninstall() throws IOException { - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); jenkins.checkPermission(Jenkins.ADMINISTER); archive.delete(); diff --git a/core/src/main/java/hudson/cli/CLIAction.java b/core/src/main/java/hudson/cli/CLIAction.java index b4116f3b58..c3858b6dd1 100644 --- a/core/src/main/java/hudson/cli/CLIAction.java +++ b/core/src/main/java/hudson/cli/CLIAction.java @@ -85,7 +85,7 @@ public class CLIAction implements UnprotectedRootAction, StaplerProxy { } public void doCommand(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException { - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); jenkins.checkPermission(Jenkins.READ); // Strip trailing slash diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index 15d496be1b..22237a7e47 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -242,10 +242,10 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { sc.setAuthentication(auth = getTransportAuthentication()); if (!(this instanceof HelpCommand || this instanceof WhoAmICommand)) - Jenkins.getActiveInstance().checkPermission(Jenkins.READ); + Jenkins.get().checkPermission(Jenkins.READ); p.parseArgument(args.toArray(new String[0])); if (!(this instanceof HelpCommand || this instanceof WhoAmICommand)) - Jenkins.getActiveInstance().checkPermission(Jenkins.READ); + Jenkins.get().checkPermission(Jenkins.READ); LOGGER.log(Level.FINE, "Invoking CLI command {0}, with {1} arguments, as user {2}.", new Object[] {getName(), args.size(), auth.getName()}); int res = run(); @@ -502,7 +502,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable { */ protected void registerOptionHandlers() { try { - for (Class c : Index.list(OptionHandlerExtension.class, Jenkins.getActiveInstance().pluginManager.uberClassLoader,Class.class)) { + for (Class c : Index.list(OptionHandlerExtension.class, Jenkins.get().pluginManager.uberClassLoader,Class.class)) { Type t = Types.getBaseClass(c, OptionHandler.class); CmdLineParser.registerHandler(Types.erasure(Types.getTypeArgument(t,0)), c); } diff --git a/core/src/main/java/hudson/cli/CancelQuietDownCommand.java b/core/src/main/java/hudson/cli/CancelQuietDownCommand.java index 24b2ec3018..a521de769b 100644 --- a/core/src/main/java/hudson/cli/CancelQuietDownCommand.java +++ b/core/src/main/java/hudson/cli/CancelQuietDownCommand.java @@ -47,7 +47,7 @@ public class CancelQuietDownCommand extends CLICommand { @Override protected int run() throws Exception { - Jenkins.getActiveInstance().doCancelQuietDown(); + Jenkins.get().doCancelQuietDown(); return 0; } } diff --git a/core/src/main/java/hudson/cli/ClearQueueCommand.java b/core/src/main/java/hudson/cli/ClearQueueCommand.java index 52cd7c4063..1a1b875bc5 100644 --- a/core/src/main/java/hudson/cli/ClearQueueCommand.java +++ b/core/src/main/java/hudson/cli/ClearQueueCommand.java @@ -47,7 +47,7 @@ public class ClearQueueCommand extends CLICommand { @Override protected int run() throws Exception { - Jenkins.getActiveInstance().getQueue().clear(); + Jenkins.get().getQueue().clear(); return 0; } diff --git a/core/src/main/java/hudson/cli/ConnectNodeCommand.java b/core/src/main/java/hudson/cli/ConnectNodeCommand.java index ce0fb64fef..eb5781fdd6 100644 --- a/core/src/main/java/hudson/cli/ConnectNodeCommand.java +++ b/core/src/main/java/hudson/cli/ConnectNodeCommand.java @@ -61,7 +61,7 @@ public class ConnectNodeCommand extends CLICommand { @Override protected int run() throws Exception { boolean errorOccurred = false; - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); final HashSet hs = new HashSet<>(nodes); diff --git a/core/src/main/java/hudson/cli/CopyJobCommand.java b/core/src/main/java/hudson/cli/CopyJobCommand.java index 3eb8049c1f..fd6b658f8f 100644 --- a/core/src/main/java/hudson/cli/CopyJobCommand.java +++ b/core/src/main/java/hudson/cli/CopyJobCommand.java @@ -50,7 +50,7 @@ public class CopyJobCommand extends CLICommand { public String dst; protected int run() throws Exception { - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); if (jenkins.getItemByFullName(dst)!=null) { throw new IllegalStateException("Job '"+dst+"' already exists"); diff --git a/core/src/main/java/hudson/cli/CreateJobCommand.java b/core/src/main/java/hudson/cli/CreateJobCommand.java index 0f0108c361..10ea5798fd 100644 --- a/core/src/main/java/hudson/cli/CreateJobCommand.java +++ b/core/src/main/java/hudson/cli/CreateJobCommand.java @@ -45,7 +45,7 @@ public class CreateJobCommand extends CLICommand { public String name; protected int run() throws Exception { - Jenkins h = Jenkins.getActiveInstance(); + Jenkins h = Jenkins.get(); if (h.getItemByFullName(name)!=null) { throw new IllegalStateException("Job '"+name+"' already exists"); diff --git a/core/src/main/java/hudson/cli/CreateNodeCommand.java b/core/src/main/java/hudson/cli/CreateNodeCommand.java index edab3f7e71..a1a826618e 100644 --- a/core/src/main/java/hudson/cli/CreateNodeCommand.java +++ b/core/src/main/java/hudson/cli/CreateNodeCommand.java @@ -52,7 +52,7 @@ public class CreateNodeCommand extends CLICommand { @Override protected int run() throws Exception { - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); jenkins.checkPermission(Computer.CREATE); final Node newNode = (Node) Jenkins.XSTREAM2.fromXML(stdin); diff --git a/core/src/main/java/hudson/cli/CreateViewCommand.java b/core/src/main/java/hudson/cli/CreateViewCommand.java index d22a0b424f..dea72441fa 100644 --- a/core/src/main/java/hudson/cli/CreateViewCommand.java +++ b/core/src/main/java/hudson/cli/CreateViewCommand.java @@ -49,7 +49,7 @@ public class CreateViewCommand extends CLICommand { @Override protected int run() throws Exception { - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); jenkins.checkPermission(View.CREATE); View newView; diff --git a/core/src/main/java/hudson/cli/DeleteJobCommand.java b/core/src/main/java/hudson/cli/DeleteJobCommand.java index 375e158a87..d829340892 100644 --- a/core/src/main/java/hudson/cli/DeleteJobCommand.java +++ b/core/src/main/java/hudson/cli/DeleteJobCommand.java @@ -54,7 +54,7 @@ public class DeleteJobCommand extends CLICommand { protected int run() throws Exception { boolean errorOccurred = false; - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); final HashSet hs = new HashSet<>(jobs); diff --git a/core/src/main/java/hudson/cli/DeleteNodeCommand.java b/core/src/main/java/hudson/cli/DeleteNodeCommand.java index 76e5c29530..57aad43507 100644 --- a/core/src/main/java/hudson/cli/DeleteNodeCommand.java +++ b/core/src/main/java/hudson/cli/DeleteNodeCommand.java @@ -54,7 +54,7 @@ public class DeleteNodeCommand extends CLICommand { protected int run() throws Exception { boolean errorOccurred = false; - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); final HashSet hs = new HashSet<>(nodes); diff --git a/core/src/main/java/hudson/cli/DisconnectNodeCommand.java b/core/src/main/java/hudson/cli/DisconnectNodeCommand.java index 51a02f1afb..6135c932bc 100644 --- a/core/src/main/java/hudson/cli/DisconnectNodeCommand.java +++ b/core/src/main/java/hudson/cli/DisconnectNodeCommand.java @@ -59,7 +59,7 @@ public class DisconnectNodeCommand extends CLICommand { @Override protected int run() throws Exception { boolean errorOccurred = false; - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); final HashSet hs = new HashSet<>(nodes); diff --git a/core/src/main/java/hudson/cli/GroovyCommand.java b/core/src/main/java/hudson/cli/GroovyCommand.java index f87ba80abc..9a802bc5a1 100644 --- a/core/src/main/java/hudson/cli/GroovyCommand.java +++ b/core/src/main/java/hudson/cli/GroovyCommand.java @@ -59,7 +59,7 @@ public class GroovyCommand extends CLICommand { protected int run() throws Exception { // this allows the caller to manipulate the JVM state, so require the execute script privilege. - Jenkins.getActiveInstance().checkPermission(Jenkins.RUN_SCRIPTS); + Jenkins.get().checkPermission(Jenkins.RUN_SCRIPTS); Binding binding = new Binding(); binding.setProperty("out",new PrintWriter(stdout,true)); @@ -67,7 +67,7 @@ public class GroovyCommand extends CLICommand { binding.setProperty("stdout",stdout); binding.setProperty("stderr",stderr); - GroovyShell groovy = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, binding); + GroovyShell groovy = new GroovyShell(Jenkins.get().getPluginManager().uberClassLoader, binding); groovy.run(loadScript(),"RemoteClass",remaining.toArray(new String[0])); return 0; } diff --git a/core/src/main/java/hudson/cli/GroovyshCommand.java b/core/src/main/java/hudson/cli/GroovyshCommand.java index 1048bc53d7..dd2584ebf0 100644 --- a/core/src/main/java/hudson/cli/GroovyshCommand.java +++ b/core/src/main/java/hudson/cli/GroovyshCommand.java @@ -60,7 +60,7 @@ public class GroovyshCommand extends CLICommand { @Override protected int run() { // this allows the caller to manipulate the JVM state, so require the admin privilege. - Jenkins.getActiveInstance().checkPermission(Jenkins.RUN_SCRIPTS); + Jenkins.get().checkPermission(Jenkins.RUN_SCRIPTS); // this being remote means no jline capability is available System.setProperty("jline.terminal", UnsupportedTerminal.class.getName()); @@ -85,12 +85,12 @@ public class GroovyshCommand extends CLICommand { Binding binding = new Binding(); // redirect "println" to the CLI binding.setProperty("out", new PrintWriter(stdout,true)); - binding.setProperty("hudson", Jenkins.getActiveInstance()); // backward compatibility - binding.setProperty("jenkins", Jenkins.getActiveInstance()); + binding.setProperty("hudson", Jenkins.get()); // backward compatibility + binding.setProperty("jenkins", Jenkins.get()); IO io = new IO(new BufferedInputStream(stdin),stdout,stderr); - final ClassLoader cl = Jenkins.getActiveInstance().pluginManager.uberClassLoader; + final ClassLoader cl = Jenkins.get().pluginManager.uberClassLoader; Closure registrar = new Closure(null, null) { private static final long serialVersionUID = 1L; diff --git a/core/src/main/java/hudson/cli/HelpCommand.java b/core/src/main/java/hudson/cli/HelpCommand.java index 071bce6a7f..a4623f5b8f 100644 --- a/core/src/main/java/hudson/cli/HelpCommand.java +++ b/core/src/main/java/hudson/cli/HelpCommand.java @@ -51,7 +51,7 @@ public class HelpCommand extends CLICommand { @Override protected int run() throws Exception { - if (!Jenkins.getActiveInstance().hasPermission(Jenkins.READ)) { + if (!Jenkins.get().hasPermission(Jenkins.READ)) { throw new AccessDeniedException("You must authenticate to access this Jenkins.\n" + CLI.usage()); } diff --git a/core/src/main/java/hudson/cli/ListJobsCommand.java b/core/src/main/java/hudson/cli/ListJobsCommand.java index 8dae08e674..dd7a1daf8f 100644 --- a/core/src/main/java/hudson/cli/ListJobsCommand.java +++ b/core/src/main/java/hudson/cli/ListJobsCommand.java @@ -49,7 +49,7 @@ public class ListJobsCommand extends CLICommand { public String name; protected int run() throws Exception { - Jenkins h = Jenkins.getActiveInstance(); + Jenkins h = Jenkins.get(); final Collection jobs; // If name is given retrieve jobs for the given view. diff --git a/core/src/main/java/hudson/cli/OnlineNodeCommand.java b/core/src/main/java/hudson/cli/OnlineNodeCommand.java index b3ec4fc0c9..095abddeb0 100644 --- a/core/src/main/java/hudson/cli/OnlineNodeCommand.java +++ b/core/src/main/java/hudson/cli/OnlineNodeCommand.java @@ -55,7 +55,7 @@ public class OnlineNodeCommand extends CLICommand { @Override protected int run() throws Exception { boolean errorOccurred = false; - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); final HashSet hs = new HashSet<>(nodes); List names = null; diff --git a/core/src/main/java/hudson/cli/QuietDownCommand.java b/core/src/main/java/hudson/cli/QuietDownCommand.java index 4d84055c8d..740ee10864 100644 --- a/core/src/main/java/hudson/cli/QuietDownCommand.java +++ b/core/src/main/java/hudson/cli/QuietDownCommand.java @@ -54,7 +54,7 @@ public class QuietDownCommand extends CLICommand { @Override protected int run() throws Exception { - Jenkins.getActiveInstance().doQuietDown(block, timeout); + Jenkins.get().doQuietDown(block, timeout); return 0; } } diff --git a/core/src/main/java/hudson/cli/ReloadJobCommand.java b/core/src/main/java/hudson/cli/ReloadJobCommand.java index 68a2bfda12..e8c51f9583 100644 --- a/core/src/main/java/hudson/cli/ReloadJobCommand.java +++ b/core/src/main/java/hudson/cli/ReloadJobCommand.java @@ -60,7 +60,7 @@ public class ReloadJobCommand extends CLICommand { protected int run() throws Exception { boolean errorOccurred = false; - final Jenkins jenkins = Jenkins.getActiveInstance(); + final Jenkins jenkins = Jenkins.get(); final HashSet hs = new HashSet<>(jobs); diff --git a/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java b/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java index 6107855812..2c901b06b4 100644 --- a/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java @@ -94,7 +94,7 @@ public class ViewOptionHandler extends OptionHandler { @CheckForNull public View getView(final String name) { - ViewGroup group = Jenkins.getActiveInstance(); + ViewGroup group = Jenkins.get(); View view = null; final StringTokenizer tok = new StringTokenizer(name, "/"); diff --git a/core/src/main/java/hudson/model/AsyncAperiodicWork.java b/core/src/main/java/hudson/model/AsyncAperiodicWork.java index 6e720c1419..194fd40a01 100644 --- a/core/src/main/java/hudson/model/AsyncAperiodicWork.java +++ b/core/src/main/java/hudson/model/AsyncAperiodicWork.java @@ -175,7 +175,7 @@ public abstract class AsyncAperiodicWork extends AperiodicWork { } else { lastRotateMillis = System.currentTimeMillis(); // migrate old log files the first time we start-up - File oldFile = new File(Jenkins.getActiveInstance().getRootDir(), f.getName()); + File oldFile = new File(Jenkins.get().getRootDir(), f.getName()); if (oldFile.isFile()) { File newFile = new File(f.getParentFile(), f.getName() + ".1"); if (!newFile.isFile()) { diff --git a/core/src/main/java/hudson/model/AsyncPeriodicWork.java b/core/src/main/java/hudson/model/AsyncPeriodicWork.java index 324d1853ee..889c33dc52 100644 --- a/core/src/main/java/hudson/model/AsyncPeriodicWork.java +++ b/core/src/main/java/hudson/model/AsyncPeriodicWork.java @@ -158,7 +158,7 @@ public abstract class AsyncPeriodicWork extends PeriodicWork { } else { lastRotateMillis = System.currentTimeMillis(); // migrate old log files the first time we start-up - File oldFile = new File(Jenkins.getActiveInstance().getRootDir(), f.getName()); + File oldFile = new File(Jenkins.get().getRootDir(), f.getName()); if (oldFile.isFile()) { File newFile = new File(f.getParentFile(), f.getName() + ".1"); if (!newFile.isFile()) { diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index 4291d95d45..0c7f198af4 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -1484,7 +1484,7 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces } if ((!proposedName.equals(nodeName)) - && Jenkins.getActiveInstance().getNode(proposedName) != null) { + && Jenkins.get().getNode(proposedName) != null) { throw new FormException(Messages.ComputerSet_SlaveAlreadyExists(proposedName), "name"); } diff --git a/core/src/main/java/hudson/model/Descriptor.java b/core/src/main/java/hudson/model/Descriptor.java index d282abbd80..2312ffbb9e 100644 --- a/core/src/main/java/hudson/model/Descriptor.java +++ b/core/src/main/java/hudson/model/Descriptor.java @@ -644,7 +644,7 @@ public abstract class Descriptor> implements Saveable, if (isApplicable(actualType, json)) { LOGGER.log(Level.FINE, "switching to newInstance {0} {1}", new Object[] {actualType.getName(), json}); try { - final Descriptor descriptor = Jenkins.getActiveInstance().getDescriptor(actualType); + final Descriptor descriptor = Jenkins.get().getDescriptor(actualType); if (descriptor != null) { return descriptor.newInstance(Stapler.getCurrentRequest(), json); } else { @@ -670,7 +670,7 @@ public abstract class Descriptor> implements Saveable, if (isApplicable(targetTypeErasure, json)) { LOGGER.log(Level.FINE, "switching to newInstance {0} {1}", new Object[] {targetTypeErasure.getName(), json}); try { - return Jenkins.getActiveInstance().getDescriptor(targetTypeErasure).newInstance(Stapler.getCurrentRequest(), json); + return Jenkins.get().getDescriptor(targetTypeErasure).newInstance(Stapler.getCurrentRequest(), json); } catch (Exception x) { LOGGER.log(Level.WARNING, "falling back to default instantiation " + targetTypeErasure.getName() + " " + json, x); } diff --git a/core/src/main/java/hudson/model/DownloadService.java b/core/src/main/java/hudson/model/DownloadService.java index c187dc0940..b5f5d022a7 100644 --- a/core/src/main/java/hudson/model/DownloadService.java +++ b/core/src/main/java/hudson/model/DownloadService.java @@ -310,7 +310,7 @@ public class DownloadService extends PageDecorator { */ public List getUrls() { List updateSites = new ArrayList<>(); - for (UpdateSite site : Jenkins.getActiveInstance().getUpdateCenter().getSiteList()) { + for (UpdateSite site : Jenkins.get().getUpdateCenter().getSiteList()) { String siteUrl = site.getUrl(); int baseUrlEnd = siteUrl.indexOf("update-center.json"); if (baseUrlEnd != -1) { @@ -398,7 +398,7 @@ public class DownloadService extends PageDecorator { public FormValidation updateNow() throws IOException { List jsonList = new ArrayList<>(); boolean toolInstallerMetadataExists = false; - for (UpdateSite updatesite : Jenkins.getActiveInstance().getUpdateCenter().getSiteList()) { + for (UpdateSite updatesite : Jenkins.get().getUpdateCenter().getSiteList()) { String site = updatesite.getMetadataUrlForDownloadable(url); if (site == null) { return FormValidation.warning("The update site " + site + " does not look like an update center"); diff --git a/core/src/main/java/hudson/model/Job.java b/core/src/main/java/hudson/model/Job.java index dc439794a8..d62f829f2d 100644 --- a/core/src/main/java/hudson/model/Job.java +++ b/core/src/main/java/hudson/model/Job.java @@ -547,7 +547,7 @@ public abstract class Job, RunT extends Run> getProperties() { Map result = Descriptor.toMap((Iterable) properties); if (logRotator != null) { - result.put(Jenkins.getActiveInstance().getDescriptorByType(BuildDiscarderProperty.DescriptorImpl.class), new BuildDiscarderProperty(logRotator)); + result.put(Jenkins.get().getDescriptorByType(BuildDiscarderProperty.DescriptorImpl.class), new BuildDiscarderProperty(logRotator)); } return result; } diff --git a/core/src/main/java/hudson/model/OverallLoadStatistics.java b/core/src/main/java/hudson/model/OverallLoadStatistics.java index 8c89201bc6..e87a82dc7b 100644 --- a/core/src/main/java/hudson/model/OverallLoadStatistics.java +++ b/core/src/main/java/hudson/model/OverallLoadStatistics.java @@ -72,7 +72,7 @@ public class OverallLoadStatistics extends LoadStatistics { @Override protected Iterable getNodes() { - return Jenkins.getActiveInstance().getNodes(); + return Jenkins.get().getNodes(); } @Override diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 06d6b7d5a4..98ee569120 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -5128,7 +5128,7 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve */ @Restricted(NoExternalUse.class) public @CheckForNull static VersionNumber getStoredVersion() { - return toVersion(Jenkins.getActiveInstance().version); + return toVersion(Jenkins.get().version); } /** diff --git a/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java b/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java index 39369d41ab..10bcce6652 100644 --- a/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java +++ b/core/src/main/java/jenkins/model/UnlabeledLoadStatistics.java @@ -102,7 +102,7 @@ public class UnlabeledLoadStatistics extends LoadStatistics { private static class UnlabeledNodesIterator extends Iterators.FilterIterator { protected UnlabeledNodesIterator() { - super(Jenkins.getActiveInstance().getNodes().iterator()); + super(Jenkins.get().getNodes().iterator()); } @Override diff --git a/core/src/main/java/jenkins/util/groovy/GroovyHookScript.java b/core/src/main/java/jenkins/util/groovy/GroovyHookScript.java index 4032541fe2..638992fc20 100644 --- a/core/src/main/java/jenkins/util/groovy/GroovyHookScript.java +++ b/core/src/main/java/jenkins/util/groovy/GroovyHookScript.java @@ -46,7 +46,7 @@ public class GroovyHookScript { @Deprecated public GroovyHookScript(String hook) { - this(hook, Jenkins.getActiveInstance()); + this(hook, Jenkins.get()); } private GroovyHookScript(String hook, Jenkins j) { diff --git a/core/src/test/java/hudson/cli/ListJobsCommandTest.java b/core/src/test/java/hudson/cli/ListJobsCommandTest.java index 7c4934ac72..a329e4f30a 100644 --- a/core/src/test/java/hudson/cli/ListJobsCommandTest.java +++ b/core/src/test/java/hudson/cli/ListJobsCommandTest.java @@ -50,7 +50,6 @@ public class ListJobsCommandTest { jenkins = mock(Jenkins.class); mockStatic(Jenkins.class); when(Jenkins.get()).thenReturn(jenkins); - when(Jenkins.getActiveInstance()).thenReturn(jenkins); command = mock(ListJobsCommand.class, Mockito.CALLS_REAL_METHODS); command.stdout = new PrintStream(stdout); command.stderr = new PrintStream(stderr); diff --git a/core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java b/core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java index 2fa4ecc732..1ae52440ed 100644 --- a/core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java +++ b/core/src/test/java/hudson/cli/handlers/ViewOptionHandlerTest.java @@ -84,7 +84,6 @@ public class ViewOptionHandlerTest { PowerMockito.mockStatic(Jenkins.class); PowerMockito.when(Jenkins.get()).thenReturn(jenkins); - PowerMockito.when(Jenkins.getActiveInstance()).thenReturn(jenkins); when(jenkins.getView("outer")).thenReturn(outer); when(jenkins.getDisplayName()).thenReturn("Jenkins"); when(jenkins.getACL()).thenReturn(new ACL() { diff --git a/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java b/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java index 38301b800c..f0a1a57cf0 100644 --- a/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java +++ b/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java @@ -80,7 +80,7 @@ public class CancelQuietDownCommandTest { @Test public void cancelQuietDownShouldSuccessOnQuietDownedJenkins() throws Exception { - j.jenkins.getActiveInstance().doQuietDown(); + j.jenkins.get().doQuietDown(); QuietDownCommandTest.assertJenkinsInQuietMode(j); final CLICommandInvoker.Result result = command .authorizedTo(Jenkins.READ, Jenkins.ADMINISTER) @@ -133,7 +133,7 @@ public class CancelQuietDownCommandTest { Future build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish); assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1)); assertThat(project.isBuilding(), equalTo(true)); - j.jenkins.getActiveInstance().doQuietDown(); + j.jenkins.get().doQuietDown(); QuietDownCommandTest.assertJenkinsInQuietMode(j); final CLICommandInvoker.Result result = command diff --git a/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java b/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java index 286999c210..2ff432373b 100644 --- a/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java +++ b/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java @@ -174,7 +174,7 @@ public class ConnectNodeCommandTest { } @Test public void connectNodeShouldSucceedOnMaster() throws Exception { - final Computer masterComputer = j.jenkins.getActiveInstance().getComputer(""); + final Computer masterComputer = j.jenkins.get().getComputer(""); CLICommandInvoker.Result result = command .authorizedTo(Computer.CONNECT, Jenkins.READ) diff --git a/test/src/test/java/hudson/cli/DisconnectNodeCommandTest.java b/test/src/test/java/hudson/cli/DisconnectNodeCommandTest.java index 534ef0f0b0..194f60b102 100644 --- a/test/src/test/java/hudson/cli/DisconnectNodeCommandTest.java +++ b/test/src/test/java/hudson/cli/DisconnectNodeCommandTest.java @@ -266,7 +266,7 @@ public class DisconnectNodeCommandTest { } public void disconnectNodeShouldSucceedOnMaster() throws Exception { - final Computer masterComputer = j.jenkins.getActiveInstance().getComputer(""); + final Computer masterComputer = j.jenkins.get().getComputer(""); assertThat(masterComputer.isOnline(), equalTo(true)); assertThat(masterComputer.getOfflineCause(), equalTo(null)); diff --git a/test/src/test/java/hudson/cli/OfflineNodeCommandTest.java b/test/src/test/java/hudson/cli/OfflineNodeCommandTest.java index 0dae4a5657..02c7f6d3bc 100644 --- a/test/src/test/java/hudson/cli/OfflineNodeCommandTest.java +++ b/test/src/test/java/hudson/cli/OfflineNodeCommandTest.java @@ -454,7 +454,7 @@ public class OfflineNodeCommandTest { @Test public void offlineNodeShouldSucceedOnMaster() throws Exception { - final Computer masterComputer = Jenkins.getActiveInstance().getComputer(""); + final Computer masterComputer = Jenkins.get().getComputer(""); final CLICommandInvoker.Result result = command .authorizedTo(Computer.DISCONNECT, Jenkins.READ) @@ -468,7 +468,7 @@ public class OfflineNodeCommandTest { @Test public void offlineNodeShouldSucceedOnMasterWithCause() throws Exception { - final Computer masterComputer = Jenkins.getActiveInstance().getComputer(""); + final Computer masterComputer = Jenkins.get().getComputer(""); final CLICommandInvoker.Result result = command .authorizedTo(Computer.DISCONNECT, Jenkins.READ) diff --git a/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java b/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java index 97c84a6e8f..fc9c1f604c 100644 --- a/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java +++ b/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java @@ -287,7 +287,7 @@ public class OnlineNodeCommandTest { } @Test public void onlineNodeShouldSucceedOnMaster() throws Exception { - final Computer masterComputer = j.jenkins.getActiveInstance().getComputer(""); + final Computer masterComputer = j.jenkins.get().getComputer(""); CLICommandInvoker.Result result = command .authorizedTo(Computer.CONNECT, Jenkins.READ) diff --git a/test/src/test/java/hudson/cli/QuietDownCommandTest.java b/test/src/test/java/hudson/cli/QuietDownCommandTest.java index c7837abc9d..70efe7e967 100644 --- a/test/src/test/java/hudson/cli/QuietDownCommandTest.java +++ b/test/src/test/java/hudson/cli/QuietDownCommandTest.java @@ -129,7 +129,7 @@ public class QuietDownCommandTest { @Test public void quietDownShouldSuccessOnAlreadyQuietDownedJenkins() throws Exception { - j.jenkins.getActiveInstance().doQuietDown(); + j.jenkins.get().doQuietDown(); assertJenkinsInQuietMode(); final CLICommandInvoker.Result result = command .authorizedTo(Jenkins.READ, Jenkins.ADMINISTER) @@ -140,7 +140,7 @@ public class QuietDownCommandTest { @Test public void quietDownShouldSuccessWithBlockOnAlreadyQuietDownedJenkins() throws Exception { - j.jenkins.getActiveInstance().doQuietDown(true, 0); + j.jenkins.get().doQuietDown(true, 0); assertJenkinsInQuietMode(); final CLICommandInvoker.Result result = command .authorizedTo(Jenkins.READ, Jenkins.ADMINISTER) @@ -151,7 +151,7 @@ public class QuietDownCommandTest { @Test public void quietDownShouldSuccessWithBlockAndTimeoutOnAlreadyQuietDownedJenkins() throws Exception { - j.jenkins.getActiveInstance().doQuietDown(true, 0); + j.jenkins.get().doQuietDown(true, 0); assertJenkinsInQuietMode(); final long time_before = System.currentTimeMillis(); final CLICommandInvoker.Result result = command @@ -484,7 +484,7 @@ public class QuietDownCommandTest { public static final void assertJenkinsInQuietMode(final JenkinsRule j) { await().pollInterval(250, TimeUnit.MILLISECONDS) .atMost(10, TimeUnit.SECONDS) - .until(() -> j.jenkins.getActiveInstance().getQueue().isBlockedByShutdown(task)); + .until(() -> j.jenkins.get().getQueue().isBlockedByShutdown(task)); } /** @@ -494,6 +494,6 @@ public class QuietDownCommandTest { public static final void assertJenkinsNotInQuietMode(final JenkinsRule j) { await().pollInterval(250, TimeUnit.MILLISECONDS) .atMost(10, TimeUnit.SECONDS) - .until(() -> !j.jenkins.getActiveInstance().getQueue().isBlockedByShutdown(task)); + .until(() -> !j.jenkins.get().getQueue().isBlockedByShutdown(task)); } } diff --git a/test/src/test/java/hudson/model/UsageStatisticsTest.java b/test/src/test/java/hudson/model/UsageStatisticsTest.java index 272698240a..10c71b62ed 100644 --- a/test/src/test/java/hudson/model/UsageStatisticsTest.java +++ b/test/src/test/java/hudson/model/UsageStatisticsTest.java @@ -90,7 +90,7 @@ public class UsageStatisticsTest { InputStreamReader r = new InputStreamReader(new GZIPInputStream( new CombinedCipherInputStream(new ByteArrayInputStream(cipherText),priv,"AES")), "UTF-8"); JSONObject o = JSONObject.fromObject(IOUtils.toString(r)); - Jenkins jenkins = Jenkins.getActiveInstance(); + Jenkins jenkins = Jenkins.get(); // A bit intrusive with UsageStatistics internals, but done to prevent undetected changes // that would cause issues with parsing/analyzing uploaded usage statistics assertEquals(1, o.getInt("stat")); @@ -136,7 +136,7 @@ public class UsageStatisticsTest { * @throws InterruptedException */ private void warmUpNodeMonitorCache() throws InterruptedException { - Jenkins j = Jenkins.getActiveInstance(); + Jenkins j = Jenkins.get(); ArchitectureMonitor.DescriptorImpl descriptor = j.getDescriptorByType(ArchitectureMonitor.DescriptorImpl.class); String value = null; int count = 1; From 5e275dbc31418caed3b0b5654e913aac212224d4 Mon Sep 17 00:00:00 2001 From: StefanSpieker Date: Sun, 13 Oct 2019 20:51:42 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Replaced instance calls with static calls as suggested by daniel-beck. Co-Authored-By: Daniel Beck <1831569+daniel-beck@users.noreply.github.com> --- .../test/java/hudson/cli/CancelQuietDownCommandTest.java | 6 +++--- test/src/test/java/hudson/cli/ConnectNodeCommandTest.java | 2 +- test/src/test/java/hudson/cli/OnlineNodeCommandTest.java | 2 +- test/src/test/java/hudson/cli/QuietDownCommandTest.java | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java b/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java index f0a1a57cf0..d05c0b1f6d 100644 --- a/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java +++ b/test/src/test/java/hudson/cli/CancelQuietDownCommandTest.java @@ -80,7 +80,7 @@ public class CancelQuietDownCommandTest { @Test public void cancelQuietDownShouldSuccessOnQuietDownedJenkins() throws Exception { - j.jenkins.get().doQuietDown(); + j.jenkins.doQuietDown(); QuietDownCommandTest.assertJenkinsInQuietMode(j); final CLICommandInvoker.Result result = command .authorizedTo(Jenkins.READ, Jenkins.ADMINISTER) @@ -133,7 +133,7 @@ public class CancelQuietDownCommandTest { Future build = OnlineNodeCommandTest.startBlockingAndFinishingBuild(project, finish); assertThat(((FreeStyleProject) j.jenkins.getItem("aProject")).getBuilds(), hasSize(1)); assertThat(project.isBuilding(), equalTo(true)); - j.jenkins.get().doQuietDown(); + j.jenkins.doQuietDown(); QuietDownCommandTest.assertJenkinsInQuietMode(j); final CLICommandInvoker.Result result = command @@ -157,4 +157,4 @@ public class CancelQuietDownCommandTest { j.assertBuildStatusSuccess(build); QuietDownCommandTest.assertJenkinsNotInQuietMode(j); } -} \ No newline at end of file +} diff --git a/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java b/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java index 2ff432373b..621fe2a183 100644 --- a/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java +++ b/test/src/test/java/hudson/cli/ConnectNodeCommandTest.java @@ -174,7 +174,7 @@ public class ConnectNodeCommandTest { } @Test public void connectNodeShouldSucceedOnMaster() throws Exception { - final Computer masterComputer = j.jenkins.get().getComputer(""); + final Computer masterComputer = j.jenkins.getComputer(""); CLICommandInvoker.Result result = command .authorizedTo(Computer.CONNECT, Jenkins.READ) diff --git a/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java b/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java index fc9c1f604c..5b31a40153 100644 --- a/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java +++ b/test/src/test/java/hudson/cli/OnlineNodeCommandTest.java @@ -287,7 +287,7 @@ public class OnlineNodeCommandTest { } @Test public void onlineNodeShouldSucceedOnMaster() throws Exception { - final Computer masterComputer = j.jenkins.get().getComputer(""); + final Computer masterComputer = j.jenkins.getComputer(""); CLICommandInvoker.Result result = command .authorizedTo(Computer.CONNECT, Jenkins.READ) diff --git a/test/src/test/java/hudson/cli/QuietDownCommandTest.java b/test/src/test/java/hudson/cli/QuietDownCommandTest.java index 38f577e41b..5d956c77ad 100644 --- a/test/src/test/java/hudson/cli/QuietDownCommandTest.java +++ b/test/src/test/java/hudson/cli/QuietDownCommandTest.java @@ -484,7 +484,7 @@ public class QuietDownCommandTest { public static final void assertJenkinsInQuietMode(final JenkinsRule j) { await().pollInterval(250, TimeUnit.MILLISECONDS) .atMost(10, TimeUnit.SECONDS) - .until(() -> j.jenkins.get().getQueue().isBlockedByShutdown(task)); + .until(() -> j.jenkins.getQueue().isBlockedByShutdown(task)); } /** @@ -494,6 +494,6 @@ public class QuietDownCommandTest { public static final void assertJenkinsNotInQuietMode(final JenkinsRule j) { await().pollInterval(250, TimeUnit.MILLISECONDS) .atMost(10, TimeUnit.SECONDS) - .until(() -> !j.jenkins.get().getQueue().isBlockedByShutdown(task)); + .until(() -> !j.jenkins.getQueue().isBlockedByShutdown(task)); } }