Skip to content

Commit a3803c6

Browse files
authored
Merge pull request jenkinsci#110 from offa/modernization
Code modernization
2 parents 243abcf + 5bb23f4 commit a3803c6

File tree

6 files changed

+29
-30
lines changed

6 files changed

+29
-30
lines changed

src/main/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentBuildWrapper.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.cloudbees.plugins.credentials.CredentialsProvider;
2929
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
3030
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
31-
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
3231
import edu.umd.cs.findbugs.annotations.CheckForNull;
3332
import edu.umd.cs.findbugs.annotations.NonNull;
3433
import edu.umd.cs.findbugs.annotations.Nullable;
@@ -125,7 +124,7 @@ public SSHAgentBuildWrapper(CredentialHolder[] credentialHolders, boolean ignore
125124
*/
126125
@SuppressWarnings("unused") // used via stapler
127126
public SSHAgentBuildWrapper(List<String> credentialIds, boolean ignoreMissing) {
128-
this.credentialIds = new ArrayList<String>(new LinkedHashSet<String>(credentialIds));
127+
this.credentialIds = new ArrayList<>(new LinkedHashSet<>(credentialIds));
129128
this.ignoreMissing = ignoreMissing;
130129
}
131130

@@ -182,11 +181,11 @@ public boolean isIgnoreMissing() {
182181
*/
183182
@SuppressWarnings("unused") // used via stapler
184183
public CredentialHolder[] getCredentialHolders() {
185-
List<CredentialHolder> result = new ArrayList<CredentialHolder>(credentialIds.size());
184+
List<CredentialHolder> result = new ArrayList<>(credentialIds.size());
186185
for (String id : credentialIds) {
187186
result.add(new CredentialHolder(id));
188187
}
189-
return result.toArray(new CredentialHolder[result.size()]);
188+
return result.toArray(new CredentialHolder[0]);
190189
}
191190

192191
/**
@@ -196,8 +195,8 @@ public CredentialHolder[] getCredentialHolders() {
196195
public void preCheckout(AbstractBuild build, Launcher launcher, BuildListener listener)
197196
throws IOException, InterruptedException {
198197
// first collect all the keys (this is so we can bomb out before starting an agent
199-
List<SSHUserPrivateKey> keys = new ArrayList<SSHUserPrivateKey>();
200-
for (String id : new LinkedHashSet<String>(getCredentialIds())) {
198+
List<SSHUserPrivateKey> keys = new ArrayList<>();
199+
for (String id : new LinkedHashSet<>(getCredentialIds())) {
201200
final SSHUserPrivateKey c = CredentialsProvider.findCredentialById(
202201
id,
203202
SSHUserPrivateKey.class,
@@ -244,7 +243,7 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, BuildList
244243
return new NoOpEnvironment();
245244
}
246245

247-
private SSHAgentEnvironment createSSHAgentEnvironment(AbstractBuild build, Launcher launcher, BuildListener listener)
246+
private SSHAgentEnvironment createSSHAgentEnvironment(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
248247
throws IOException, InterruptedException {
249248
try {
250249
return new SSHAgentEnvironment(launcher, listener, build.getWorkspace());
@@ -359,7 +358,7 @@ public SSHAgentEnvironment(Launcher launcher, BuildListener listener, @CheckForN
359358
this.launcher = launcher;
360359
this.listener = listener;
361360
listener.getLogger().println("[ssh-agent] Looking for ssh-agent implementation...");
362-
Map<String, Throwable> faults = new LinkedHashMap<String, Throwable>();
361+
Map<String, Throwable> faults = new LinkedHashMap<>();
363362
for (RemoteAgentFactory factory : Jenkins.get().getExtensionList(RemoteAgentFactory.class)) {
364363
if (factory.isSupported(launcher, listener)) {
365364
try {
@@ -465,7 +464,7 @@ public String getId() {
465464
*/
466465
@NonNull
467466
public static List<String> toIdList(@Nullable CredentialHolder[] credentialHolders) {
468-
List<String> result = new ArrayList<String>(credentialHolders == null ? 0 : credentialHolders.length);
467+
List<String> result = new ArrayList<>(credentialHolders == null ? 0 : credentialHolders.length);
469468
if (credentialHolders != null) {
470469
for (CredentialHolder h : credentialHolders) {
471470
result.add(h.getId());
@@ -505,7 +504,7 @@ public ListBoxModel doFillIdItems(@AncestorInPath Item item) {
505504
item instanceof Queue.Task ? Tasks.getAuthenticationOf((Queue.Task) item) : ACL.SYSTEM,
506505
item,
507506
SSHUserPrivateKey.class,
508-
Collections.<DomainRequirement>emptyList(),
507+
Collections.emptyList(),
509508
SSHAuthenticator.matcher()
510509
);
511510
}

src/main/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentStep.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
55
import com.cloudbees.plugins.credentials.CredentialsProvider;
66
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
7-
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
87
import edu.umd.cs.findbugs.annotations.NonNull;
98
import hudson.Extension;
109
import hudson.model.Item;
@@ -90,7 +89,7 @@ public ListBoxModel doFillCredentialsItems(@AncestorInPath Item item) {
9089
item instanceof Queue.Task ? Tasks.getAuthenticationOf((Queue.Task)item) : ACL.SYSTEM,
9190
item,
9291
SSHUserPrivateKey.class,
93-
Collections.<DomainRequirement>emptyList(),
92+
Collections.emptyList(),
9493
SSHAuthenticator.matcher()
9594
);
9695
}

src/main/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentStepExecution.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SSHAgentStepExecution extends AbstractStepExecutionImpl implements
6060
@Override
6161
public boolean start() throws Exception {
6262
StepContext context = getContext();
63-
sockets = new ArrayList<String>();
63+
sockets = new ArrayList<>();
6464
initRemoteAgent();
6565
context.newBodyInvoker().
6666
withContext(EnvironmentExpander.merge(getContext().get(EnvironmentExpander.class), new ExpanderImpl(this))).
@@ -133,8 +133,8 @@ public void expand(EnvVars env) throws IOException, InterruptedException {
133133
*/
134134
private void initRemoteAgent() throws IOException, InterruptedException {
135135

136-
List<SSHUserPrivateKey> userPrivateKeys = new ArrayList<SSHUserPrivateKey>();
137-
for (String id : new LinkedHashSet<String>(step.getCredentials())) {
136+
List<SSHUserPrivateKey> userPrivateKeys = new ArrayList<>();
137+
for (String id : new LinkedHashSet<>(step.getCredentials())) {
138138
final SSHUserPrivateKey c = CredentialsProvider.findCredentialById(id, SSHUserPrivateKey.class, build);
139139
CredentialsProvider.track(build, c);
140140
if (c == null && !step.isIgnoreMissing()) {
@@ -150,7 +150,7 @@ private void initRemoteAgent() throws IOException, InterruptedException {
150150

151151
// TODO UI could be streamlined now that there is only one impl
152152
listener.getLogger().println("[ssh-agent] Looking for ssh-agent implementation...");
153-
Map<String, Throwable> faults = new LinkedHashMap<String, Throwable>();
153+
Map<String, Throwable> faults = new LinkedHashMap<>();
154154
for (RemoteAgentFactory factory : Jenkins.get().getExtensionList(RemoteAgentFactory.class)) {
155155
if (factory.isSupported(launcher, listener)) {
156156
try {

src/test/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.net.BindException;
1212
import java.net.InetAddress;
1313
import java.net.ServerSocket;
14+
import java.nio.charset.StandardCharsets;
1415
import java.nio.file.Paths;
1516
import java.security.PublicKey;
1617
import java.security.interfaces.RSAPublicKey;
@@ -112,7 +113,7 @@ public void setExitCallback(ExitCallback exitCallback) {
112113
public void start(ChannelSession channel, Environment env) throws IOException {
113114
if (outputStream != null) {
114115
try {
115-
outputStream.write("Connection established. Closing...\n".getBytes("UTF-8"));
116+
outputStream.write("Connection established. Closing...\n".getBytes(StandardCharsets.UTF_8));
116117
outputStream.flush();
117118
} catch (IOException e) {
118119
// squash

src/test/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentBuildWrapperTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SSHAgentBuildWrapperTest extends SSHAgentBase {
3838
public void sshAgentAvailable() throws Exception {
3939
startMockSSHServer();
4040

41-
List<String> credentialIds = new ArrayList<String>();
41+
List<String> credentialIds = new ArrayList<>();
4242
credentialIds.add(CREDENTIAL_ID);
4343

4444
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -67,7 +67,7 @@ public void sshAgentAvailable() throws Exception {
6767
public void sshAgentDoesNotDieAfterFirstUse() throws Exception {
6868
startMockSSHServer();
6969

70-
List<String> credentialIds = new ArrayList<String>();
70+
List<String> credentialIds = new ArrayList<>();
7171
credentialIds.add(CREDENTIAL_ID);
7272

7373
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -108,7 +108,7 @@ public void sshAgentDoesNotDieAfterFirstUse() throws Exception {
108108
public void sshAgentUnavailable() throws Exception {
109109
startMockSSHServer();
110110

111-
List<String> credentialIds = new ArrayList<String>();
111+
List<String> credentialIds = new ArrayList<>();
112112
credentialIds.add(CREDENTIAL_ID);
113113

114114
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -130,7 +130,7 @@ public void sshAgentUnavailable() throws Exception {
130130
public void sshAgentWithInvalidCredentials() throws Exception {
131131
startMockSSHServer();
132132

133-
List<String> credentialIds = new ArrayList<String>();
133+
List<String> credentialIds = new ArrayList<>();
134134
credentialIds.add(CREDENTIAL_ID);
135135

136136
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -156,7 +156,7 @@ public void sshAgentWithInvalidCredentials() throws Exception {
156156
public void testTrackingOfCredential() throws Exception {
157157
startMockSSHServer();
158158

159-
List<String> credentialIds = new ArrayList<String>();
159+
List<String> credentialIds = new ArrayList<>();
160160
credentialIds.add(CREDENTIAL_ID);
161161

162162
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -195,7 +195,7 @@ public void testTrackingOfCredential() throws Exception {
195195
public void sshAgentWithSpacesInWorkspacePath() throws Exception {
196196
startMockSSHServer();
197197

198-
List<String> credentialIds = new ArrayList<String>();
198+
List<String> credentialIds = new ArrayList<>();
199199
credentialIds.add(CREDENTIAL_ID);
200200

201201
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -226,7 +226,7 @@ public void sshAgentWithSpacesInWorkspacePath() throws Exception {
226226
public void sshAgentWithTrickyPassphrase() throws Exception {
227227
startMockSSHServer();
228228

229-
List<String> credentialIds = new ArrayList<String>();
229+
List<String> credentialIds = new ArrayList<>();
230230
credentialIds.add(CREDENTIAL_ID);
231231

232232
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",

src/test/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentStepWorkflowTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void sshAgentAvailable() throws Exception {
5959
public void evaluate() throws Throwable {
6060
startMockSSHServer();
6161

62-
List<String> credentialIds = new ArrayList<String>();
62+
List<String> credentialIds = new ArrayList<>();
6363
credentialIds.add(CREDENTIAL_ID);
6464

6565
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -100,7 +100,7 @@ public void sshAgentAvailableAfterRestart() throws Exception {
100100
public void evaluate() throws Throwable {
101101
startMockSSHServer();
102102

103-
List<String> credentialIds = new ArrayList<String>();
103+
List<String> credentialIds = new ArrayList<>();
104104
credentialIds.add(CREDENTIAL_ID);
105105

106106
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -140,7 +140,7 @@ public void evaluate() throws Throwable {
140140

141141
Pattern pattern = Pattern.compile("(?:SSH Agent (?:before|after) restart )/.+/ssh-.+/agent.(\\d)+");
142142
Scanner sc = new Scanner(b.getLogFile());
143-
List<String> socketFile = new ArrayList<String>();
143+
List<String> socketFile = new ArrayList<>();
144144
while (sc.hasNextLine()) {
145145
String match = sc.findInLine(pattern);
146146
if (match != null) {
@@ -167,7 +167,7 @@ public void evaluate() throws Throwable {
167167
@Test
168168
public void agentConnectionDropTest() throws Exception {
169169
story.then(r -> {
170-
List<String> credentialIds = new ArrayList<String>();
170+
List<String> credentialIds = new ArrayList<>();
171171
credentialIds.add(CREDENTIAL_ID);
172172
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
173173
new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(getPrivateKey()), "cloudbees", "test");
@@ -208,7 +208,7 @@ public void testTrackingOfCredential() {
208208
public void evaluate() throws Throwable {
209209
startMockSSHServer();
210210

211-
List<String> credentialIds = new ArrayList<String>();
211+
List<String> credentialIds = new ArrayList<>();
212212
credentialIds.add(CREDENTIAL_ID);
213213

214214
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "cloudbees",
@@ -252,7 +252,7 @@ public void sshAgentDocker() throws Exception {
252252
assumeNoException("have Docker installed", x);
253253
}
254254

255-
List<String> credentialIds = new ArrayList<String>();
255+
List<String> credentialIds = new ArrayList<>();
256256
credentialIds.add(CREDENTIAL_ID);
257257

258258
SSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, credentialIds.get(0), "x",

0 commit comments

Comments
 (0)