From a1c8c83cf7fa58a1d0f4ca92b4e293601966d13e Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Fri, 15 Nov 2024 10:58:26 -0800 Subject: [PATCH] Register `OptionHandler`s through `META-INF/services/annotations` and Annotation Indexer rather than `META-INF/services` and Commons Discovery --- core/src/main/java/hudson/cli/CLICommand.java | 29 ++++++++----------- .../hudson/cli/declarative/CLIRegisterer.java | 1 - .../declarative/OptionHandlerExtension.java | 1 - .../handlers/AbstractItemOptionHandler.java | 6 ++-- .../AbstractProjectOptionHandler.java | 5 ++-- .../hudson/cli/handlers/JobOptionHandler.java | 5 ++-- .../cli/handlers/NodeOptionHandler.java | 4 +-- .../ParameterizedJobOptionHandler.java | 5 ++-- .../handlers/TopLevelItemOptionHandler.java | 5 ++-- .../cli/handlers/ViewOptionHandler.java | 4 +-- 10 files changed, 27 insertions(+), 38 deletions(-) diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index f2dc2f8b5fc3..ba7fd4250fdd 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -242,7 +242,6 @@ public int main(List args, Locale locale, InputStream stdin, PrintStream this.stdout = stdout; this.stderr = stderr; this.locale = locale; - registerOptionHandlers(); CmdLineParser p = getCmdLineParser(); // add options from the authenticator @@ -527,20 +526,6 @@ protected CLICommand createClone() { } } - /** - * Auto-discovers {@link OptionHandler}s and add them to the given command line parser. - */ - protected void registerOptionHandlers() { - try { - 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); - } - } catch (IOException e) { - throw new Error(e); - } - } - /** * Returns all the registered {@link CLICommand}s. */ @@ -577,11 +562,21 @@ public static CLICommand getCurrent() { static { // register option handlers that are defined - ClassLoaders cls = new ClassLoaders(); Jenkins j = Jenkins.getInstanceOrNull(); if (j != null) { // only when running on the controller - cls.put(j.getPluginManager().uberClassLoader); + // Register OptionHandlers through META-INF/services/annotations and Annotation Indexer + try { + 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); + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + // Register OptionHandlers through META-INF/services and Commons Discovery + ClassLoaders cls = new ClassLoaders(); + cls.put(j.getPluginManager().uberClassLoader); ResourceNameIterator servicesIter = new DiscoverServiceNames(cls).findResourceNames(OptionHandler.class.getName()); final ResourceClassIterator itr = diff --git a/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java b/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java index f5d050174940..76b2c2612c15 100644 --- a/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java +++ b/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java @@ -131,7 +131,6 @@ protected CmdLineParser getCmdLineParser() { private CmdLineParser bindMethod(List binders) { - registerOptionHandlers(); ParserProperties properties = ParserProperties.defaults().withAtSyntax(ALLOW_AT_SYNTAX); CmdLineParser parser = new CmdLineParser(null, properties); diff --git a/core/src/main/java/hudson/cli/declarative/OptionHandlerExtension.java b/core/src/main/java/hudson/cli/declarative/OptionHandlerExtension.java index f009038ed2ac..3972514bc46c 100644 --- a/core/src/main/java/hudson/cli/declarative/OptionHandlerExtension.java +++ b/core/src/main/java/hudson/cli/declarative/OptionHandlerExtension.java @@ -35,7 +35,6 @@ /** * {@link OptionHandler}s that should be auto-discovered. - * TODO is this actually necessary? {@code @MetaInfServices(OptionHandler.class)} seems to work as well. * @author Kohsuke Kawaguchi */ @Indexed diff --git a/core/src/main/java/hudson/cli/handlers/AbstractItemOptionHandler.java b/core/src/main/java/hudson/cli/handlers/AbstractItemOptionHandler.java index c99f3090799e..6919bd8a1ebc 100644 --- a/core/src/main/java/hudson/cli/handlers/AbstractItemOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/AbstractItemOptionHandler.java @@ -24,18 +24,18 @@ package hudson.cli.handlers; +import hudson.cli.declarative.OptionHandlerExtension; import hudson.model.AbstractItem; -import org.kohsuke.MetaInfServices; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; -import org.kohsuke.args4j.spi.OptionHandler; import org.kohsuke.args4j.spi.Setter; /** * Refers to an {@link AbstractItem} by name. * @since 1.538 */ -@MetaInfServices(OptionHandler.class) public class AbstractItemOptionHandler extends GenericItemOptionHandler { +@OptionHandlerExtension +public class AbstractItemOptionHandler extends GenericItemOptionHandler { public AbstractItemOptionHandler(CmdLineParser parser, OptionDef option, Setter setter) { super(parser, option, setter); diff --git a/core/src/main/java/hudson/cli/handlers/AbstractProjectOptionHandler.java b/core/src/main/java/hudson/cli/handlers/AbstractProjectOptionHandler.java index ab7402de65ae..0efd7fca54f8 100644 --- a/core/src/main/java/hudson/cli/handlers/AbstractProjectOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/AbstractProjectOptionHandler.java @@ -24,11 +24,10 @@ package hudson.cli.handlers; +import hudson.cli.declarative.OptionHandlerExtension; import hudson.model.AbstractProject; -import org.kohsuke.MetaInfServices; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; -import org.kohsuke.args4j.spi.OptionHandler; import org.kohsuke.args4j.spi.Setter; /** @@ -36,7 +35,7 @@ * * @author Kohsuke Kawaguchi */ -@MetaInfServices(OptionHandler.class) +@OptionHandlerExtension @SuppressWarnings("rawtypes") public class AbstractProjectOptionHandler extends GenericItemOptionHandler { public AbstractProjectOptionHandler(CmdLineParser parser, OptionDef option, Setter setter) { diff --git a/core/src/main/java/hudson/cli/handlers/JobOptionHandler.java b/core/src/main/java/hudson/cli/handlers/JobOptionHandler.java index 519293150341..e0141446cce1 100644 --- a/core/src/main/java/hudson/cli/handlers/JobOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/JobOptionHandler.java @@ -24,11 +24,10 @@ package hudson.cli.handlers; +import hudson.cli.declarative.OptionHandlerExtension; import hudson.model.Job; -import org.kohsuke.MetaInfServices; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; -import org.kohsuke.args4j.spi.OptionHandler; import org.kohsuke.args4j.spi.Setter; /** @@ -36,7 +35,7 @@ * * @author Kohsuke Kawaguchi */ -@MetaInfServices(OptionHandler.class) +@OptionHandlerExtension @SuppressWarnings("rawtypes") public class JobOptionHandler extends GenericItemOptionHandler { public JobOptionHandler(CmdLineParser parser, OptionDef option, Setter setter) { diff --git a/core/src/main/java/hudson/cli/handlers/NodeOptionHandler.java b/core/src/main/java/hudson/cli/handlers/NodeOptionHandler.java index 45bd0f4e679b..7e4d0367bd9e 100644 --- a/core/src/main/java/hudson/cli/handlers/NodeOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/NodeOptionHandler.java @@ -24,9 +24,9 @@ package hudson.cli.handlers; +import hudson.cli.declarative.OptionHandlerExtension; import hudson.model.Node; import jenkins.model.Jenkins; -import org.kohsuke.MetaInfServices; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; @@ -40,7 +40,7 @@ * @author ogondza * @since 1.526 */ -@MetaInfServices +@OptionHandlerExtension public class NodeOptionHandler extends OptionHandler { public NodeOptionHandler(CmdLineParser parser, OptionDef option, Setter setter) { diff --git a/core/src/main/java/hudson/cli/handlers/ParameterizedJobOptionHandler.java b/core/src/main/java/hudson/cli/handlers/ParameterizedJobOptionHandler.java index a37dbf1790f1..b75369070581 100644 --- a/core/src/main/java/hudson/cli/handlers/ParameterizedJobOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/ParameterizedJobOptionHandler.java @@ -24,20 +24,19 @@ package hudson.cli.handlers; +import hudson.cli.declarative.OptionHandlerExtension; import jenkins.model.ParameterizedJobMixIn; -import org.kohsuke.MetaInfServices; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; -import org.kohsuke.args4j.spi.OptionHandler; import org.kohsuke.args4j.spi.Setter; /** * Refer to {@link jenkins.model.ParameterizedJobMixIn.ParameterizedJob} by its name. */ @Restricted(DoNotUse.class) -@MetaInfServices(OptionHandler.class) +@OptionHandlerExtension @SuppressWarnings("rawtypes") public class ParameterizedJobOptionHandler extends GenericItemOptionHandler { diff --git a/core/src/main/java/hudson/cli/handlers/TopLevelItemOptionHandler.java b/core/src/main/java/hudson/cli/handlers/TopLevelItemOptionHandler.java index 97fdf4b8c5b6..93f1dd969ba1 100644 --- a/core/src/main/java/hudson/cli/handlers/TopLevelItemOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/TopLevelItemOptionHandler.java @@ -1,10 +1,9 @@ package hudson.cli.handlers; +import hudson.cli.declarative.OptionHandlerExtension; import hudson.model.TopLevelItem; -import org.kohsuke.MetaInfServices; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; -import org.kohsuke.args4j.spi.OptionHandler; import org.kohsuke.args4j.spi.Setter; /** @@ -12,7 +11,7 @@ * * @author Kohsuke Kawaguchi */ -@MetaInfServices(OptionHandler.class) +@OptionHandlerExtension public class TopLevelItemOptionHandler extends GenericItemOptionHandler { public TopLevelItemOptionHandler(CmdLineParser parser, OptionDef option, Setter setter) { super(parser, option, setter); diff --git a/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java b/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java index 65656c15e33c..50101b284908 100644 --- a/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java +++ b/core/src/main/java/hudson/cli/handlers/ViewOptionHandler.java @@ -25,11 +25,11 @@ package hudson.cli.handlers; import edu.umd.cs.findbugs.annotations.CheckForNull; +import hudson.cli.declarative.OptionHandlerExtension; import hudson.model.View; import hudson.model.ViewGroup; import java.util.StringTokenizer; import jenkins.model.Jenkins; -import org.kohsuke.MetaInfServices; import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.OptionDef; @@ -58,7 +58,7 @@ * @author ogondza * @since 1.538 */ -@MetaInfServices +@OptionHandlerExtension public class ViewOptionHandler extends OptionHandler { public ViewOptionHandler(CmdLineParser parser, OptionDef option, Setter setter) {