Skip to content

Commit

Permalink
Merge pull request #10 from oleg-nenashev/JENKINS-45055-findbugs-cleanup
Browse files Browse the repository at this point in the history


[JENKINS-45055] - FindBugs cleanup in EnvInject Lib
  • Loading branch information
oleg-nenashev committed Jun 25, 2017
2 parents 9e9ded8 + 62dc34b commit 438588e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 20 deletions.
36 changes: 34 additions & 2 deletions pom.xml
Expand Up @@ -52,7 +52,34 @@
<connection>scm:git:git://github.com/jenkinsci/envinject-lib.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/envinject-lib.git</developerConnection>
<tag>HEAD</tag>
</scm>
</scm>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<configuration>
<effort>Max</effort>
<failOnError>true</failOnError>
</configuration>
<executions>
<execution>
<id>findbugs</id>
<goals>
<goal>check</goal>
</goals>
<phase>verify</phase>
<configuration>
<xmlOutput>true</xmlOutput>
<findbugsXmlOutput>false</findbugsXmlOutput>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>

Expand All @@ -62,7 +89,12 @@
<version>${jenkins.core.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/jenkinsci/lib/envinject/EnvInjectAction.java
@@ -1,6 +1,7 @@
package org.jenkinsci.lib.envinject;

import com.google.common.collect.Maps;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.AbstractBuild;
import hudson.model.Job;
import hudson.model.Run;
Expand Down Expand Up @@ -34,7 +35,7 @@ public class EnvInjectAction implements RunAction2, StaplerProxy {
@Restricted(NoExternalUse.class)
protected transient @CheckForNull Map<String, String> envMap;

private transient @Nonnull Run<?, ?> build;
private transient @CheckForNull Run<?, ?> build;

@Override
public void onAttached(Run<?, ?> run) {
Expand Down Expand Up @@ -96,6 +97,7 @@ public void overrideAll(
}

@SuppressWarnings({"unused", "unchecked"})
@CheckForNull
public Map<String, String> getEnvMap() {
if (envMap == null) {
//Try to fill the envMap from the build injected environment
Expand Down Expand Up @@ -128,8 +130,10 @@ private Object writeReplace() throws ObjectStreamException {
try {
EnvInjectSavable dao = new EnvInjectSavable();

Map<String, String> toWrite = envMap != null ? envMap : Collections.<String, String>emptyMap();

if (rootDir == null) {
dao.saveEnvironment(build.getRootDir(), Maps.transformEntries(envMap,
dao.saveEnvironment(build.getRootDir(), Maps.transformEntries(toWrite,
new Maps.EntryTransformer<String, String, String>() {
public String transformEntry(String key, String value) {
return (sensibleVariables != null && sensibleVariables.contains(key))
Expand All @@ -139,15 +143,15 @@ public String transformEntry(String key, String value) {
return this;
}

dao.saveEnvironment(rootDir, envMap);
dao.saveEnvironment(rootDir, toWrite);
} catch (Throwable e) {
e.printStackTrace();
}

return this;
}


@CheckForNull
private Map<String, String> getEnvironment(@CheckForNull Run<?, ?> build) throws EnvInjectException {

if (build == null) {
Expand All @@ -165,14 +169,18 @@ private Map<String, String> getEnvironment(@CheckForNull Run<?, ?> build) throws

/**
* Retrieves an owner {@link Run} of this action.
* @return {@link Run}, which contains the action
* @return {@link Run}, which contains the action.
* May be {@code null} if and only if the action is not attached to the run.
* @since TODO
*/
@CheckForNull
public Run<?,?> getOwner() {
return build;
}

@SuppressWarnings("unused")
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
justification = "Data migration")
private Object readResolve() throws ObjectStreamException {

if (resultVariables != null) {
Expand Down
@@ -1,19 +1,23 @@
package org.jenkinsci.lib.envinject;

import javax.annotation.Nonnull;

/**
* Exception type for the EnvInject logic.
*
* @author Gregory Boissinot
*/
public class EnvInjectException extends Exception {

public EnvInjectException(String s) {
public EnvInjectException(@Nonnull String s) {
super(s);
}

public EnvInjectException(Throwable throwable) {
public EnvInjectException(@Nonnull Throwable throwable) {
super(throwable);
}

public EnvInjectException(String s, Throwable throwable) {
public EnvInjectException(@Nonnull String s, @Nonnull Throwable throwable) {
super(s, throwable);
}
}
11 changes: 7 additions & 4 deletions src/main/java/org/jenkinsci/lib/envinject/EnvInjectLogger.java
Expand Up @@ -3,27 +3,30 @@
import hudson.model.TaskListener;

import java.io.Serializable;
import javax.annotation.Nonnull;

/**
* @author Gregory Boissinot
*/
public class EnvInjectLogger implements Serializable {

private TaskListener listener;
@Nonnull
private final TaskListener listener;

public EnvInjectLogger(TaskListener listener) {
public EnvInjectLogger(@Nonnull TaskListener listener) {
this.listener = listener;
}

@Nonnull
public TaskListener getListener() {
return listener;
}

public void info(String message) {
public void info(@Nonnull String message) {
listener.getLogger().println("[EnvInject] - " + message);
}

public void error(String message) {
public void error(@Nonnull String message) {
listener.getLogger().println("[EnvInject] - [ERROR] - " + message);
}
}
Expand Down
Expand Up @@ -38,13 +38,13 @@ public Action getEnvInjectAction(AbstractBuild<?, ?> build) {
}
}
} catch (ClassNotFoundException e) {
LOGGER.log(Level.FINEST, String.format("hudson.matrix.MatrixRun is not installed", e));
LOGGER.log(Level.FINEST, "hudson.matrix.MatrixRun is not installed", e);
} catch (NoSuchMethodException e) {
LOGGER.log(Level.WARNING, String.format("The method getParentBuild does not exist for hudson.matrix.MatrixRun", e));
LOGGER.log(Level.WARNING, "The method getParentBuild does not exist for hudson.matrix.MatrixRun", e);
} catch (IllegalAccessException e) {
LOGGER.log(Level.WARNING, String.format("There was a problem in the invocation of getParentBuild in hudson.matrix.MatrixRun", e));
LOGGER.log(Level.WARNING, "There was a problem in the invocation of getParentBuild in hudson.matrix.MatrixRun", e);
} catch (InvocationTargetException e) {
LOGGER.log(Level.WARNING, String.format("There was a problem in the invocation of getParentBuild in hudson.matrix.MatrixRun", e));
LOGGER.log(Level.WARNING, "There was a problem in the invocation of getParentBuild in hudson.matrix.MatrixRun", e);
}
actions = build.getActions();
for (Action action : actions) {
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.lib.envinject.service;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.jenkinsci.lib.envinject.EnvInjectException;

import java.io.*;
Expand All @@ -21,6 +22,7 @@ public class EnvInjectSavable {
private static final String ENVINJECT_TXT_FILENAME = "injectedEnvVars.txt";
private static final String TOKEN = "=";

@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "Deprecated class")
public Map<String, String> getEnvironment(File envInjectBaseDir) throws EnvInjectException {

if (envInjectBaseDir == null) {
Expand All @@ -34,7 +36,7 @@ public Map<String, String> getEnvironment(File envInjectBaseDir) throws EnvInjec
return null;
}
fileReader = new FileReader(f);
Map result = new HashMap();
final Map<String, String> result = new HashMap<String, String>();
fromTxt(fileReader, result);
return result;
} catch (FileNotFoundException fne) {
Expand All @@ -50,6 +52,7 @@ public Map<String, String> getEnvironment(File envInjectBaseDir) throws EnvInjec
}
}

@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "Deprecated class")
public void saveEnvironment(File rootDir, Map<String, String> envMap) throws EnvInjectException {
FileWriter fileWriter = null;
try {
Expand Down Expand Up @@ -99,7 +102,7 @@ private void fromTxt(FileReader fileReader, Map<String, String> result) throws E

private void toTxt(Map<String, String> envMap, FileWriter fw) throws IOException {
for (Map.Entry<String, String> entry : envMap.entrySet()) {
fw.write(String.format("%s%s%s\n", entry.getKey(), TOKEN, entry.getValue()));
fw.write(String.format("%s%s%s%n", entry.getKey(), TOKEN, entry.getValue()));
}
}

Expand Down

0 comments on commit 438588e

Please sign in to comment.