Skip to content

Commit

Permalink
Miscellaneous code cleanup (#6362)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Mar 14, 2022
1 parent 49833dd commit c5f996a
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 73 deletions.
2 changes: 0 additions & 2 deletions core/src/main/java/hudson/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1783,8 +1783,6 @@ static class UrlPluginCopier implements PluginCopier {
public void copy(File target) throws Exception {
try (InputStream input = ProxyConfiguration.getInputStream(new URL(url))) {
Files.copy(input, target.toPath());
} catch (Exception e) {
throw e;
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/hudson/tasks/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.Functions;
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.PersistentDescriptor;
Expand All @@ -43,7 +44,6 @@
import jenkins.tasks.filters.EnvVarsFilterLocalRule;
import jenkins.tasks.filters.EnvVarsFilterLocalRuleDescriptor;
import net.sf.json.JSONObject;
import org.apache.commons.lang.SystemUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.Beta;
Expand Down Expand Up @@ -175,7 +175,7 @@ public String getShell() {
@Deprecated
public String getShellOrDefault() {
if (shell == null) {
return SystemUtils.IS_OS_WINDOWS ? "sh" : "/bin/sh";
return Functions.isWindows() ? "sh" : "/bin/sh";
}
return shell;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ private static final class Shellinterpreter extends MasterToSlaveCallable<String

@Override
public String call() throws IOException {
return SystemUtils.IS_OS_WINDOWS ? "sh" : "/bin/sh";
return Functions.isWindows() ? "sh" : "/bin/sh";
}
}

Expand Down
10 changes: 7 additions & 3 deletions core/src/main/java/hudson/util/PackedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.mapper.Mapper;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractSet;
Expand All @@ -54,9 +55,8 @@
*
* @author Kohsuke Kawaguchi
*/
@SuppressWarnings("unchecked")
public final class PackedMap<K, V> extends AbstractMap<K, V> {
private Object[] kvpairs;
private final Object[] kvpairs;

/**
*
Expand All @@ -77,6 +77,7 @@ private PackedMap(Map<? extends K, ? extends V> src) {
}

private final Set<Entry<K, V>> entrySet = new AbstractSet<Entry<K, V>>() {
@NonNull
@Override
public Iterator<Entry<K, V>> iterator() {
return new Iterator<Entry<K, V>>() {
Expand Down Expand Up @@ -136,14 +137,17 @@ public boolean containsKey(Object key) {
}

@Override
@SuppressWarnings("unchecked")
public V get(Object key) {
for (int i = 0; i < kvpairs.length; i += 2)
if (key.equals(kvpairs[i]))
return (V) kvpairs[i + 1];
return null;
}

@NonNull
@Override
@SuppressWarnings("unchecked")
public Collection<V> values() {
return new AbstractList<V>() {
@Override
Expand Down Expand Up @@ -178,7 +182,7 @@ protected Object createCollection(Class type) {

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
return PackedMap.of((Map) super.unmarshal(reader, context));
return PackedMap.of((Map<?, ?>) super.unmarshal(reader, context));
}
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@ public DescribableList<NodeProperty<?>, NodePropertyDescriptor> getGlobalNodePro
*/
void trimLabels(Node... nodes) {
Set<LabelAtom> includedLabels = new HashSet<>();
Arrays.asList(nodes).stream().filter(Objects::nonNull).forEach(n -> includedLabels.addAll(n.getAssignedLabels()));
Arrays.stream(nodes).filter(Objects::nonNull).forEach(n -> includedLabels.addAll(n.getAssignedLabels()));
trimLabels(includedLabels);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public boolean isEmpty() {
@Override
public boolean contains(Object o) {
if (o instanceof Map.Entry) {
Map.Entry e = (Map.Entry) o;
Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
Object k = e.getKey();
if (k instanceof Integer) {
return owner.getByNumber((Integer) k).equals(e.getValue());
Expand Down
46 changes: 2 additions & 44 deletions core/src/test/java/hudson/ChannelRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.remoting.FastPipedInputStream;
import hudson.remoting.FastPipedOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -54,50 +55,7 @@ public Channel call() throws Exception {
french.join();
british.join();
} catch (IOException e) {
// perhaps this exception is caused by earlier abnormal termination of the channel?
/* for the record, this is the failure.
Nov 12, 2009 6:18:55 PM hudson.remoting.Channel$CloseCommand execute
SEVERE: close command failed on This side of the channel
java.io.IOException: Pipe is already closed
at hudson.remoting.FastPipedOutputStream.write(FastPipedOutputStream.java:127)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1838)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1747)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1249)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1203)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1387)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1538)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:329)
at hudson.remoting.Channel.send(Channel.java:413)
at hudson.remoting.Channel.close(Channel.java:717)
at hudson.remoting.Channel$CloseCommand.execute(Channel.java:676)
at hudson.remoting.Channel$ReaderThread.run(Channel.java:860)
Caused by: hudson.remoting.FastPipedInputStream$ClosedBy: The pipe was closed at...
at hudson.remoting.FastPipedInputStream.close(FastPipedInputStream.java:103)
at java.io.ObjectInputStream$PeekInputStream.close(ObjectInputStream.java:2305)
at java.io.ObjectInputStream$BlockDataInputStream.close(ObjectInputStream.java:2643)
at java.io.ObjectInputStream.close(ObjectInputStream.java:873)
at hudson.remoting.Channel$ReaderThread.run(Channel.java:866)
Nov 12, 2009 6:18:55 PM hudson.remoting.Channel$CloseCommand execute
INFO: close command created at
Command close created at
at hudson.remoting.Command.<init>(Command.java:58)
at hudson.remoting.Command.<init>(Command.java:47)
at hudson.remoting.Channel$CloseCommand.<init>(Channel.java:673)
at hudson.remoting.Channel$CloseCommand.<init>(Channel.java:673)
at hudson.remoting.Channel.close(Channel.java:717)
at hudson.remoting.Channel$CloseCommand.execute(Channel.java:676)
at hudson.remoting.Channel$ReaderThread.run(Channel.java:860)
Nov 12, 2009 6:18:55 PM hudson.remoting.Channel$ReaderThread run
SEVERE: I/O error in channel This side of the channel
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2554)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at hudson.remoting.Channel$ReaderThread.run(Channel.java:849)
*/
e.printStackTrace();
throw new UncheckedIOException(e);
} catch (InterruptedException x) {
throw new AssertionError(x);
}
Expand Down
6 changes: 2 additions & 4 deletions core/src/test/java/hudson/util/ConsistentHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,8 @@ public void usesCustomHash() {
throw exception;
};

final RuntimeException e = assertThrows(RuntimeException.class, () -> {
ConsistentHash<String> hash = new ConsistentHash<>(hashFunction);
hash.add("foo");
});
ConsistentHash<String> hash = new ConsistentHash<>(hashFunction);
final RuntimeException e = assertThrows(RuntimeException.class, () -> hash.add("foo"));
assertSame(exception, e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public static void main(String[] a) throws Exception {
}
br.close();
// List html entries not in TLD text list
for (String key : (new TreeMap<>(htmlInfo)).keySet()) {
for (String key : new TreeMap<>(htmlInfo).keySet()) {
if (!ianaTlds.contains(key)) {
if (isNotInRootZone(key)) {
System.out.println("INFO: HTML entry not yet in root zone: " + key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts)
}


return (!maxIndex);
return !maxIndex;
}

private String testPartsIndextoString() {
Expand Down
2 changes: 1 addition & 1 deletion test/src/test/java/hudson/model/QueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public static final class FileItemPersistenceTestServlet extends HttpServlet {
@SuppressWarnings("deprecation")
HtmlPage p = (HtmlPage) wc.getPage("http://localhost:" + connector.getLocalPort() + '/');
HtmlForm f = p.getFormByName("main");
HtmlFileInput input = (HtmlFileInput) f.getInputByName("test");
HtmlFileInput input = f.getInputByName("test");
input.setData(testData);
HtmlFormUtil.submit(f);
} finally {
Expand Down
7 changes: 3 additions & 4 deletions test/src/test/java/hudson/model/ViewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlLabel;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand Down Expand Up @@ -175,17 +174,17 @@ public class ViewTest {
HtmlAnchor privateViewsLink = userPage.getAnchorByText("My Views");
assertNotNull("My Views link not available", privateViewsLink);

HtmlPage privateViewsPage = (HtmlPage) privateViewsLink.click();
HtmlPage privateViewsPage = privateViewsLink.click();

Text viewLabel = (Text) privateViewsPage.getFirstByXPath("//div[@class='tabBar']//div[@class='tab active']/a/text()");
Text viewLabel = privateViewsPage.getFirstByXPath("//div[@class='tabBar']//div[@class='tab active']/a/text()");
assertTrue("'All' view should be selected", viewLabel.getTextContent().contains(Hudson_ViewName()));

View listView = listView("listView");

HtmlPage newViewPage = wc.goTo("user/me/my-views/newView");
HtmlForm form = newViewPage.getFormByName("createItem");
form.getInputByName("name").setValueAttribute("proxy-view");
((HtmlRadioButtonInput) form.getInputByValue("hudson.model.ProxyView")).setChecked(true);
form.getInputByValue("hudson.model.ProxyView").setChecked(true);
HtmlPage proxyViewConfigurePage = j.submit(form);
View proxyView = user.getProperty(MyViewsProperty.class).getView("proxy-view");
assertNotNull(proxyView);
Expand Down
3 changes: 1 addition & 2 deletions test/src/test/java/hudson/security/LoginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlFormUtil;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
Expand Down Expand Up @@ -86,7 +85,7 @@ private HtmlForm prepareLoginFormWithRememberMeChecked(WebClient wc) throws IOEx
HtmlForm form = page.getFormByName("login");
form.getInputByName("j_username").setValueAttribute("alice");
form.getInputByName("j_password").setValueAttribute("alice");
((HtmlCheckBoxInput) form.getInputByName("remember_me")).setChecked(true);
form.getInputByName("remember_me").setChecked(true);
return form;
}

Expand Down
8 changes: 4 additions & 4 deletions test/src/test/java/lib/form/RowVisibilityGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class RowVisibilityGroupTest {
public void test1() throws Exception {
HtmlPage p = j.createWebClient().goTo("self/test1");

HtmlElement outer = (HtmlElement) DomNodeUtil.selectSingleNode(p, "//INPUT[@name='outer']");
HtmlElement inner = (HtmlElement) DomNodeUtil.selectSingleNode(p, "//INPUT[@name='inner']");
HtmlInput field = (HtmlInput) DomNodeUtil.selectSingleNode(p, "//INPUT[@type='text'][@name='_.field']");
HtmlElement outer = DomNodeUtil.selectSingleNode(p, "//INPUT[@name='outer']");
HtmlElement inner = DomNodeUtil.selectSingleNode(p, "//INPUT[@name='inner']");
HtmlInput field = DomNodeUtil.selectSingleNode(p, "//INPUT[@type='text'][@name='_.field']");

// outer gets unfolded, but inner should be still folded
outer.click();
Expand All @@ -80,7 +80,7 @@ public void test1() throws Exception {
public void test2() throws Exception {
HtmlPage p = j.createWebClient().goTo("self/test2");

HtmlSelect s = (HtmlSelect) DomNodeUtil.selectSingleNode(p, "//SELECT");
HtmlSelect s = DomNodeUtil.selectSingleNode(p, "//SELECT");
List<HtmlOption> opts = s.getOptions();

// those first selections will load additional HTMLs
Expand Down
3 changes: 1 addition & 2 deletions test/src/test/java/lib/layout/RenderOnDemandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClientUtil;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.InvisibleAction;
import hudson.model.RootAction;
Expand Down Expand Up @@ -95,7 +94,7 @@ public void testScript() throws Exception {
HtmlPage p = j.createWebClient().goTo("self/testScript");
assertNull(p.getElementById("loaded"));

((HtmlElement) p.getElementById("button")).click();
p.getElementById("button").click();
WebClientUtil.waitForJSExec(p.getWebClient());
// all AJAX calls complete before the above method returns
assertNotNull(p.getElementById("loaded"));
Expand Down

0 comments on commit c5f996a

Please sign in to comment.