Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous code cleanup #71

Merged
merged 10 commits into from
Dec 8, 2021
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jenkinsci.plugins.workflow.steps;

import hudson.model.Executor;
import hudson.model.Result;

import static hudson.model.Result.ABORTED;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ protected void configure() {
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
for (Field f : type.getRawType().getDeclaredFields()) {
if (f.isAnnotationPresent(StepContextParameter.class)) {
encounter.register(new FieldInjector<I>(f));
encounter.register(new FieldInjector<>(f));
}
}
for (Method m : type.getRawType().getDeclaredMethods()) {
if (m.isAnnotationPresent(StepContextParameter.class)) {
encounter.register(new MethodInjector<I>(m));
encounter.register(new MethodInjector<>(m));
}
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Class<?> getType() {
* exclude them
*/
public @Nonnull List<StepDescriptor> getProviders() {
List<StepDescriptor> r = new ArrayList<StepDescriptor>();
List<StepDescriptor> r = new ArrayList<>();
for (StepDescriptor sd : StepDescriptor.all()) {
if (isIn(sd.getProvidedContext()) && !isIn(sd.getRequiredContext()))
r.add(sd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public boolean blocksRestart() {
* @see StepExecutionIterator
*/
public static ListenableFuture<?> applyAll(Function<StepExecution,Void> f) {
List<ListenableFuture<?>> futures = new ArrayList<ListenableFuture<?>>();
List<ListenableFuture<?>> futures = new ArrayList<>();
for (StepExecutionIterator i : StepExecutionIterator.all())
futures.add(i.apply(f));
return Futures.allAsList(futures);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +98,7 @@ public class DescribableHelper {
*/
public static <T> T instantiate(Class<? extends T> clazz, Map<String,?> arguments) throws Exception {
String[] names = loadConstructorParamNames(clazz);
Constructor<T> c = DescribableHelper.<T>findConstructor(clazz, names.length);
Constructor<T> c = DescribableHelper.findConstructor(clazz, names.length);
Object[] args = buildArguments(clazz, arguments, c.getGenericParameterTypes(), names, true);
T o = c.newInstance(args);
injectSetters(o, arguments);
Expand All @@ -112,7 +113,7 @@ public static <T> T instantiate(Class<? extends T> clazz, Map<String,?> argument
*/
public static Map<String,Object> uninstantiate(Object o) throws UnsupportedOperationException {
Class<?> clazz = o.getClass();
Map<String, Object> r = new TreeMap<String, Object>();
Map<String, Object> r = new TreeMap<>();
String[] names;
try {
names = loadConstructorParamNames(clazz);
Expand All @@ -123,8 +124,8 @@ public static Map<String,Object> uninstantiate(Object o) throws UnsupportedOpera
inspect(r, o, clazz, name);
}
r.values().removeAll(Collections.singleton(null));
Map<String,Object> constructorOnlyDataBoundProps = new TreeMap<String,Object>(r);
List<String> dataBoundSetters = new ArrayList<String>();
Map<String,Object> constructorOnlyDataBoundProps = new TreeMap<>(r);
List<String> dataBoundSetters = new ArrayList<>();
for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
for (Field f : c.getDeclaredFields()) {
if (f.isAnnotationPresent(DataBoundSetter.class)) {
Expand Down Expand Up @@ -167,16 +168,16 @@ public static final class Schema {
private final List<String> mandatoryParameters;

Schema(Class<?> clazz) {
this(clazz, new Stack<String>());
this(clazz, new Stack<>());
}

Schema(Class<?> clazz, @Nonnull Stack<String> tracker) {
this.type = clazz;
/*if(tracker == null){
tracker = new Stack<String>();
}*/
mandatoryParameters = new ArrayList<String>();
parameters = new TreeMap<String,ParameterType>();
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++) {
Expand Down Expand Up @@ -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;
Expand All @@ -262,7 +263,7 @@ public String getDisplayName() {
@Override public String toString() {
StringBuilder b = new StringBuilder("(");
boolean first = true;
Map<String,ParameterType> params = new TreeMap<String,ParameterType>(parameters());
Map<String,ParameterType> params = new TreeMap<>(parameters());
for (String param : mandatoryParameters()) {
if (first) {
first = false;
Expand Down Expand Up @@ -300,7 +301,7 @@ public Type getActualType() {
}

static ParameterType of(Type type){
return of(type, new Stack<String>());
return of(type, new Stack<>());
}

private static ParameterType of(Type type, @Nonnull Stack<String> tracker) {
Expand All @@ -311,11 +312,11 @@ private static ParameterType of(Type type, @Nonnull Stack<String> tracker) {
return new AtomicType(c);
}
if (Enum.class.isAssignableFrom(c)) {
List<String> constants = new ArrayList<String>();
List<String> 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);
Expand All @@ -330,16 +331,13 @@ private static ParameterType of(Type type, @Nonnull Stack<String> tracker) {
return new HomogeneousObjectType(c);
} else {
// Definitely heterogeneous.
Map<String,List<Class<?>>> subtypesBySimpleName = new HashMap<String,List<Class<?>>>();
Map<String,List<Class<?>>> subtypesBySimpleName = new HashMap<>();
for (Class<?> subtype : subtypes) {
String simpleName = subtype.getSimpleName();
List<Class<?>> bySimpleName = subtypesBySimpleName.get(simpleName);
if (bySimpleName == null) {
subtypesBySimpleName.put(simpleName, bySimpleName = new ArrayList<Class<?>>());
}
List<Class<?>> bySimpleName = subtypesBySimpleName.computeIfAbsent(simpleName, unused -> new ArrayList<>());
bySimpleName.add(subtype);
}
Map<String,Schema> types = new TreeMap<String,Schema>();
Map<String,Schema> types = new TreeMap<>();
for (Map.Entry<String,List<Class<?>>> entry : subtypesBySimpleName.entrySet()) {
if (entry.getValue().size() == 1) { // normal case: unambiguous via simple name
try {
Expand Down Expand Up @@ -522,7 +520,7 @@ private static void clearDefaultSetters(Class<?> clazz, Map<String,Object> allDa
LOG.log(Level.WARNING, "Cannot create control version of " + clazz + " using " + constructorOnlyDataBoundProps, x);
return;
}
Map<String,Object> fromControl = new HashMap<String,Object>(constructorOnlyDataBoundProps);
Map<String,Object> fromControl = new HashMap<>(constructorOnlyDataBoundProps);
Iterator<String> fields = dataBoundSetters.iterator();
while (fields.hasNext()) {
String field = fields.next();
Expand Down Expand Up @@ -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<String,Object> m = new HashMap<String,Object>();
Map<String,Object> m = new HashMap<>();
for (Map.Entry<?,?> entry : ((Map<?,?>) o).entrySet()) {
m.put((String) entry.getKey(), entry.getValue());
}
Expand Down Expand Up @@ -629,7 +627,7 @@ private static boolean acceptsList(Type type) {
}

private static List<Object> mapList(String context, Type type, List<?> list) throws Exception {
List<Object> r = new ArrayList<Object>();
List<Object> r = new ArrayList<>();
for (Object elt : list) {
r.add(coerce(context, type, elt));
}
Expand Down Expand Up @@ -691,7 +689,7 @@ private static void injectSetters(Object o, Map<String,?> arguments) throws Exce
}

private static void inspect(Map<String, Object> r, Object o, Class<?> clazz, String field) {
AtomicReference<Type> type = new AtomicReference<Type>();
AtomicReference<Type> type = new AtomicReference<>();
Object value = inspect(o, clazz, field, type);
try {
String[] names = loadConstructorParamNames(clazz);
Expand All @@ -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<Object> list = new ArrayList<Object>();
List<Object> 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<Object> list = new ArrayList<Object>();
List<Object> list = new ArrayList<>();
for (Object elt : (List<?>) o) {
list.add(uncoerce(elt, ((ParameterizedType) type).getActualTypeArguments()[0]));
}
Expand Down Expand Up @@ -777,7 +775,7 @@ private static Object inspect(Object o, Class<?> clazz, String field, AtomicRefe
}

static Set<Class<?>> findSubtypes(Class<?> supertype) {
Set<Class<?>> clazzes = new HashSet<Class<?>>();
Set<Class<?>> clazzes = new HashSet<>();
for (Descriptor<?> d : getDescriptorList()) {
if (supertype.isAssignableFrom(d.clazz)) {
clazzes.add(d.clazz);
Expand Down Expand Up @@ -810,7 +808,7 @@ private static List<? extends Descriptor> getDescriptorList() {
return j.getExtensionList(Descriptor.class);
} else {
// TODO should be part of ExtensionList.lookup in core, but here now for benefit of tests:
List<Descriptor<?>> descriptors = new ArrayList<Descriptor<?>>();
List<Descriptor<?>> descriptors = new ArrayList<>();
for (IndexItem<Extension,Object> item : Index.load(Extension.class, Object.class)) {
try {
Object o = item.instance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<File,State> states = new HashMap<File,State>();
private static final Map<File,State> 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();
Expand All @@ -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)) {
Expand Down Expand Up @@ -267,14 +266,14 @@ public Set<? extends Class<?>> 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<Void> {
Exec(StepContext context) {
super(context);
}
@Override protected Void run() throws Exception {
@Override protected Void run() {
throw new AssertionError("ought to fail");
}
}
Expand All @@ -300,14 +299,14 @@ private static final class Exec extends SynchronousNonBlockingStepExecution<Void
}
public static final class CheckClassLoader extends Step {
@DataBoundConstructor public CheckClassLoader() {}
@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<Void> {
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!");
}
Expand Down