Skip to content

Commit

Permalink
Merge pull request #125 from darxriggs/improvements
Browse files Browse the repository at this point in the history
Some Code Improvements
  • Loading branch information
jvz committed Aug 14, 2019
2 parents 1371e42 + c72a8bb commit 6d79efc
Show file tree
Hide file tree
Showing 30 changed files with 72 additions and 147 deletions.
Expand Up @@ -177,11 +177,7 @@ public static Icon getIconByClassSpec(String spec) {
try {
Method getIconByClassSpec = IconSet.class.getMethod("getIconByClassSpec", Object.class);
return (Icon) getIconByClassSpec.invoke(IconSet.icons, spec);
} catch (NoSuchMethodException e) {
// ignore
} catch (IllegalAccessException e) {
// ignore
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// ignore
}
return IconSet.icons.getIconByClassSpec(spec);
Expand Down Expand Up @@ -227,9 +223,7 @@ public static Icon getIcon(@NonNull Action action) {
if (icon == null) {
icon = IconSet.icons.getIconByUrl(action.getIconFileName());
}
} catch (InvocationTargetException e) {
icon = null;
} catch (IllegalAccessException e) {
} catch (InvocationTargetException | IllegalAccessException e) {
icon = null;
}
return icon;
Expand Down Expand Up @@ -259,9 +253,7 @@ public static String getQualifiedUrl(@CheckForNull Icon icon) {
}
}
return null;
} catch (NoSuchFieldException e) {
// ignore we'll use a JellyContext
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException | IllegalAccessException e) {
// ignore we'll use a JellyContext
}
JellyContext ctx = new JellyContext();
Expand Down
Expand Up @@ -496,9 +496,7 @@ public void calcFillSettings(String field, Map<String, Object> attributes) {
}

}
} catch (AssertionError e) {
// ignore, we did the best we could
} catch (UnsupportedEncodingException e) {
} catch (AssertionError | UnsupportedEncodingException e) {
// ignore, we did the best we could
}
}
Expand Down Expand Up @@ -542,9 +540,7 @@ public void calcAutoCompleteSettings(String field, Map<String, Object> attribute
}

}
} catch (AssertionError e) {
// ignore, we did the best we could
} catch (UnsupportedEncodingException e) {
} catch (AssertionError | UnsupportedEncodingException e) {
// ignore, we did the best we could
}
}
Expand Down Expand Up @@ -601,9 +597,7 @@ public String toCheckUrl() {
}

}
} catch (AssertionError e) {
// ignore, we did the best we could
} catch (UnsupportedEncodingException e) {
} catch (AssertionError | UnsupportedEncodingException e) {
// ignore, we did the best we could
}
return checkUrl;
Expand Down
Expand Up @@ -603,11 +603,7 @@ public void exitLiteral(CQLParser.LiteralContext ctx) {
Class<?> enumClazz = Class.forName(enumClass);
Field field = enumClazz.getDeclaredField(enumConst);
literal = (Serializable) field.get(null);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
Expand Down
Expand Up @@ -91,11 +91,7 @@ Result name(@NonNull Credentials credentials, @NonNull Class<?> clazz) {
try {
CredentialsNameProvider nameProvider = nameWith.value().newInstance();
return new Result(nameProvider.getName(credentials), nameWith.priority());
} catch (ClassCastException e) {
// ignore
} catch (InstantiationException e) {
// ignore
} catch (IllegalAccessException e) {
} catch (ClassCastException | InstantiationException | IllegalAccessException e) {
// ignore
}
}
Expand Down
Expand Up @@ -12,7 +12,6 @@
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jenkins.model.Jenkins;
Expand Down
Expand Up @@ -42,6 +42,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.GlobalConfiguration;
Expand Down Expand Up @@ -299,11 +300,11 @@ public int compare(CredentialsProviderTypeRestriction o1, CredentialsProviderTyp
if (index2 == -1) {
return -1;
}
return index1 < index2 ? -1 : (index1 == index2 ? 0 : 1);
return Integer.compare(index1, index2);
}
});
}
if (restrictions == null ? this.restrictions != null : !restrictions.equals(this.restrictions)) {
if (!Objects.equals(restrictions, this.restrictions)) {
this.restrictions = restrictions;
this.restrictionGroups = null;
try {
Expand Down
Expand Up @@ -135,10 +135,7 @@ public static <C extends Credentials> CredentialsResolver<Credentials, C> getRes
}
LOGGER.log(Level.SEVERE, "Resolver {0} for type {1} resolves to {2} which is not assignable to {1}",
new Object[]{resolver.getClass(), clazz, resolver.getToClass()});
} catch (InstantiationException e) {
LOGGER.log(Level.WARNING, "Could not instantiate resolver: " + resolveWith.value(), e);
return null;
} catch (IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException e) {
LOGGER.log(Level.WARNING, "Could not instantiate resolver: " + resolveWith.value(), e);
return null;
}
Expand Down
Expand Up @@ -899,9 +899,7 @@ public ModelObject getContext(String token) {
} catch (NoSuchMethodException e) {
// old Jenkins pre SECURITY-243
return User.get(token, false, Collections.emptyMap());
} catch (InvocationTargetException e) {
return null;
} catch (IllegalAccessException e) {
} catch (InvocationTargetException | IllegalAccessException e) {
return null;
}
}
Expand Down
Expand Up @@ -545,9 +545,7 @@ public HttpResponse doCreateDomain(StaplerRequest req) throws ServletException,
try {
XMLUtils.safeTransform(new StreamSource(req.getReader()), new StreamResult(out));
out.close();
} catch (TransformerException e) {
throw new IOException("Failed to parse credential", e);
} catch (SAXException e) {
} catch (TransformerException | SAXException e) {
throw new IOException("Failed to parse credential", e);
}

Expand Down Expand Up @@ -810,9 +808,7 @@ public HttpResponse doCreateCredentials(StaplerRequest req) throws ServletExcept
try {
XMLUtils.safeTransform(new StreamSource(req.getReader()), new StreamResult(out));
out.close();
} catch (TransformerException e) {
throw new IOException("Failed to parse credential", e);
} catch (SAXException e) {
} catch (TransformerException | SAXException e) {
throw new IOException("Failed to parse credential", e);
}

Expand Down Expand Up @@ -1007,9 +1003,7 @@ public void updateByXml(Source source) throws IOException {
try {
XMLUtils.safeTransform(source, new StreamResult(out));
out.close();
} catch (TransformerException e) {
throw new IOException("Failed to parse credential", e);
} catch (SAXException e) {
} catch (TransformerException | SAXException e) {
throw new IOException("Failed to parse credential", e);
}

Expand Down
Expand Up @@ -42,7 +42,6 @@
import jenkins.model.GlobalConfigurationCategory;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.jenkins.ui.icon.IconSpec;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpResponse;
Expand Down
Expand Up @@ -24,7 +24,6 @@
package com.cloudbees.plugins.credentials;

import hudson.Plugin;
import java.util.Arrays;
import org.jenkins.ui.icon.Icon;
import org.jenkins.ui.icon.IconSet;
import org.jenkins.ui.icon.IconType;
Expand Down
Expand Up @@ -43,7 +43,6 @@
import hudson.security.ACL;
import hudson.security.AccessDeniedException2;
import hudson.security.Permission;
import hudson.util.CopyOnWriteMap;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.net.URI;
Expand Down
Expand Up @@ -99,9 +99,7 @@ protected static HierarchicalStreamReader safeXmlStreamReader(Source source) thr
try {
XMLUtils.safeTransform(source, new StreamResult(out));
out.close();
} catch (TransformerException e) {
throw new IOException("Failed to parse", e);
} catch (SAXException e) {
} catch (TransformerException | SAXException e) {
throw new IOException("Failed to parse", e);
}
return new XppDriver().createReader(new StringReader(out.toString()));
Expand Down
Expand Up @@ -245,8 +245,8 @@ private boolean matchStrings(String pattern, String str) {
char ch;

boolean containsStar = false;
for (int i = 0; i < patArr.length; i++) {
if (patArr[i] == '*') {
for (char c : patArr) {
if (c == '*') {
containsStar = true;
break;
}
Expand Down Expand Up @@ -390,7 +390,7 @@ public String extractPathWithinPattern(String pattern, String path) {
String[] patternParts = StringUtils.split(pattern, this.pathSeparator);
String[] pathParts = StringUtils.split(path, this.pathSeparator);

StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();

// Add any path parts that have a wildcarded pattern part.
int puts = 0;
Expand Down
Expand Up @@ -34,6 +34,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.kohsuke.stapler.DataBoundConstructor;

/**
Expand Down Expand Up @@ -224,11 +226,7 @@ public boolean equals(Object o) {

Domain domain = (Domain) o;

if (name != null ? !name.equals(domain.name) : domain.name != null) {
return false;
}

return true;
return Objects.equals(name, domain.name);
}

/**
Expand Down
Expand Up @@ -31,9 +31,7 @@
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsSelectHelper;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.CredentialsStoreAction;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
Expand All @@ -47,10 +45,7 @@
import hudson.util.FormValidation;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.Set;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
Expand Down
Expand Up @@ -156,17 +156,7 @@ public synchronized KeyStore getKeyStore() {
}
try {
keyStore.load(new ByteArrayInputStream(keyStoreSource.getKeyStoreBytes()), toCharArray(password));
} catch (CertificateException e) {
LogRecord lr = new LogRecord(Level.WARNING, "Credentials ID {0}: Could not load keystore from {1}");
lr.setParameters(new Object[]{getId(), keyStoreSource});
lr.setThrown(e);
LOGGER.log(lr);
} catch (NoSuchAlgorithmException e) {
LogRecord lr = new LogRecord(Level.WARNING, "Credentials ID {0}: Could not load keystore from {1}");
lr.setParameters(new Object[]{getId(), keyStoreSource});
lr.setThrown(e);
LOGGER.log(lr);
} catch (IOException e) {
} catch (CertificateException | NoSuchAlgorithmException | IOException e) {
LogRecord lr = new LogRecord(Level.WARNING, "Credentials ID {0}: Could not load keystore from {1}");
lr.setParameters(new Object[]{getId(), keyStoreSource});
lr.setThrown(e);
Expand Down Expand Up @@ -336,13 +326,7 @@ protected static FormValidation validateCertificateKeystore(String type, byte[]
return FormValidation.ok(StringUtils
.defaultIfEmpty(StandardCertificateCredentials.NameProvider.getSubjectDN(keyStore),
buf.toString()));
} catch (KeyStoreException e) {
return FormValidation.warning(e, Messages.CertificateCredentialsImpl_LoadKeystoreFailed());
} catch (CertificateException e) {
return FormValidation.warning(e, Messages.CertificateCredentialsImpl_LoadKeystoreFailed());
} catch (NoSuchAlgorithmException e) {
return FormValidation.warning(e, Messages.CertificateCredentialsImpl_LoadKeystoreFailed());
} catch (IOException e) {
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
return FormValidation.warning(e, Messages.CertificateCredentialsImpl_LoadKeystoreFailed());
} finally {
if (passwordChars != null) {
Expand Down Expand Up @@ -698,13 +682,7 @@ public HttpResponse doUpload(@NonNull StaplerRequest req) throws ServletExceptio
Method m = XStream2.class.getMethod("addCriticalField", Class.class, String.class);
m.invoke(Items.XSTREAM2, CertificateCredentialsImpl.class, "keyStoreSource");
}
catch (IllegalAccessException e) {
throw new ExceptionInInitializerError(e);
}
catch (InvocationTargetException e) {
throw new ExceptionInInitializerError(e);
}
catch (NoSuchMethodException e) {
catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new ExceptionInInitializerError(e);
}
}
Expand Down
Expand Up @@ -34,6 +34,8 @@
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

import org.apache.commons.lang.StringEscapeUtils;

/**
Expand Down Expand Up @@ -109,10 +111,8 @@ public boolean matches(@NonNull Credentials item) {
}
try {
Object actual = readMethod.invoke(item);
return expected == null ? actual == null : expected.equals(actual);
} catch (IllegalAccessException e) {
return false; // if we cannot access it then it's not a match
} catch (InvocationTargetException e) {
return Objects.equals(expected, actual);
} catch (IllegalAccessException | InvocationTargetException e) {
return false; // if we cannot access it then it's not a match
}
}
Expand Down Expand Up @@ -150,7 +150,7 @@ public boolean equals(Object o) {
if (!name.equals(that.name)) {
return false;
}
return expected != null ? expected.equals(that.expected) : that.expected == null;
return Objects.equals(expected, that.expected);

}

Expand Down

0 comments on commit 6d79efc

Please sign in to comment.