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

Code improvements and clean ups #307

Merged
merged 9 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public String getFunctionName() {
return "emailextrecipients";
}

@NonNull
@Override
public String getDisplayName() {
return "Extended Email Recipients";
Expand Down
1 change: 1 addition & 0 deletions src/main/java/hudson/plugins/emailext/EmailExtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public String getFunctionName() {
return "emailext";
}

@NonNull
@Override
public String getDisplayName() {
return "Extended Email";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ public void setMatrixTriggerMode(MatrixTriggerMode matrixTriggerMode) {
this.matrixTriggerMode = matrixTriggerMode;
}

@NonNull
@Override
public Collection<? extends Action> getProjectActions(AbstractProject<?, ?> project) {
return Collections.singletonList(new EmailExtWatchAction(project));
Expand All @@ -347,7 +348,7 @@ public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
debug(listener.getLogger(), "Checking for post-build");
if (!(build instanceof MatrixRun) || isExecuteOnMatrixNodes()) {
debug(listener.getLogger(), "Performing post-build step");
Expand Down Expand Up @@ -713,7 +714,7 @@ private boolean executeScript(String rawScript, String scriptName, ExtendedEmail
throw e;
} catch (Throwable t) {
Functions.printStackTrace(t, pw);
logger.println(out.toString());
logger.println(out);
// should we cancel the sending of the email???
}
debug(logger, out.toString());
Expand Down Expand Up @@ -873,7 +874,7 @@ MailAccount getMailAccount(ExtendedEmailPublisherContext context) throws Address

private MimeMessage createMail(
ExtendedEmailPublisherContext context, InternetAddress fromAddress, Session session)
throws MessagingException, UnsupportedEncodingException, InterruptedException {
throws MessagingException, UnsupportedEncodingException {
ExtendedEmailPublisherDescriptor descriptor = getDescriptor();

String charset = descriptor.getCharset();
Expand Down Expand Up @@ -1142,7 +1143,7 @@ public MatrixAggregator createAggregator(MatrixBuild matrixbuild,
Launcher launcher, BuildListener buildlistener) {
return new MatrixAggregator(matrixbuild, launcher, buildlistener) {
@Override
public boolean endBuild() throws InterruptedException, IOException {
public boolean endBuild() {
LOGGER.log(Level.FINER, "end build of {0}", this.build.getDisplayName());

// Will be run by parent so we check if needed to be executed by parent
Expand All @@ -1153,7 +1154,7 @@ public boolean endBuild() throws InterruptedException, IOException {
}

@Override
public boolean startBuild() throws InterruptedException, IOException {
public boolean startBuild() {
LOGGER.log(Level.FINER, "end build of {0}", this.build.getDisplayName());
// Will be run by parent so we check if needed to be executed by parent
if (getMatrixTriggerMode().forParent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected void setPublisher(ExtendedEmailPublisher publisher) {
@Deprecated
public AbstractBuild<?, ?> getBuild() {
if(run instanceof AbstractBuild) {
return (AbstractBuild)run;
return (AbstractBuild<?, ?>)run;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
Expand Down Expand Up @@ -260,6 +259,7 @@ public static void autoConfigure() {
}
}

@NonNull
@Override
public String getDisplayName() {
return Messages.ExtendedEmailPublisherDescriptor_DisplayName();
Expand Down Expand Up @@ -720,7 +720,6 @@ public void setDefaultClasspath(List<GroovyScriptPath> defaultClasspath) throws
public List<String> getDefaultTriggerIds() {
if (defaultTriggerIds.isEmpty()) {
if (!defaultTriggers.isEmpty()) {
defaultTriggerIds.clear();
for (EmailTriggerDescriptor t : this.defaultTriggers) {
// we have to do the below because a bunch of stuff is not serialized for the Descriptor
EmailTriggerDescriptor d = Jenkins.get().getDescriptorByType(t.getClass());
Expand Down Expand Up @@ -772,8 +771,7 @@ public String getHelpFile() {
return "/plugin/email-ext/help/main.html";
}

public FormValidation doAddressCheck(@QueryParameter final String value)
throws IOException, ServletException {
public FormValidation doAddressCheck(@QueryParameter final String value) {
try {
new InternetAddress(value);
return FormValidation.ok();
Expand All @@ -782,13 +780,11 @@ public FormValidation doAddressCheck(@QueryParameter final String value)
}
}

public FormValidation doRecipientListRecipientsCheck(@QueryParameter final String value)
throws IOException, ServletException {
public FormValidation doRecipientListRecipientsCheck(@QueryParameter final String value) {
return new EmailRecipientUtils().validateFormRecipientList(value);
}

public FormValidation doMaxAttachmentSizeCheck(@QueryParameter final String value)
throws IOException, ServletException {
public FormValidation doMaxAttachmentSizeCheck(@QueryParameter final String value) {
try {
String testValue = value.trim();
// we support an empty value (which means default)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/emailext/GroovyScriptPath.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.emailext;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
Expand Down Expand Up @@ -49,6 +50,7 @@ public URL asURL() {

@Extension
public static class GroovyScriptPathDescriptor extends Descriptor<GroovyScriptPath> {
@NonNull
@Override
public String getDisplayName() {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public ContentType getContentType() {
return ContentType.DefinedType.GROOVY;
}

@NonNull
@Override
public String getDisplayName() {
return Messages.GroovyTemplateConfigProvider_DisplayName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public ContentType getContentType() {
return ContentType.DefinedType.XML;
}

@NonNull
@Override
public String getDisplayName() {
return Messages.JellyTemplateConfigProvider_DisplayName();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/emailext/MailAccount.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.emailext;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
Expand Down Expand Up @@ -73,6 +74,7 @@ void setDefaultAccount(boolean defaultAccount) {

@Extension
public static class MailAccountDescriptor extends Descriptor<MailAccount>{
@NonNull
@Override
public String getDisplayName(){
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ private int findFirst(InternetAddress address) {
int firstIdx = Integer.MAX_VALUE;
if (address.getPersonal() != null) {
firstIdx = findFirst(firstIdx, "bcc:" + address.getPersonal());
firstIdx = findFirst(firstIdx, "bcc:\"" + address.toString() + "\"");
firstIdx = findFirst(firstIdx, "bcc:\"" + address + "\"");
firstIdx = findFirst(firstIdx, "cc:" + address.getPersonal());
firstIdx = findFirst(firstIdx, "cc:\"" + address.toString() + "\"");
firstIdx = findFirst(firstIdx, "cc:\"" + address + "\"");
firstIdx = findFirst(firstIdx, address.getPersonal());
firstIdx = findFirst(firstIdx, "\"" + address.toString() + "\"");
firstIdx = findFirst(firstIdx, "\"" + address + "\"");
} else {
firstIdx = findFirst(firstIdx, "bcc:" + address.toString());
firstIdx = findFirst(firstIdx, "cc:" + address.toString());
firstIdx = findFirst(firstIdx, "bcc:" + address);
firstIdx = findFirst(firstIdx, "cc:" + address);
firstIdx = findFirst(firstIdx, address.toString());
}
return firstIdx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected synchronized boolean permitsInstance(@CheckForNull Object instance) {
protected boolean isClass(Class<?> declaringClass) {
T t = instance.get();
if (t != null) {
Class c = t.getClass();
Class<?> c = t.getClass();
while (c != null && c != Object.class) {
if (declaringClass == c) {
return true;
Expand All @@ -38,8 +38,8 @@ protected boolean isClass(Class<?> declaringClass) {
return false;
}

private boolean isInterface(Class<?> declaringClass, Class[] interfaces) {
for (Class interf : interfaces) {
private boolean isInterface(Class<?> declaringClass, Class<?>[] interfaces) {
for (Class<?> interf : interfaces) {
if (declaringClass == interf) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected EmailType createMailType(StaplerRequest req, JSONObject formData) {
* @return The number of test failures for the Run
*/
protected int getNumFailures(Run<?, ?> build) {
AbstractTestResultAction<? extends AbstractTestResultAction> a = build.getAction(AbstractTestResultAction.class);
AbstractTestResultAction<? extends AbstractTestResultAction<?>> a = build.getAction(AbstractTestResultAction.class);
if (a instanceof AggregatedTestResultAction) {
int result = 0;
AggregatedTestResultAction action = (AggregatedTestResultAction) a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public List<RecipientProvider> getDefaultRecipientProviders() {
protected EmailTrigger _createDefault() {
EmailTrigger trigger;
try {
Constructor ctor = clazz.getConstructor(List.class, String.class, String.class, String.class, String.class, String.class, int.class, String.class);
trigger = (EmailTrigger)ctor.newInstance(defaultRecipientProviders, "", "$PROJECT_DEFAULT_REPLYTO", "$PROJECT_DEFAULT_SUBJECT", "$PROJECT_DEFAULT_CONTENT", "", 0, "project");
Constructor<? extends EmailTrigger> ctor = clazz.getConstructor(List.class, String.class, String.class, String.class, String.class, String.class, int.class, String.class);
trigger = ctor.newInstance(defaultRecipientProviders, "", "$PROJECT_DEFAULT_REPLYTO", "$PROJECT_DEFAULT_SUBJECT", "$PROJECT_DEFAULT_CONTENT", "", 0, "project");
} catch(Exception e) {
trigger = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static DescriptorExtensionList<RecipientProvider, RecipientProviderDescri
return Jenkins.get().getDescriptorList(RecipientProvider.class);
}

public static List<RecipientProviderDescriptor> allSupporting(Class<? extends Job> clazz) {
public static List<RecipientProviderDescriptor> allSupporting(Class<? extends Job<?, ?>> clazz) {
List<RecipientProviderDescriptor> rt = new ArrayList<>();
for (RecipientProviderDescriptor recipientProviderDescriptor : all()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String getContentType() {
return MIME_TYPE;
}

public InputStream getInputStream() throws IOException {
public InputStream getInputStream() {
return new ByteArrayInputStream(contents);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -161,7 +160,7 @@ public static boolean isChildOf(final FilePath potentialChild, final FilePath pa
return parent.act(new IsChildFileCallable(potentialChild));
}

private InputStream getManagedFile(Run<?, ?> run, String fileName) throws UnsupportedEncodingException {
private InputStream getManagedFile(Run<?, ?> run, String fileName) {
InputStream stream = null;
Plugin plugin = Jenkins.get().getPlugin("config-file-provider");
if (plugin != null) {
Expand Down Expand Up @@ -196,7 +195,7 @@ public static boolean isApprovedScript(final String script, final Language langu
//checking doesn't check if we are system or not since it assumed being called from doCheckField
return ACL.impersonate(Jenkins.ANONYMOUS, new NotReallyRoleSensitiveCallable<Boolean, Exception>() {
@Override
public Boolean call() throws Exception {
public Boolean call() {
return approval.checking(script, language).kind == FormValidation.Kind.OK;
}
});
Expand All @@ -214,7 +213,7 @@ private IsChildFileCallable(FilePath potentialChild) {
}

@Override
public Boolean invoke(File parent, VirtualChannel channel) throws IOException, InterruptedException {
public Boolean invoke(File parent, VirtualChannel channel) {
if (potentialChild.isRemote()) {
//Not on the same machine so can't be a child of the local file
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void populateArgs(Object args, Map<String, String> map, ListMultimap<Str
for(Map.Entry<String, Object> entry : argMap.entrySet()) {
String value = entry.getValue().toString();
if(entry.getValue() instanceof List) {
List valueList = (List)entry.getValue();
List<?> valueList = (List<?>)entry.getValue();
for(Object v : valueList) {
multiMap.put(entry.getKey(), v.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private void outputTest(StringBuilder buffer, String lineBreak, FailedTest faile
if (failedTest.stackTrace != null || failedTest.errorMessage != null) {
local.append(lineBreak);
}
buffer.append(local.toString());
buffer.append(local);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -91,7 +90,7 @@ public String evaluate(Run<?, ?> run, FilePath workspace, TaskListener listener,
LOGGER.log(Level.SEVERE, missingScriptError);
result = missingScriptError;
} catch (GroovyRuntimeException e) {
result = "Error in script or template: " + e.toString();
result = "Error in script or template: " + e;
} finally {
IOUtils.closeQuietly(inputStream);
}
Expand All @@ -110,8 +109,7 @@ protected Class<? extends ConfigProvider> getProviderClass () {
* @param templateStream the template file stream
* @return the rendered template content
*/
private String renderTemplate(Run<?, ?> build, FilePath workspace, TaskListener listener, InputStream templateStream)
throws IOException {
private String renderTemplate(Run<?, ?> build, FilePath workspace, TaskListener listener, InputStream templateStream) {

String result;

Expand Down Expand Up @@ -152,11 +150,8 @@ private String renderTemplate(Run<?, ?> build, FilePath workspace, TaskListener
} else {
//unapproved script, so run in sandbox
StaticProxyInstanceWhitelist whitelist = new StaticProxyInstanceWhitelist(build, "templates-instances.whitelist");
result = GroovySandbox.runInSandbox(new Callable<String>() {
@Override
public String call() throws Exception {
return tmplR.make(binding).toString(); //TODO there is a PrintWriter instance created in make and bound to out
}
result = GroovySandbox.runInSandbox(() -> {
return tmplR.make(binding).toString(); //TODO there is a PrintWriter instance created in make and bound to out
}, new ProxyWhitelist(
Whitelist.all(),
new TaskListenerInstanceWhitelist(listener),
Expand All @@ -169,7 +164,7 @@ public String call() throws Exception {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Functions.printStackTrace(e, pw);
result = "Exception raised during template rendering: " + e.getMessage() + "\n\n" + sw.toString();
result = "Exception raised during template rendering: " + e.getMessage() + "\n\n" + sw;
}
return result;
}
Expand Down Expand Up @@ -236,8 +231,7 @@ private String executeScript(Run<?, ?> build, FilePath workspace, TaskListener l
* @param variables user variables to be added to the Groovy context
* @return a GroovyShell instance
*/
private GroovyShell createEngine(ExtendedEmailPublisherDescriptor descriptor, Map<String, Object> variables, boolean secure)
throws IOException {
private GroovyShell createEngine(ExtendedEmailPublisherDescriptor descriptor, Map<String, Object> variables, boolean secure) {

ClassLoader cl;
CompilerConfiguration cc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public Action getCoberturaAction() {
public List<TestResult> getJUnitTestResult() {
List<TestResult> result = new ArrayList<>();
List<AggregatedTestResultAction> actions = build.getActions(AggregatedTestResultAction.class);
for (Action action : actions) {
for (AggregatedTestResultAction action : actions) {
/* Maven Project */
List<AggregatedTestResultAction.ChildReport> reportList =
((AggregatedTestResultAction) action).getChildReports();
List<AggregatedTestResultAction.ChildReport> reportList = action.getChildReports();
for (AggregatedTestResultAction.ChildReport report : reportList) {
if (report.result instanceof hudson.tasks.junit.TestResult) {
result.add((TestResult) report.result);
Expand Down
Loading