diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/AbstractSynchronousStepExecution.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/AbstractSynchronousStepExecution.java index df5da8ab..bd5748d4 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/AbstractSynchronousStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/AbstractSynchronousStepExecution.java @@ -1,7 +1,6 @@ package org.jenkinsci.plugins.workflow.steps; import hudson.model.Executor; -import hudson.model.Result; import static hudson.model.Result.ABORTED; diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/ContextParameterModule.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/ContextParameterModule.java index ea63bd7e..aeeca4bc 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/ContextParameterModule.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/ContextParameterModule.java @@ -46,12 +46,12 @@ protected void configure() { public void hear(TypeLiteral type, TypeEncounter encounter) { for (Field f : type.getRawType().getDeclaredFields()) { if (f.isAnnotationPresent(StepContextParameter.class)) { - encounter.register(new FieldInjector(f)); + encounter.register(new FieldInjector<>(f)); } } for (Method m : type.getRawType().getDeclaredMethods()) { if (m.isAnnotationPresent(StepContextParameter.class)) { - encounter.register(new MethodInjector(m)); + encounter.register(new MethodInjector<>(m)); } } } @@ -76,9 +76,7 @@ public void injectMembers(T instance) { f.set(instance, context.get(f.getType())); } catch (IllegalAccessException e) { throw (Error) new IllegalAccessError(e.getMessage()).initCause(e); - } catch (InterruptedException e) { - throw new ProvisionException("Failed to set a context parameter", e); - } catch (IOException e) { + } catch (InterruptedException | IOException e) { throw new ProvisionException("Failed to set a context parameter", e); } } @@ -103,11 +101,7 @@ public void injectMembers(T instance) { m.invoke(instance, args); } catch (IllegalAccessException e) { throw (Error) new IllegalAccessError(e.getMessage()).initCause(e); - } catch (InvocationTargetException e) { - throw new ProvisionException("Failed to set a context parameter", e); - } catch (InterruptedException e) { - throw new ProvisionException("Failed to set a context parameter", e); - } catch (IOException e) { + } catch (InterruptedException | InvocationTargetException | IOException e) { throw new ProvisionException("Failed to set a context parameter", e); } } diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/DynamicContext.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/DynamicContext.java index 61a4d631..8fc228af 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/DynamicContext.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/DynamicContext.java @@ -42,7 +42,7 @@ public interface DynamicContext extends ExtensionPoint { /** * Restricted version of {@link StepContext} used only for delegation in {@link #get(Class, DelegatedContext)}. */ - public interface DelegatedContext { + interface DelegatedContext { /** * Look for objects of the same or another type defined in this context. diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java index b34d3aca..dc1f7a2f 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/MissingContextVariableException.java @@ -50,7 +50,7 @@ public Class getType() { * exclude them */ public @Nonnull List getProviders() { - List r = new ArrayList(); + List r = new ArrayList<>(); for (StepDescriptor sd : StepDescriptor.all()) { if (isIn(sd.getProvidedContext()) && !isIn(sd.getRequiredContext())) r.add(sd); diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java index dc8e8c34..af730ff0 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/StepExecution.java @@ -172,7 +172,7 @@ public boolean blocksRestart() { * @see StepExecutionIterator */ public static ListenableFuture applyAll(Function f) { - List> futures = new ArrayList>(); + List> futures = new ArrayList<>(); for (StepExecutionIterator i : StepExecutionIterator.all()) futures.add(i.apply(f)); return Futures.allAsList(futures); diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/SynchronousStepExecution.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/SynchronousStepExecution.java index 0f3767a8..7f0031cc 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/SynchronousStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/SynchronousStepExecution.java @@ -1,7 +1,6 @@ package org.jenkinsci.plugins.workflow.steps; import hudson.model.Executor; -import hudson.model.Result; import static hudson.model.Result.ABORTED; import javax.annotation.Nonnull; diff --git a/src/main/java/org/jenkinsci/plugins/workflow/structs/DescribableHelper.java b/src/main/java/org/jenkinsci/plugins/workflow/structs/DescribableHelper.java index 9c668b46..209924b2 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/structs/DescribableHelper.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/structs/DescribableHelper.java @@ -42,6 +42,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -97,7 +98,7 @@ public class DescribableHelper { */ public static T instantiate(Class clazz, Map arguments) throws Exception { String[] names = loadConstructorParamNames(clazz); - Constructor c = DescribableHelper.findConstructor(clazz, names.length); + Constructor c = DescribableHelper.findConstructor(clazz, names.length); Object[] args = buildArguments(clazz, arguments, c.getGenericParameterTypes(), names, true); T o = c.newInstance(args); injectSetters(o, arguments); @@ -112,7 +113,7 @@ public static T instantiate(Class clazz, Map argument */ public static Map uninstantiate(Object o) throws UnsupportedOperationException { Class clazz = o.getClass(); - Map r = new TreeMap(); + Map r = new TreeMap<>(); String[] names; try { names = loadConstructorParamNames(clazz); @@ -123,8 +124,8 @@ public static Map uninstantiate(Object o) throws UnsupportedOpera inspect(r, o, clazz, name); } r.values().removeAll(Collections.singleton(null)); - Map constructorOnlyDataBoundProps = new TreeMap(r); - List dataBoundSetters = new ArrayList(); + Map constructorOnlyDataBoundProps = new TreeMap<>(r); + List dataBoundSetters = new ArrayList<>(); for (Class c = clazz; c != null; c = c.getSuperclass()) { for (Field f : c.getDeclaredFields()) { if (f.isAnnotationPresent(DataBoundSetter.class)) { @@ -167,7 +168,7 @@ public static final class Schema { private final List mandatoryParameters; Schema(Class clazz) { - this(clazz, new Stack()); + this(clazz, new Stack<>()); } Schema(Class clazz, @Nonnull Stack tracker) { @@ -175,8 +176,8 @@ public static final class Schema { /*if(tracker == null){ tracker = new Stack(); }*/ - mandatoryParameters = new ArrayList(); - parameters = new TreeMap(); + mandatoryParameters = new ArrayList<>(); + parameters = new TreeMap<>(); String[] names = loadConstructorParamNames(clazz); Type[] types = findConstructor(clazz, names.length).getGenericParameterTypes(); for (int i = 0; i < names.length; i++) { @@ -253,7 +254,7 @@ public String getDisplayName() { for (Klass c = Klass.java(type); c != null; c = c.getSuperClass()) { URL u = c.getResource(parameter == null ? "help.html" : "help-" + parameter + ".html"); if (u != null) { - return IOUtils.toString(u, "UTF-8"); + return IOUtils.toString(u, StandardCharsets.UTF_8); } } return null; @@ -262,7 +263,7 @@ public String getDisplayName() { @Override public String toString() { StringBuilder b = new StringBuilder("("); boolean first = true; - Map params = new TreeMap(parameters()); + Map params = new TreeMap<>(parameters()); for (String param : mandatoryParameters()) { if (first) { first = false; @@ -300,7 +301,7 @@ public Type getActualType() { } static ParameterType of(Type type){ - return of(type, new Stack()); + return of(type, new Stack<>()); } private static ParameterType of(Type type, @Nonnull Stack tracker) { @@ -311,11 +312,11 @@ private static ParameterType of(Type type, @Nonnull Stack tracker) { return new AtomicType(c); } if (Enum.class.isAssignableFrom(c)) { - List constants = new ArrayList(); + List constants = new ArrayList<>(); for (Enum value : c.asSubclass(Enum.class).getEnumConstants()) { constants.add(value.name()); } - return new EnumType(c, constants.toArray(new String[constants.size()])); + return new EnumType(c, constants.toArray(new String[0])); } if (c == URL.class) { return new AtomicType(String.class); @@ -330,16 +331,13 @@ private static ParameterType of(Type type, @Nonnull Stack tracker) { return new HomogeneousObjectType(c); } else { // Definitely heterogeneous. - Map>> subtypesBySimpleName = new HashMap>>(); + Map>> subtypesBySimpleName = new HashMap<>(); for (Class subtype : subtypes) { String simpleName = subtype.getSimpleName(); - List> bySimpleName = subtypesBySimpleName.get(simpleName); - if (bySimpleName == null) { - subtypesBySimpleName.put(simpleName, bySimpleName = new ArrayList>()); - } + List> bySimpleName = subtypesBySimpleName.computeIfAbsent(simpleName, unused -> new ArrayList<>()); bySimpleName.add(subtype); } - Map types = new TreeMap(); + Map types = new TreeMap<>(); for (Map.Entry>> entry : subtypesBySimpleName.entrySet()) { if (entry.getValue().size() == 1) { // normal case: unambiguous via simple name try { @@ -522,7 +520,7 @@ private static void clearDefaultSetters(Class clazz, Map allDa LOG.log(Level.WARNING, "Cannot create control version of " + clazz + " using " + constructorOnlyDataBoundProps, x); return; } - Map fromControl = new HashMap(constructorOnlyDataBoundProps); + Map fromControl = new HashMap<>(constructorOnlyDataBoundProps); Iterator fields = dataBoundSetters.iterator(); while (fields.hasNext()) { String field = fields.next(); @@ -572,7 +570,7 @@ private static Object coerce(String context, Type type, @Nonnull Object o) throw if (type instanceof Class && Primitives.wrap((Class) type).isInstance(o)) { return o; } else if (o instanceof Map) { - Map m = new HashMap(); + Map m = new HashMap<>(); for (Map.Entry entry : ((Map) o).entrySet()) { m.put((String) entry.getKey(), entry.getValue()); } @@ -629,7 +627,7 @@ private static boolean acceptsList(Type type) { } private static List mapList(String context, Type type, List list) throws Exception { - List r = new ArrayList(); + List r = new ArrayList<>(); for (Object elt : list) { r.add(coerce(context, type, elt)); } @@ -691,7 +689,7 @@ private static void injectSetters(Object o, Map arguments) throws Exce } private static void inspect(Map r, Object o, Class clazz, String field) { - AtomicReference type = new AtomicReference(); + AtomicReference type = new AtomicReference<>(); Object value = inspect(o, clazz, field, type); try { String[] names = loadConstructorParamNames(clazz); @@ -717,14 +715,14 @@ private static Object uncoerce(Object o, Type type) { } else if ((type == Character.class || type == char.class) && o instanceof Character) { return ((Character) o).toString(); } else if (o instanceof Object[]) { - List list = new ArrayList(); + List list = new ArrayList<>(); Object[] array = (Object[]) o; for (Object elt : array) { list.add(uncoerce(elt, array.getClass().getComponentType())); } return list; } else if (o instanceof List && acceptsList(type)) { - List list = new ArrayList(); + List list = new ArrayList<>(); for (Object elt : (List) o) { list.add(uncoerce(elt, ((ParameterizedType) type).getActualTypeArguments()[0])); } @@ -777,7 +775,7 @@ private static Object inspect(Object o, Class clazz, String field, AtomicRefe } static Set> findSubtypes(Class supertype) { - Set> clazzes = new HashSet>(); + Set> clazzes = new HashSet<>(); for (Descriptor d : getDescriptorList()) { if (supertype.isAssignableFrom(d.clazz)) { clazzes.add(d.clazz); @@ -810,7 +808,7 @@ private static List getDescriptorList() { return j.getExtensionList(Descriptor.class); } else { // TODO should be part of ExtensionList.lookup in core, but here now for benefit of tests: - List> descriptors = new ArrayList>(); + List> descriptors = new ArrayList<>(); for (IndexItem item : Index.load(Extension.class, Object.class)) { try { Object o = item.instance(); diff --git a/src/test/java/org/jenkinsci/plugins/workflow/steps/AbstractStepImplTest.java b/src/test/java/org/jenkinsci/plugins/workflow/steps/AbstractStepImplTest.java index 7aada018..7bb5e87b 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/steps/AbstractStepImplTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/steps/AbstractStepImplTest.java @@ -99,7 +99,7 @@ public static class BogusStepExecution extends AbstractSynchronousStepExecution< Node n; @Override - protected Void run() throws Exception { + protected Void run() { assertSame(jenkins, Jenkins.getInstance()); assertSame(n, Jenkins.getInstance()); return null; diff --git a/src/test/java/org/jenkinsci/plugins/workflow/steps/GeneralNonBlockingStepExecutionTest.java b/src/test/java/org/jenkinsci/plugins/workflow/steps/GeneralNonBlockingStepExecutionTest.java index 2356da1b..e2ec8f03 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/steps/GeneralNonBlockingStepExecutionTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/steps/GeneralNonBlockingStepExecutionTest.java @@ -24,24 +24,27 @@ package org.jenkinsci.plugins.workflow.steps; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.empty; +import static org.junit.Assert.assertTrue; + import hudson.model.Result; import hudson.model.TaskListener; import java.util.Collections; import java.util.Set; import java.util.concurrent.Semaphore; import java.util.logging.Level; -import static org.hamcrest.Matchers.*; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution; import org.jenkinsci.plugins.workflow.cps.CpsStepContext; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep; -import org.junit.ClassRule; -import org.junit.Test; -import static org.junit.Assert.*; import org.junit.Before; +import org.junit.ClassRule; import org.junit.Rule; +import org.junit.Test; import org.jvnet.hudson.test.BuildWatcher; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; @@ -142,7 +145,7 @@ public class GeneralNonBlockingStepExecutionTest { public static final class SlowBlockStep extends Step { @DataBoundConstructor public SlowBlockStep() {} - @Override public StepExecution start(StepContext context) throws Exception { + @Override public StepExecution start(StepContext context) { return new Execution(context, this); } private static final class Execution extends GeneralNonBlockingStepExecution { diff --git a/src/test/java/org/jenkinsci/plugins/workflow/steps/SynchronousNonBlockingStepExecutionTest.java b/src/test/java/org/jenkinsci/plugins/workflow/steps/SynchronousNonBlockingStepExecutionTest.java index a6207be6..764aef5f 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/steps/SynchronousNonBlockingStepExecutionTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/steps/SynchronousNonBlockingStepExecutionTest.java @@ -5,7 +5,6 @@ import hudson.model.Run; import java.io.File; -import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.HashSet; @@ -15,7 +14,7 @@ import jenkins.model.Jenkins; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.cps.nodes.StepNode; +import org.jenkinsci.plugins.workflow.graph.StepNode; import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; import org.jenkinsci.plugins.workflow.graph.FlowNode; import org.jenkinsci.plugins.workflow.job.WorkflowJob; @@ -149,9 +148,9 @@ public void parallelTest() throws Exception { public static final class SynchronousNonBlockingStep extends Step implements Serializable { public static final class State { - private static final Map states = new HashMap(); + private static final Map states = new HashMap<>(); static synchronized State get() { - File home = Jenkins.getActiveInstance().getRootDir(); + File home = Jenkins.get().getRootDir(); State state = states.get(home); if (state == null) { state = new State(); @@ -175,11 +174,11 @@ public String getId() { } @Override - public StepExecution start(StepContext context) throws Exception { + public StepExecution start(StepContext context) { return new StepExecutionImpl(this, context); } - public static void waitForStart(String id, Run b) throws IOException, InterruptedException { + public static void waitForStart(String id, Run b) throws InterruptedException { State s = State.get(); synchronized (s) { while (!s.started.contains(id)) { @@ -267,14 +266,14 @@ public Set> getRequiredContext() { } public static final class Erroneous extends Step { @DataBoundConstructor public Erroneous() {} - @Override public StepExecution start(StepContext context) throws Exception { + @Override public StepExecution start(StepContext context) { return new Exec(context); } private static final class Exec extends SynchronousNonBlockingStepExecution { Exec(StepContext context) { super(context); } - @Override protected Void run() throws Exception { + @Override protected Void run() { throw new AssertionError("ought to fail"); } } @@ -300,14 +299,14 @@ private static final class Exec extends SynchronousNonBlockingStepExecution { Exec(StepContext context) { super(context); } - @Override protected Void run() throws Exception { + @Override protected Void run() { if (Thread.currentThread().getContextClassLoader() == null) { throw new AssertionError("Context class loader should not be null!"); }