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 #256

Merged
merged 12 commits into from
Mar 20, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@
}

private <T> T castOrNull(Class<T> key, Object o) {
if (key.isInstance(o)) return key.cast(o);
else return null;
if (key.isInstance(o)) {

Check warning on line 149 in src/main/java/org/jenkinsci/plugins/workflow/support/DefaultStepContext.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 149 is only partially covered, one branch is missing
return key.cast(o);
} else {
return null;

Check warning on line 152 in src/main/java/org/jenkinsci/plugins/workflow/support/DefaultStepContext.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 152 is not covered by tests
}
}

private @CheckForNull Launcher makeLauncher(@CheckForNull Launcher contextual) throws IOException, InterruptedException {
Expand Down Expand Up @@ -198,7 +201,7 @@
.filter(e -> e instanceof PasswordParameterValue
&& !((Secret) e.getValue()).getPlainText().isEmpty())
.map(ParameterValue::getName)
.collect(Collectors.toCollection(() -> new HashSet<>())); // Make sure the set is serializable.
.collect(Collectors.toCollection(HashSet::new)); // Make sure the set is serializable.
} else {
passwordParameterVariables = Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
Expand Down Expand Up @@ -134,37 +133,35 @@
} catch (IOException e) {
ByteBuffer buf = new ByteBuffer();
PrintStream ps;
try {
ps = new PrintStream(buf, false, "UTF-8");
} catch (UnsupportedEncodingException x) {
throw new AssertionError(x);
}
ps = new PrintStream(buf, false, StandardCharsets.UTF_8);
ps.println("Failed to find log file for id="+parent.getId());
e.printStackTrace(ps);
ps.close();
return new AnnotatedLargeText<FlowNode>(buf, StandardCharsets.UTF_8, true, parent);
return new AnnotatedLargeText<>(buf, StandardCharsets.UTF_8, true, parent);
}
}

/**
* The actual log file.
*/
private File getLogFile() throws IOException {
if (log==null)
if (log == null) {
log = new File(parent.getExecution().getOwner().getRootDir(), parent.getId() + ".log");
}
return log;
}

/**
* Used from <code>console.jelly</code> to write annotated log to the given output.
*/
@SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED", justification = "Deprecated and unused code, only exists for compatibility with old builds")
@Restricted(DoNotUse.class) // Jelly
public void writeLogTo(long offset, XMLOutput out) throws IOException {
AnnotatedLargeText l = getLogText();
if (l!=null)
if (l != null) {
l.writeHtmlTo(offset, out.asWriter());
}
}

Check warning on line 164 in src/main/java/org/jenkinsci/plugins/workflow/support/actions/LogActionImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 136-164 are not covered by tests

@Override
public void onLoad(FlowNode parent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static boolean isPaused(@NonNull FlowNode node) {
* @return The {@link PauseAction} instances for the supplied node. Returns an empty list if there are none.
*/
public static @NonNull List<PauseAction> getPauseActions(@NonNull FlowNode node) {
List<PauseAction> pauseActions = new ArrayList<PauseAction>();
List<PauseAction> pauseActions = new ArrayList<>();
List<Action> actions = node.getActions();

for (Action action : actions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public WorkspaceActionImpl(FilePath workspace, FlowNode parent) {
node = FilePathUtils.getNodeName(workspace);
Jenkins j = Jenkins.getInstanceOrNull();
Node n = j == null ? null : node.isEmpty() ? j : j.getNode(node);
labels = new TreeSet<LabelAtom>();
labels = new TreeSet<>();
if (n != null) {
labels.addAll(n.getAssignedLabels());
labels.remove(n.getSelfLabel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@
}
List<WorkspaceActionImpl> r = new ArrayList<>();
for (FlowNode node : new DepthFirstScanner().allNodes(exec)) {
for (WorkspaceActionImpl action : node.getActions(WorkspaceActionImpl.class)) {
r.add(action);
}
r.addAll(node.getActions(WorkspaceActionImpl.class));

Check warning on line 89 in src/main/java/org/jenkinsci/plugins/workflow/support/actions/WorkspaceRunAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 89 is not covered by tests
}
Collections.reverse(r);
return r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ChainingListenableFuture<I, O>
private ListenableFuture<? extends I> inputFuture;
private volatile ListenableFuture<? extends O> outputFuture;
private final BlockingQueue<Boolean> mayInterruptIfRunningChannel =
new LinkedBlockingQueue<Boolean>(1);
new LinkedBlockingQueue<>(1);
private final CountDownLatch outputCreated = new CountDownLatch(1);

ChainingListenableFuture(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
AsyncFunction<? super I, ? extends O> function,
Executor executor) {
ChainingListenableFuture<I, O> output =
new ChainingListenableFuture<I, O>(function, input);
new ChainingListenableFuture<>(function, input);
input.addListener(output, executor);
return output;
}
Expand Down Expand Up @@ -399,7 +399,7 @@
final Function<? super I, ? extends O> function, Executor executor) {
checkNotNull(function);
AsyncFunction<I, O> wrapperFunction
= new AsyncFunction<I, O>() {
= new AsyncFunction<>() {
@Override public ListenableFuture<O> apply(I input) {
O output = function.apply(input);
return immediateFuture(output);
Expand Down Expand Up @@ -427,7 +427,7 @@
@Beta
public static <V> ListenableFuture<List<V>> allAsList(
ListenableFuture<? extends V>... futures) {
return new ListFuture<V>(ImmutableList.copyOf(futures), true,
return new ListFuture<>(ImmutableList.copyOf(futures), true,

Check warning on line 430 in src/main/java/org/jenkinsci/plugins/workflow/support/concurrent/Futures.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 430 is not covered by tests
MoreExecutors.directExecutor());
}

Expand All @@ -450,7 +450,7 @@
@Beta
public static <V> ListenableFuture<List<V>> allAsList(
Iterable<? extends ListenableFuture<? extends V>> futures) {
return new ListFuture<V>(ImmutableList.copyOf(futures), true,
return new ListFuture<>(ImmutableList.copyOf(futures), true,
MoreExecutors.directExecutor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.kohsuke.accmod.restrictions.NoExternalUse;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;

/**
* Factory and utility methods for {@link java.util.concurrent.Executor}, {@link ExecutorService},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@
}

private void tryLater(int currentDelay) {
if (isCancelled()) return;
if (isCancelled()) {
return;

Check warning on line 87 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/TryRepeatedly.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 86-87 are not covered by tests
}

next = Timer.get().schedule(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@

@Deprecated
public ListenableFuture<PickleResolver> rehydrate() {
return rehydrate(new ArrayList<ListenableFuture<?>>());
return rehydrate(new ArrayList<>());
}

public ListenableFuture<PickleResolver> rehydrate(Collection<ListenableFuture<?>> pickleFutures) {
// if there's nothing to rehydrate, we are done
if (pickles.isEmpty())
if (pickles.isEmpty()) {
return Futures.immediateFuture(this);
}

List<ListenableFuture<?>> members = new ArrayList<ListenableFuture<?>>();
List<ListenableFuture<?>> members = new ArrayList<>();
for (Pickle r : pickles) {
// TODO log("rehydrating " + r);

Check warning on line 83 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/serialization/PickleResolver.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: log("rehydrating " + r);
ListenableFuture<?> future;
try {
future = r.rehydrate(owner);
Expand All @@ -97,14 +98,16 @@

ListenableFuture<List<Object>> all = Futures.allAsList(members);

return Futures.transform(all,new Function<List<Object>, PickleResolver>() {
return Futures.transform(all,new Function<>() {
@Override
public PickleResolver apply(List<Object> input) {
values = input;
return PickleResolver.this;
}
});
}

@Override
public Object readResolve(Object o) {
if (o instanceof DryCapsule) {
DryCapsule cap = (DryCapsule) o;
Expand All @@ -113,6 +116,7 @@
return o;
}

@Override
public Object writeReplace(Object original) {
// only meant to be used for deserialization
throw new IllegalStateException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@
private ObjectResolver ownerResolver = new ObjectResolver() {
@Override
public Object readResolve(Object replacement) {
if (replacement instanceof DryOwner)
if (replacement instanceof DryOwner) {
return owner;
}
return replacement;
}

Expand All @@ -114,19 +115,21 @@
}

private int parseHeader(DataInputStream din) throws IOException {
if (din.readLong()!= RiverWriter.HEADER)
if (din.readLong() != RiverWriter.HEADER) {

Check warning on line 118 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/serialization/RiverReader.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 118 is only partially covered, one branch is missing
throw new IOException("Invalid stream header");
}

short v = din.readShort();
if (v!= RiverWriter.VERSION)
if (v != RiverWriter.VERSION) {

Check warning on line 123 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/serialization/RiverReader.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 123 is only partially covered, one branch is missing
throw new IOException("Unexpected stream version: "+v);
}

return din.readInt();
}

@Deprecated
public ListenableFuture<Unmarshaller> restorePickles() throws IOException {
return restorePickles(new ArrayList<ListenableFuture<?>>());
return restorePickles(new ArrayList<>());

Check warning on line 132 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/serialization/RiverReader.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 132 is not covered by tests
}

/**
Expand Down Expand Up @@ -157,7 +160,8 @@
final Unmarshaller sandboxed = new SandboxedUnmarshaller(eu);

// start rehydrating, and when done make the unmarshaller available
return Futures.transform(evr.rehydrate(pickleFutures), new Function<PickleResolver, Unmarshaller>() {
return Futures.transform(evr.rehydrate(pickleFutures), new Function<>() {
@Override
public Unmarshaller apply(PickleResolver input) {
return sandboxed;
}
Expand Down Expand Up @@ -256,11 +260,11 @@
}

@Override public Object readObject() throws ClassNotFoundException, IOException {
return sandbox(() -> delegate.readObject());
return sandbox(delegate::readObject);
}

@Override public Object readObjectUnshared() throws ClassNotFoundException, IOException {
return sandbox(() -> delegate.readObjectUnshared());
return sandbox(delegate::readObjectUnshared);

Check warning on line 267 in src/main/java/org/jenkinsci/plugins/workflow/support/pickles/serialization/RiverReader.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 267 is not covered by tests
}

@Override public <T> T readObject(Class<T> type) throws ClassNotFoundException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class RiverWriter implements Closeable {
/**
* Persisted form of stateful objects that need special handling during rehydration.
*/
List<Pickle> pickles = new ArrayList<Pickle>();
List<Pickle> pickles = new ArrayList<>();

@Deprecated
public RiverWriter(File f, FlowExecutionOwner _owner) throws IOException {
Expand All @@ -112,13 +112,16 @@ public RiverWriter(File f, FlowExecutionOwner _owner, final Collection<? extends
MarshallingConfiguration config = new MarshallingConfiguration();
//config.setSerializabilityChecker(new SerializabilityCheckerImpl());
config.setObjectResolver(new ObjectResolver() {
@Override
public Object readResolve(Object o) {
throw new IllegalStateException();
}

@Override
public Object writeReplace(Object o) {
if (o==owner)
if (o == owner) {
return new DryOwner();
}

if (pickling) {
for (PickleFactory f : pickleFactories) {
Expand Down Expand Up @@ -162,6 +165,7 @@ public ObjectOutput getObjectOutput() {
return marshaller;
}

@Override
public void close() throws IOException {
int ephemeralsOffset;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import hudson.console.ConsoleAnnotationDescriptor;
import hudson.console.HyperlinkNote;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
Expand Down Expand Up @@ -93,15 +92,11 @@
*/
private static String encodeForJavascript(String str) {
// https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
try {
String encode = URLEncoder.encode(str, StandardCharsets.UTF_8.name());
return Base64.getUrlEncoder().encodeToString(encode.getBytes(StandardCharsets.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new InternalError("UTF-8 is missing but mandated by the JVM specification", e);
}
String encode = URLEncoder.encode(str, StandardCharsets.UTF_8);
return Base64.getUrlEncoder().encodeToString(encode.getBytes(StandardCharsets.UTF_8));
}

// TODO why does there need to be a descriptor at all?

Check warning on line 99 in src/main/java/org/jenkinsci/plugins/workflow/support/steps/input/POSTHyperlinkNote.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: why does there need to be a descriptor at all?
@Extension public static final class DescriptorImpl extends ConsoleAnnotationDescriptor {
@Override public String getDisplayName() {
return "POST Hyperlinks";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
@Override
public List<Action> loadActions(@NonNull FlowNode node) throws IOException {
Tag t = getOrLoadNodes().get(node.getId());
return (t != null) ? t.actions() : Collections.<Action>emptyList();
return (t != null) ? t.actions() : Collections.emptyList();

Check warning on line 193 in src/main/java/org/jenkinsci/plugins/workflow/support/storage/BulkFlowNodeStorage.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 193 is not covered by tests
}

/**
Expand Down Expand Up @@ -238,7 +238,7 @@
}

public @NonNull List<Action> actions() {
return actions != null ? Arrays.asList(actions) : Collections.<Action>emptyList();
return actions != null ? Arrays.asList(actions) : Collections.emptyList();
}
}

Expand Down
Loading