Skip to content

Commit

Permalink
Updating baseline and removing reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Oct 23, 2020
1 parent af19950 commit 666817e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 132 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.7</version>
<version>4.12</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -64,15 +64,15 @@
<properties>
<revision>2.23</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.249.1</jenkins.version>
<jenkins.version>2.263</jenkins.version>
<java.level>8</java.level>
<useBeta>true</useBeta>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.249.x</artifactId>
<artifactId>bom-2.249.x</artifactId> <!-- TODO https://github.com/jenkinsci/bom/pull/339 -->
<version>12</version>
<scope>import</scope>
<type>pom</type>
Expand Down
32 changes: 2 additions & 30 deletions src/main/java/org/jenkinsci/plugins/workflow/steps/CoreStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import hudson.model.TaskListener;
import hudson.tasks.Builder;
import hudson.tasks.Publisher;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -85,22 +83,7 @@ private static final class Execution extends SynchronousNonBlockingStepExecution
final Launcher launcher = ctx.get(Launcher.class);
final TaskListener listener = Objects.requireNonNull(ctx.get(TaskListener.class));
final EnvVars env = Objects.requireNonNull(ctx.get(EnvVars.class));
boolean workspaceRequired = true;
// In Jenkins 2.258, a SimpleBuildStep can indicate that it does not require a workspace context by
// overriding a requiresWorkspace() method to return false. So use that if it's available.
// Note: this uses getMethod() on the delegate's type and not SimpleBuildStep so that an implementation can
// get this behaviour without switching to Jenkins 2.258 itself.
// TODO: Use 'workspaceRequired = this.delegate.requiresWorkspace()' once this plugin depends on Jenkins 2.258 or later.
try {
final Method requiresWorkspace = this.delegate.getClass().getMethod("requiresWorkspace");
workspaceRequired = (boolean) requiresWorkspace.invoke(this.delegate);
} catch(NoSuchMethodException e) {
// ok, default to true
} catch (InvocationTargetException ite) {
final Throwable realException = ite.getCause();
throw realException instanceof Exception ? (Exception) realException : ite;
}
if (workspaceRequired) {
if (delegate.requiresWorkspace()) {
if (workspace == null) {
throw new MissingContextVariableException(FilePath.class);
}
Expand All @@ -115,18 +98,7 @@ private static final class Execution extends SynchronousNonBlockingStepExecution
if (workspace != null && launcher != null) {
delegate.perform(run, workspace, env, launcher, listener);
} else {
// If we get here, workspaceRequired is false and there is no workspace context. In that case, the
// overload of perform() introduced in Jenkins 2.258 MUST exist.
// Note: this uses getMethod() on the delegate's type and not SimpleBuildStep so that an implementation
// can get this behaviour without switching to Jenkins 2.258 itself.
// TODO: Use 'this.delegate.perform(run, env, listener)' once the minimum core version for this plugin is 2.258 or newer.
final Method perform = this.delegate.getClass().getMethod("perform", Run.class, EnvVars.class, TaskListener.class);
try {
perform.invoke(this.delegate, run, env, listener);
} catch (InvocationTargetException ite) {
final Throwable realException = ite.getCause();
throw realException instanceof Exception ? (Exception) realException : ite;
}
delegate.perform(run, env, listener);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import hudson.model.TaskListener;
import hudson.tasks.BuildWrapperDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -94,20 +92,7 @@ private static final class Execution2 extends GeneralNonBlockingStepExecution {
}

private void doStart() throws Exception {
SimpleBuildWrapper.Context c = null;
// In Jenkins 2.258, a createContext() method on SimpleBuildWrapper is required to ensure that a Disposer
// registered on that context inherits the wrapper's workspace requirement. Use it when available.
// TODO: Use 'c = this.delegate.createContext()' once this plugin depends on Jenkins 2.258 or later.
try {
final Method createContext = SimpleBuildWrapper.class.getMethod("createContext");
c = (SimpleBuildWrapper.Context) createContext.invoke(this.delegate);
}
catch (NoSuchMethodException e) {
c = new SimpleBuildWrapper.Context();
} catch (InvocationTargetException ite) {
final Throwable realException = ite.getCause();
throw realException instanceof Exception ? (Exception) realException : ite;
}
SimpleBuildWrapper.Context c = delegate.createContext();
final StepContext context = getContext();
final Run<?, ?> run = context.get(Run.class);
assert run != null;
Expand All @@ -118,22 +103,7 @@ private void doStart() throws Exception {
assert env != null;
final FilePath workspace = context.get(FilePath.class);
final Launcher launcher = context.get(Launcher.class);
boolean workspaceRequired = true;
// In Jenkins 2.258, a SimpleBuildWrapper can indicate that it does not require a workspace context by
// overriding a requiresWorkspace() method to return false. So use that if it's available.
// Note: this uses getMethod() on the delegate's type and not SimpleBuildWrapper so that an
// implementation can get this behaviour without switching to Jenkins 2.258 itself.
// TODO: Use 'workspaceRequired = this.delegate.requiresWorkspace()' once this plugin depends on Jenkins 2.258 or later.
try {
final Method requiresWorkspace = this.delegate.getClass().getMethod("requiresWorkspace");
workspaceRequired = (boolean) requiresWorkspace.invoke(this.delegate);
} catch(NoSuchMethodException e) {
// ok, default to true
} catch (InvocationTargetException ite) {
final Throwable realException = ite.getCause();
throw realException instanceof Exception ? (Exception) realException : ite;
}
if (workspaceRequired) {
if (delegate.requiresWorkspace()) {
if (workspace == null) {
throw new MissingContextVariableException(FilePath.class);
}
Expand All @@ -147,16 +117,7 @@ private void doStart() throws Exception {
} else {
// If we get here, workspaceRequired is false and there is no workspace context. In that case, the
// overload of setUp() introduced in Jenkins 2.258 MUST exist.
// Note: this uses getMethod() on the delegate's type and not SimpleBuildWrapper so that an
// implementation can get this behaviour without switching to Jenkins 2.258 itself.
// TODO: Use 'this.delegate.setUp(c, run, listener, env)' once the minimum core version for this plugin is 2.258 or newer.
final Method perform = this.delegate.getClass().getMethod("setUp", SimpleBuildWrapper.Context.class, Run.class, TaskListener.class, EnvVars.class);
try {
perform.invoke(this.delegate, c, run, listener, env);
} catch (InvocationTargetException ite) {
final Throwable realException = ite.getCause();
throw realException instanceof Exception ? (Exception) realException : ite;
}
delegate.setUp(c, run, listener, env);
}
}
BodyInvoker bodyInvoker = context.newBodyInvoker();
Expand Down Expand Up @@ -219,23 +180,7 @@ private static final class Callback extends BodyExecutionCallback.TailCall {
assert listener != null;
final FilePath workspace = context.get(FilePath.class);
final Launcher launcher = context.get(Launcher.class);
boolean workspaceRequired = true;
// In Jenkins 2.258, a Disposer has a final requiresWorkspace() method that indicates whether or not it (or
// more accurately, its associated wrapper) requires a workspace context. This is set up via the Context, as
// long as that is created via SimpleBuildWrapper.createContext().
// Note: this uses getMethod() on the disposer's type and not SimpleBuildWrapper.Disposer so that an
// implementation can get this behaviour without switching to Jenkins 2.258 itself.
// TODO: Use 'workspaceRequired = this.disposer.requiresWorkspace()' once this plugin depends on Jenkins 2.258 or later.
try {
final Method requiresWorkspace = this.disposer.getClass().getMethod("requiresWorkspace");
workspaceRequired = (boolean) requiresWorkspace.invoke(this.disposer);
} catch(NoSuchMethodException e) {
// ok, default to true
} catch (InvocationTargetException ite) {
final Throwable realException = ite.getCause();
throw realException instanceof Exception ? (Exception) realException : ite;
}
if (workspaceRequired) {
if (disposer.requiresWorkspace()) {
if (workspace == null) {
throw new MissingContextVariableException(FilePath.class);
}
Expand All @@ -247,18 +192,7 @@ private static final class Callback extends BodyExecutionCallback.TailCall {
if (workspace != null && launcher != null) {
this.disposer.tearDown(run, workspace, launcher, listener);
} else {
// If we get here, workspaceRequired is false and there is no workspace context. In that case, the
// overload of tearDown() introduced in Jenkins 2.258 MUST exist.
// Note: this uses getMethod() on the disposer's type and not SimpleBuildWrapper.Disposer so that an
// implementation can get this behaviour without switching to Jenkins 2.258 itself.
// TODO: Use 'this.disposer.tearDown(run, listener)' once the minimum core version for this plugin is 2.258 or newer.
final Method perform = this.disposer.getClass().getMethod("tearDown", Run.class, TaskListener.class);
try {
perform.invoke(this.disposer, run, listener);
} catch (InvocationTargetException ite) {
final Throwable realException = ite.getCause();
throw realException instanceof Exception ? (Exception) realException : ite;
}
disposer.tearDown(run, listener);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,10 @@ public static class BuilderWithWorkspaceRequirement extends Builder implements S
@DataBoundConstructor
public BuilderWithWorkspaceRequirement() {
}
// TODO: Remove once the minimum core version for this plugin is 2.258 or newer.
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
fail("This method should not get called.");
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void perform(@Nonnull Run<?, ?> run, @Nonnull EnvVars env, @Nonnull TaskListener listener) throws InterruptedException, IOException {
@Override public void perform(@Nonnull Run<?, ?> run, @Nonnull EnvVars env, @Nonnull TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println("workspace context required, but not provided!");
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull EnvVars env, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
@Override public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull EnvVars env, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println("workspace context required and provided.");
}
// While the @TextExtension supposedly limits this descriptor to the named test method, it still gets picked up
Expand Down Expand Up @@ -247,20 +241,13 @@ public static class BuilderWithoutWorkspaceRequirement extends Builder implement
@DataBoundConstructor
public BuilderWithoutWorkspaceRequirement() {
}
// TODO: Remove once the minimum core version for this plugin is 2.258 or newer.
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
fail("This method should not get called.");
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void perform(@Nonnull Run<?, ?> run, @Nonnull EnvVars env, @Nonnull TaskListener listener) throws InterruptedException, IOException {
@Override public void perform(@Nonnull Run<?, ?> run, @Nonnull EnvVars env, @Nonnull TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println("workspace context not needed.");
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull EnvVars env, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
@Override public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull EnvVars env, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println("workspace context not needed, but provided.");
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public boolean requiresWorkspace() {
@Override public boolean requiresWorkspace() {
return false;
}
// While the @TextExtension supposedly limits this descriptor to the named test method, it still gets picked up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,15 @@ public static final class WrapperWithWorkspaceRequirement extends SimpleBuildWra
listener.getLogger().println(">>> workspace context required and provided.");
context.setDisposer(new DisposerWithWorkspaceRequirement());
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void setUp(@Nonnull Context context, @Nonnull Run<?, ?> build, @Nonnull TaskListener listener, @Nonnull EnvVars initialEnvironment) throws IOException, InterruptedException {
@Override public void setUp(@Nonnull Context context, @Nonnull Run<?, ?> build, @Nonnull TaskListener listener, @Nonnull EnvVars initialEnvironment) throws IOException, InterruptedException {
listener.getLogger().println(">>> workspace context required but not provided!");
context.setDisposer(new DisposerWithWorkspaceRequirement());
}
public static final class DisposerWithWorkspaceRequirement extends Disposer {
@Override public void tearDown(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException {
listener.getLogger().println("<<< workspace context required and provided.");
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void tearDown(@Nonnull Run<?, ?> build, @Nonnull TaskListener listener) throws IOException, InterruptedException {
@Override public void tearDown(@Nonnull Run<?, ?> build, @Nonnull TaskListener listener) throws IOException, InterruptedException {
listener.getLogger().println("<<< workspace context required but not provided!");
}
}
Expand Down Expand Up @@ -339,23 +337,20 @@ public void wrapperWithWorkspaceRequirement() throws Exception {

public static final class WrapperWithoutWorkspaceRequirement extends SimpleBuildWrapper {
@DataBoundConstructor public WrapperWithoutWorkspaceRequirement() { }
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public boolean requiresWorkspace() { return false; }
@Override public boolean requiresWorkspace() { return false; }
@Override public void setUp(@Nonnull Context context, @Nonnull Run<?, ?> build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener, @Nonnull EnvVars initialEnvironment) throws IOException, InterruptedException {
listener.getLogger().println(">>> workspace context not needed, but provided.");
context.setDisposer(new DisposerWithoutWorkspaceRequirement());
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void setUp(@Nonnull Context context, @Nonnull Run<?, ?> build, @Nonnull TaskListener listener, @Nonnull EnvVars initialEnvironment) throws IOException, InterruptedException {
@Override public void setUp(@Nonnull Context context, @Nonnull Run<?, ?> build, @Nonnull TaskListener listener, @Nonnull EnvVars initialEnvironment) throws IOException, InterruptedException {
listener.getLogger().println(">>> workspace context not needed.");
context.setDisposer(new DisposerWithoutWorkspaceRequirement());
}
public static final class DisposerWithoutWorkspaceRequirement extends Disposer {
@Override public void tearDown(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException {
listener.getLogger().println("<<< workspace context not needed, but provided.");
}
// TODO: Mark as @Override once the minimum core version for this plugin is 2.258 or newer.
public void tearDown(@Nonnull Run<?, ?> build, @Nonnull TaskListener listener) throws IOException, InterruptedException {
@Override public void tearDown(@Nonnull Run<?, ?> build, @Nonnull TaskListener listener) throws IOException, InterruptedException {
listener.getLogger().println("<<< workspace context not needed.");
}
}
Expand Down

0 comments on commit 666817e

Please sign in to comment.